CAN Bus interface

Provides a software interface to the CAN BUS. Internally this objects uses the SocketCan interface.

Object

class IQData {
     constructor CanBus();
     bool open( String interface );
     bool close();

     bool send( can_id , data : array of integers );
     Object receive( can_id );
}

.open

  • Opens the specified CAN interface. Returns false if the interface is not available or cannot be opened.

.close

  • Closes (releases) the CAN interface previously opened.

.send

Send data to the CAN Bus.

    bool send( can_id , data : array of integers );

Returns true if the provided data has been sent.
Parameters :

  • can_id : the CAN ID to be used for this message,
    • bit 0-28 : CAN identifier (11/29 bit)
    • bit 29 : error message frame flag (0 = data frame, 1 = error message)
    • bit 30 : remote transmission request flag (1 = rtr frame)
    • bit 31 : frame format flag (0 = standard 11 bit, 1 = extended 29 bit)
  • data : an array of integers. The length of the given array is used to set the frame length.

.receive

  • Waits for data. This is a blocking call.
        Object receive( can_id );
    

Returns an object with the following structure :

    {
        "send_to": <CAN ID of the sender>,
        "data":[ array of integers]
    }

Example

For a background on CAN Bus under linux, see :

/* Can bus example 
   to test under linux with no CAN phyiscal interface , we use vcan0
   setup :
   sudo ip link add dev vcan0 type vcan
   sudo ifconfig vcan0 up
*/

var interface_name = 'vcan0' ;

var bus = new CanBus();
print('Opening interface...');

if( bus.open(interface_name) === false ) {
    print('Cannot open interface ['+interface_name+']') ;
    exit();
}


// Test send data
// under linux, data can be monitored using tools from package :
// sudo apt-get install can-utils
// then :
// candump vcan0
var data = new Array();
data[0] = 0x01 ;
data[1] = 0x02 ;
data[2] = 0x02 ;
bus.send(0xf1,data) ; // candump will display : vcan0  0F1   [3]  01 02 02

// Test receive
// under linux , for example :
// cansend vcan0 002#0102030405
var x = bus.receive(0x02) ;
print( JSON.stringify(x));
// with given example, will print : {"send_to":2,"data":[1,2,3,4,5]}
Last update: January 8, 2022