gocan is a Go module for communicating with automotive CAN and CAN FD
networks. It provides one stack from hardware access and raw capture through
ISO-TP and UDS, with semantic codecs for DBC and CANdela diagnostic data.
go get github.com/tomrford/gocanTwo buses on one virtual network share a capture and exchange frames in-process.
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/tomrford/gocan"
"github.com/tomrford/gocan/drivers/virtual"
)
func main() {
ctx := context.Background()
capture := gocan.NewCapture()
var network virtual.Network
sender, err := network.Open(ctx, capture, virtual.Config{ID: 1, Name: "sender"})
if err != nil {
log.Fatal(err)
}
defer sender.Close()
receiver, err := network.Open(ctx, capture, virtual.Config{ID: 2, Name: "receiver"})
if err != nil {
log.Fatal(err)
}
defer receiver.Close()
frame, err := gocan.NewFrame(0x123, []byte{1, 2, 3, 4}, 0)
if err != nil {
log.Fatal(err)
}
cursor := capture.End()
if err := sender.Send(ctx, frame); err != nil {
log.Fatal(err)
}
wait, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
received, _, err := capture.Next(wait, gocan.FrameKey{
Bus: receiver.ID(),
ID: frame.ID,
Direction: gocan.DirectionReceive,
}, cursor)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%x\n", received.Frame.Data[:received.Frame.DataLength()])
}| Package | Purpose |
|---|---|
gocan |
Raw CAN and CAN FD frames, the common bus interface, and concurrent multi-bus capture |
drivers |
Discovery and opening of physical CAN channels |
drivers/virtual |
In-process CAN networks for development and tests |
drivers/conformance |
Driver-independent test suite for gocan.Bus implementations |
cyclic |
Recurring raw frame transmission |
asc |
Vector ASCII trace writing |
mf4 |
MDF 4.10 raw CAN/CAN FD export with DBC attachments, event markers, compression and application metadata |
recorder |
Lifecycled trace recording with safe flush checkpoints |
dbc |
DBC parsing with original source retention, DBC writing, and CAN frame encoding and decoding |
j1939 |
J1939 identifiers, passive decoding, active address claiming and classical BAM/RTS-CTS transport |
isotp |
ISO-TP payload transport over classical CAN and CAN FD |
uds |
Raw exchanges and typed Unified Diagnostic Services operations |
xcp |
Sequential XCP-on-CAN/CAN FD sessions and raw byte-addressed memory reads |
cdd |
CANdela ECU/variant selection, diagnostic service catalogs and metadata, and data-record codecs |
| Driver | Platform | CAN | CAN FD | Qualification |
|---|---|---|---|---|
| Virtual | Portable, including macOS | Yes | Yes | Development and tests |
| SocketCAN | Linux | Yes | Yes | Software and virtual interfaces |
| PCAN | Windows | Yes | Yes | Physical adapters |
| Vector | Windows x64 | Yes | Yes | Physical adapters |
| NI-XNET | Windows x64 | Yes | Yes | PXI-8512 CAN5/CAN6, ISO FD at 500k/2M and 500k/4M |
| ZLG | Windows x64 | Yes | USBCANFD | USBCAN-I tested; receive loss under load; USBCANFD unqualified |
Physical channels are discovered and opened through drivers. SocketCAN uses
link timing configured by Linux. PCAN, Vector and NI-XNET accept shared exact
CAN FD bit timing through the same public configuration; ZLG accepts only its
own rate presets.
go test -race ./...
go vet ./...
gofmt -l .gocan is licensed under the MIT License.