# Sevcon CAN programming with Arduino



## waterarc (Mar 16, 2016)

I see that member DaveAK was working on an Arduino sketch for interpreting the CAN traffic from Sevcon PowerPak controllers in the 2011-2012 timeframe (see thread Sevcon CAN Calibrator Emulator). I have a Sevcon PP784 that I am likewise trying to program with an Arduino and SparkFun CAN shield. Looks like Dave's website that had his code has been down for a number of years. Does anyone happen to have a copy of his Arduino sketch and library that they would be able to share? It sounds like he made the software freely available on his website at the time, so maybe he would be OK with it being posted here for others who may be messing around with the Sevcons?

If anyone else has successfully developed Arduino libraries and sketches for CAN programming in general, I would certainly be interested in any advice!

Thanks,
JJ


----------



## bigmouse (Sep 28, 2008)

Here's some example code I wrote based on what I have running on a Teensy 3.1/3.2. 

I've provided some functions to send to or receive messages from the bus.

I'm using the FlexCAN library. Code for the shield is similar, but the structs used in FlexCAN aren't used by the library I've used with the shields before. Those use the MCP-CAN library which uses arrays for messages. The switch-case with that library works the same, but the parsing is done differently.

CANTEST is defined as the message ID for the example below.

For each message, add a "case" to the switch-case in the send and/or read functions as necessary.

int1-3 are examples only, but are global variables declared elsewhere. I'm showing how to send/receive messages that contain 3 16-bit values and 2 8-bit values. For other sizes (I use a lot of 12-bit values and 1-bit flags in my own code), you'll need to come up with the bit-masks yourself.

The sendCan() function can be called periodically using a metro interval timer. Values in the global variables may be updated several times between CAN message transmission.

The readCAN() function is run whenever there's idle time. It is called in the main loop. If no CAN messages are waiting, then this will just skip to the end when called.


```
#define CANTEST         0x221

#include <FlexCAN.h>
FlexCAN CANbus(500000);

int txCount,rxCount;

static CAN_message_t txmsg, rxmsg;

void initCANmessages(void)
{
  CANbus.begin();
  txmsg.len = 8;
  rxmsg.len = 8;
}

void sendCan(uint32_t ident)
{
  txmsg.id = ident;
  switch(txmsg.id)
  {
    case CANTEST:
      txmsg.buf[0] = int1 & 0xFF;
      txmsg.buf[1] = int1 >> 8;
      txmsg.buf[2] = int2 & 0xFF;
      txmsg.buf[3] = int2 >> 8;
      txmsg.buf[4] = int3 & 0xFF;
      txmsg.buf[5] = int3 >> 8;
      txmsg.buf[6] = byte1;
      txmsg.buf[7] = byte2;
      break;
  }
}

void readCAN(void)
{
  while(CANbus.available())
  {
    CANbus.read(rxmsg);
    switch(rxmsg.id)
    {
      case CANTEST:
        int1 = (rxmsg.buf[1] << 8) | rxmsg.buf[0];
        int2 = (rxmsg.buf[3] << 8) | rxmsg.buf[2];
        int3 = (rxmsg.buf[5] << 8) | rxmsg.buf[4];
        byte1 = rxmsg.buf[6];
        byte2 = rxmsg.buf[7];
        break;
    }
  }
}
```
To send a specific message, call the sendCAN() function as below:

```
sendCan(CANTEST);
```


----------



## waterarc (Mar 16, 2016)

bigmouse,

Thanks for the code snip. Currently I'm in the CAN sniffing stage and trying to make sense of unfiltered data as I have no object dictionary to fall back on. Once I get a couple of WAGs correct, I'll be able to employ a similar case structure on the array elements to peel out the data.

If anyone out there has an object dictionary for the PowerPaks, I'll owe you a couple cold ones.....


----------



## Darci Junior (10 mo ago)

waterarc said:


> bigmouse,
> 
> Thanks for the code snip. Currently I'm in the CAN sniffing stage and trying to make sense of unfiltered data as I have no object dictionary to fall back on. Once I get a couple of WAGs correct, I'll be able to employ a similar case structure on the array elements to peel out the data.
> 
> If anyone out there has an object dictionary for the PowerPaks, I'll owe you a couple cold ones.....


Hello waterarc. I'm also sniffing the CAN. Do you have any information?


----------

