einride/can-go
소개
Controller Area Network (CAN) SDK for Go.
시스템 및 인프라측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- 0
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
Original:electric_plug: go.einride.tech/can
CAN toolkit for Go programmers.
can-go makes use of the Linux SocketCAN abstraction for CAN communication. (See the SocketCAN documentation for more details).
Installation
go get -u go.einride.tech/can
Examples
Setting up a CAN interface
import "go.einride.tech/can/pkg/candevice"
func main() {
// Error handling omitted to keep example simple
d, _ := candevice.New("can0")
_ := d.SetBitrate(250000)
_ := d.SetUp()
defer d.SetDown()
}
Receiving CAN frames
Receiving CAN frames from a socketcan interface.
import "go.einride.tech/can/pkg/socketcan"
func main() {
// Error handling omitted to keep example simple
conn, _ := socketcan.DialContext(context.Background(), "can", "can0")
recv := socketcan.NewReceiver(conn)
for recv.Receive() {
frame := recv.Frame()
fmt.Println(frame.String())
}
}
Sending CAN frames/messages
Sending CAN frames to a socketcan interface.
import "go.einride.tech/can/pkg/socketcan"
func main() {
// Error handling omitted to keep example simple
conn, _ := socketcan.DialContext(context.Background(), "can", "can0")
frame := can.Frame{}
tx := socketcan.NewTransmitter(conn)
_ = tx.TransmitFrame(context.Background(), frame)
}
Generating Go code from a DBC file
It is possible to generate Go code from a .dbc file.
$ go run go.einride.tech/can/cmd/cantool generate <dbc file root folder> <output folder>
In order to generate Go code that makes sense, we currently perform some validations when parsing the DBC file so there may need to be some changes on the DBC file to make it work
After generating Go code we can marshal a message to a frame:
// import etruckcan "github.com/myproject/myrepo/gen"
auxMsg := etruckcan.NewAuxiliary().SetHeadLights(etruckcan.Auxiliary_HeadLights_LowBeam)
frame := auxMsg.Frame()
Or unmarshal a frame to a message:
// import etruckcan "github.com/myproject/myrepo/gen"
// Error handling omitted for simplicity
_ := recv.Receive()
frame := recv.Frame()
var auxMsg *etruckcan.Auxiliary
_ = auxMsg.UnmarshalFrame(frame)
Running integration tests
Building the tests:
$ make build-integration-tests
Built tests are placed in build/tests.
The candevice test requires access to physical HW, so run it using sudo. Example:
$ sudo ./build/tests/candevice.test
> PASS
관련 프로젝트 살펴보기
불러오는 중…