github.com/blueinnovationsgroup/can-go@v0.0.0-20230518195432-d0567cda0028/README.md (about)

     1  :electric_plug: CAN Go
     2  ======================
     3  
     4  [![PkgGoDev](https://pkg.go.dev/badge/go.einride.tech/can)](https://pkg.go.dev/go.einride.tech/can) [![GoReportCard](https://goreportcard.com/badge/go.einride.tech/can)](https://goreportcard.com/report/go.einride.tech/can) [![Codecov](https://codecov.io/gh/einride/can-go/branch/master/graph/badge.svg)](https://codecov.io/gh/einride/can-go)
     5  
     6  CAN toolkit for Go programmers.
     7  
     8  can-go makes use of the Linux SocketCAN abstraction for CAN communication. (See the [SocketCAN](https://www.kernel.org/doc/Documentation/networking/can.txt) documentation for more details).
     9  
    10  Examples
    11  --------
    12  
    13  ### Setting up a CAN interface
    14  
    15  ```go
    16  func main() {
    17  	// Error handling omitted to keep example simple
    18  	d, _ := candevice.New("can0")
    19  	_ := d.SetBitrate(250000)
    20  	_ := d.SetUp()
    21  	defer d.SetDown()
    22  }
    23  ```
    24  
    25  ### Receiving CAN frames
    26  
    27  Receiving CAN frames from a socketcan interface.
    28  
    29  ```go
    30  func main() {
    31  	// Error handling omitted to keep example simple
    32  	conn, _ := socketcan.DialContext(context.Background(), "can", "can0")
    33  
    34  	recv := socketcan.NewReceiver(conn)
    35  	for recv.Receive() {
    36  		frame := recv.Frame()
    37  		fmt.Println(frame.String())
    38  	}
    39  }
    40  ```
    41  
    42  ### Sending CAN frames/messages
    43  
    44  Sending CAN frames to a socketcan interface.
    45  
    46  ```go
    47  func main() {
    48  	// Error handling omitted to keep example simple
    49  
    50  	conn, _ := socketcan.DialContext(context.Background(), "can", "can0")
    51  
    52  	frame := can.Frame{}
    53  	tx := socketcan.NewTransmitter(conn)
    54  	_ = tx.TransmitFrame(context.Background(), frame)
    55  }
    56  ```
    57  
    58  ### Generating Go code from a DBC file
    59  
    60  It is possible to generate Go code from a `.dbc` file.
    61  
    62  ```
    63  $ go run go.einride.tech/can/cmd/cantool generate <dbc file root folder> <output folder>
    64  ```
    65  
    66  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
    67  
    68  After generating Go code we can marshal a message to a frame:
    69  
    70  ```go
    71  // import etruckcan "github.com/myproject/myrepo/gen"
    72  
    73  auxMsg := etruckcan.NewAuxiliary().SetHeadLights(etruckcan.Auxiliary_HeadLights_LowBeam)
    74  frame := auxMsg.Frame()
    75  ```
    76  
    77  Or unmarshal a frame to a message:
    78  
    79  ```go
    80  // import etruckcan "github.com/myproject/myrepo/gen"
    81  
    82  // Error handling omitted for simplicity
    83  _ := recv.Receive()
    84  frame := recv.Frame()
    85  
    86  var auxMsg *etruckcan.Auxiliary
    87  _ = auxMsg.UnmarshalFrame(frame)
    88  
    89  ```
    90  
    91  Running integration tests
    92  -------------------------
    93  
    94  Building the tests:
    95  
    96  ```shell
    97  $ make build-integration-tests
    98  ```
    99  
   100  Built tests are placed in build/tests.
   101  
   102  The candevice test requires access to physical HW, so run it using sudo. Example:
   103  
   104  ```shell
   105  $ sudo ./build/tests/candevice.test
   106  > PASS
   107  ```