Hi all! I'm trying to set up a simple CAN bus between two Teensy 3.5 boards each with a little board I made that includes the necessary circuitry to transmit and receive on a bus.
I'm just running some tests before trying to communicate with a Bosch motor and I seem to have a small issue.
I've set up the following simple programme to transmit and receive a simple message across the bus:
#include <FlexCAN.h> //Can Bus
//------------DIAGNOSTIC SERIAL DUMP------------
static uint8_t hex[17] = "0123456789abcdef";
static void hexDump(uint8_t dumpLen, uint8_t *bytePtr)
{
uint8_t working;
while ( dumpLen-- ) {
working = *bytePtr++;
Serial.write( hex[ working >> 4 ] );
Serial.write( hex[ working & 15 ] );
}
Serial.write('\r');
Serial.write('\n');
}
//------------CAN Variables------------
CAN_message_t can_MsgRx, can_MsgTx;
bool CANWaiting = false;
void setup() {
Serial.begin(9600);
Can0.begin(500000);
delay(500);
}
void loop() {
CAN_SEND();
delay(500);
CAN_Read();
delay(500);
}
void printFullBin(long long data) {
for (int c = (sizeof(data) * 8) - 1; c >= 0; c--)
{
Serial.print((byte)(data >> c) & 1);
}
Serial.println();
}
void CAN_SEND() {
can_MsgTx.ext = 0;
can_MsgTx.id = 0x6001;
can_MsgTx.len = 8;
can_MsgTx.buf[0] = 0x01;
can_MsgTx.buf[1] = 0x02;
can_MsgTx.buf[2] = 0x03;
can_MsgTx.buf[3] = 0x04;
can_MsgTx.buf[4] = 0x05;
can_MsgTx.buf[5] = 0x06;
can_MsgTx.buf[6] = 0x07;
can_MsgTx.buf[7] = 0x08;
can_MsgTx.flags.extended = 0;
can_MsgTx.flags.remote = 0;
Serial.print("CAN SENT: "); Serial.print(can_MsgTx.id); Serial.print(" "); hexDump(8, can_MsgTx.buf); //Diagnostic - Dump message to Serial//@@@@@@@@@@@@@@@@@@@@@@@@@@@CAN DIAGNOSTICS@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//Serial.println(currentMillis / 10);
Can0.write(can_MsgTx); //Send message
}
void CAN_Read() {
if (Can0.available() > 0) {//Is a response waiting?
Can0.read(can_MsgRx);
Serial.print("CAN Received: "); Serial.print(can_MsgRx.id); Serial.print(" "); hexDump(8, can_MsgRx.buf);
//printFullBin(can_MsgRx.buf);
}
}
After setting this up and reading the serial monitors, I can see that the data in the main buffer (can_MsgRx.buf) is received no problem at all, so the message is successfully sent and received across the two boards. I then tried to check the CAN message ID, but this does not seem to be received correctly.
On the transmit board, it reports that the message ID for the outgoing message is 0x6001 (24577).
On the receive board, the data is received correctly, but the message ID reads in the serial monitor as just 1.
I'm surprised that the data could arrive correctly but not the message ID. Is there anything obviously wrong with what I'm doing that could cause this. Is it that I'm reading the id wrong, or could it be a problem with FlexCAN? I'm using the Collin80 fork.