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 hades32 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

can's Issues

Config.txt

Hi,

there is no config.txt in my system (its not a raspberry pi).
Why does I need to set it?

T

Not correct ID to dump extended frame format

Hi!

I run

  1. brutella/can/cmd/candump.go
  2. candump can0 (Linux standard utility)
  3. I execute the command - "cansend can0 18020F00#AFAF"

Output candump - "18020F00 [2] AF AF"

Output candump.go - "98020F00 [2] AF AF"

Why is the ID incorrect?
Also, all IDs are incorrect.

Screenshot attached

Снимок экрана 2022-08-17 в 08 46 38

Breakage in recent linux

Hi there, thanks for writing this awesome wrapper package.

I've just recently started having an issue, where I get the following output running the demo candump app

$ go run cmd/candump.go -if vcan0
2018/09/19 03:37:16 invalid argument
exit status 1

I traced the error down to the call to syscall.Bind() in NewReadWriteCloserForInterface, but I'm not familiar enough with the relationship between the golang syscall package and the C end to take that gap any further. It's possible that this is actually a lower level issue in the go stack, but I figured I'd come here first just in case.

The kernel version on the vm it broke for:

$ uname -a
Linux kali 4.17.0-kali3-amd64 #1 SMP Debian 4.17.17-1kali1 (2018-08-21) x86_64 GNU/Linux

(Just for reference, I've confirmed multiple times including with the socketcan C based canplayer/candump executables that my vcan socket is definitely present and up and functional).

Missing package name 'can' in README.md

change:

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

to:

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

(missing package name in README.md)

Question: is `.Publish` thread-safe?

Do the users of the package need to take care that the .Publish method is not invoked in parallel by different consumers in different go-routines or is it safe to call it from different go-routines?

Thanks for creating this nice library!

Listen Interval

Hey Guys,

in what interval does this library gets can bus signals?
Could my program pass some signals? Does it block while now signal comes?
What is with CPU usage while listening?

T

Bus State

Hello, I am trying to acces to bus state via golang to restart the bus in case of busoff state. I want to also see the bus load. Do you know how can I acces either the TEC or have a solution already for this kind of acces ?

add time stamp to received frames

Hi there,

I would love to get time stamps of received CAN frames as accurate as possible. At least for Linux it should be possible.

From Linux SocketCAN documentation:

An accurate timestamp can be obtained with an ioctl(2) call after reading a message from the socket:

struct timeval tv;
ioctl(s, SIOCGSTAMP, &tv);

PR follows.

Regards, Stephan

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.