Coder Social home page Coder Social logo

can's Introduction

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.

can's People

Contributors

brutella avatar

Watchers

maddie avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.