• Stars
    star
    198
  • Rank 196,898 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Connect to a CAN bus in Go

can

can provides an interface to a CAN bus to read and write frames. The library is based on the SocketCAN network stack on Linux.

Hardware

I'm using a Raspberry Pi 2 Model B and PiCAN2 CAN-Bus board for Raspberry Pi 2 to connect to a CAN bus.

Software

The Raspberry Pi runs Raspbian.

Configuration

Update /boot/config.txt with

dtparam=spi=on 
dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25 
dtoverlay=spi-bcm2835-overlay

and reboot.

After phyiscally connecting to the CAN bus, you have to set up the can network interface for a specific bitrate, i.e. 50 kB

sudo ip link set can0 up type can bitrate 50000

Running ifconfig should now include the can0 interface.

Test your configuration

You should test if you actually receive data from the CAN bus. You can either use the candump tool from the can-utils or a simple reimplementation under cmd/candump.go.

Either way you will see something like this

> go run $GOSRC/github.com/brutella/can/cmd/candump.go -if can0

can0 100  [6] 20 83 0C 00 67 29        ' ...g)'
can0 701  [1] 05                       '.'

Usage

Setup the CAN bus

bus, _ := can.NewBusForInterfaceWithName("can0")
bus.ConnectAndPublish()

Send a CAN frame

frm := can.Frame{
	ID:     0x701,
	Length: 1,
	Flags:  0,
	Res0:   0,
	Res1:   0,
	Data:   [8]uint8{0x05},
}

bus.Publish(frm)

Receive a CAN frame

bus.SubscribeFunc(handleCANFrame)

func handleCANFrame(frm can.Frame) {    
    ...
}

There is more to learn from the documentation.

Contact

Matthias Hochgatterer

Github: https://github.com/brutella

Twitter: https://twitter.com/brutella

License

can is available under the MIT license. See the LICENSE file for more info.

More Repositories

1

hc

hc is a lightweight framework to develop HomeKit accessories in Go.
Go
1,741
star
2

chatheads

An implementation of Facebook's ChatHeads on iOS.
Objective-C
883
star
3

hkcam

Open-Source HomeKit Surveillance Camera
Go
881
star
4

hap

The HomeKit Accessory Protocol (hap) implemented in Go
Go
289
star
5

dnssd

This library implements Multicast DNS (mDNS) and DNS-Based Service Discovery (DNS-SD) for Zero Configuration Networking in Go.
Go
174
star
6

hklifx

LIFX HomeKit bridge
Go
106
star
7

hkknx-public

hkknx is a HomeKit KNX bridge for KNX.
89
star
8

swift-csv

Fast and memory-efficient CSV library in Swift.
Swift
80
star
9

hklight

Example project of a HomeKit light bulb using HomeControl
Go
39
star
10

canopen

Communicate with other CANopen nodes in Go
Go
26
star
11

TextViewLineNumbers

Implementation of NSTextView with line numbers
Swift
25
star
12

simplediff-swift

simplediff is a simple diff algorithm implementation in Swift.
Swift
16
star
13

nsuserdefaults-macros

Some handy NSUserDefaults macros/functions
Objective-C
15
star
14

NSTreePopUpButton

An NSPopUpButton subclass which supports binding to NSTreeController
Swift
15
star
15

uvr

Communicate with an UVR1611 over the CAN bus
Go
10
star
16

hkuvr1611

UVR1611 HomeKit bridge
Go
9
star
17

log-swift

Logging to console and file
Swift
9
star
18

Axt

A forgiving HTML SAX Parser for iOS
Objective-C
9
star
19

gouvr

Library to decode the data bus of UVR 1611 and similar devices
Go
7
star
20

hkuvr

HomeKit bridge for UVR1611
Go
5
star
21

hksymo

HomeKit bridge for Fronius Symo inverter
Go
3
star
22

Journal

Easy file logging in Swift
Swift
3
star
23

XCollectionData

Swift
2
star
24

comment-generator

Ruby classes to generate comment templates from source code
Ruby
2
star
25

git-diff

Compare git branches, tags and commits in your favourite diff tool.
Shell
1
star
26

C3Presentation

Slides about C++ programming built with deck.js and deckem
JavaScript
1
star
27

geizhals

Fetch prices from Geizhals
Go
1
star