模拟接收
模拟发送
package mainimport ("context""fmt""go.einride.tech/can""go.einride.tech/can/pkg/candevice""go.einride.tech/can/pkg/socketcan"
)func main() {// linux系统设置// sudo ip link add dev can0 type vcan //添加can0网卡// sudo ip link set dev can0 up //开启can0// https://github.com/einride/can-go// Setting up a CAN interfaced, _ := candevice.New("can0")d.SetBitrate(250000)d.SetUp()defer d.SetDown()connSend, _ := socketcan.DialContext(context.Background(), "can", "can0")// 创建 CAN 帧frame := can.Frame{ID: 0x701, // 设置消息 IDLength: uint8(8), // 数据长度(最大 8)Data: [8]uint8{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, // 数据部分(设置您的数据)}tx := socketcan.NewTransmitter(connSend)_ = tx.TransmitFrame(context.Background(), frame)// cansend can0 123#DEADBEEF //发送(标准帧,数据帧,ID:123,date:DEADBEEF)connRecv, _ := socketcan.DialContext(context.Background(), "can", "can0")recv := socketcan.NewReceiver(connRecv)for recv.Receive() {frame := recv.Frame()fmt.Printf("Received: ID=%x Data=%v\n", frame.ID, frame.Data)}
}