/gocan

CAN (Controller Area Network) Bus Simulation in Go

Primary LanguageGo

Synopsis

A CAN (Controller Area Network) Bus simulation in GO.

Code Example

This is a simple Timed Node implementation to send frames at a fixed frequency.

type timed struct {
	T *Transceiver
	periodMs uint32
}

/* Returns a new TimedNode which sends random data messages every 'timeMs'
   as soon the Start() method is called */
func NewTimedNode(bus chan *Frame, timeMs uint32, id int) *timed {
	t := NewTransceiver(bus, id)
	t.Mask = 0xFFFFFFFF
	
	node := &timed{
		T : t,
		periodMs : timeMs,
	}

	return node  
}

/* Starts the Timed Node Simulation, must be run in a separate goroutine */
func (node *timed) Start() {
	//start node transceiver
	go node.T.Run()

	ticker := time.NewTicker(time.Millisecond * time.Duration(node.periodMs))

	for tick := range ticker.C {
		node.T.Send(&Frame{Id: 0x10, TimeStamp: tick, Data: RandomData()}) 
	}
}

This Example Program shows how to start (NumNodes) timed Nodes with a Logger and Run the Simulation

func Example() {
	fmt.Println("GoCAN example")

	//initialize
	bus := NewBus("Bus1")
	var timeds []*timed
	for i := 1; i <= NumNodes; i++ {
		node := NewTimedNode(bus, uint32(i*1000), i*10))
		timeds = append(timeds, node)
	} 
	logger := NewLogger(bus, 0)

	//run
	go bus.Simulate()
	go logger.Start()
	for _, t := range timeds {
		t2 := t //fresh variable copy
		go t2.Start()
	}

}

To have a node on multiple buses just embed a Transceiver for every bus connected to the Node.

Installation

Just clone this rep into you src folder (see https://golang.org/doc/code.html for go workspace setup) and run go install.

cd $GOPATH/src
git clone https://github.com/danielbk18/gocan
cd gocan
go install

After this just import the pkg "gocan" into your project to use it:

package main

import(
 "fmt"
 "gocan"
 )

func main() {
	gocan.Example()
	fmt.Scanln()
}