Hello,
I am struggling to communicate with a CAN hardware device and receive back its hardware ID.
Program structure:
Send *IDN? to CAN hardware device and receive back ID and print to terminal.
Looking at my attached code, can you see any obvious mistakes?
Many thanks in advance.
Thanks,
Sparky84
// Required libraries
#include "variant.h"
#include <due_can.h>
//Leave defined if you use native port, comment if using programming port
#define Serial SerialUSB
void setup()
{
Serial.begin(115200);
// Initialize CAN0, Set the proper baud rates here
Can0.begin(CAN_BPS_250K);
Can0.watchFor();
}
void sendData()
{
CAN_FRAME outgoing;
outgoing.id = 0x00; // ***** 0x400
outgoing.extended = false;
outgoing.priority = 4; //0-15 lower is higher priority
outgoing.data.byte[0] = 0x00;
outgoing.data.byte[1] = 0x2a; // *
outgoing.data.byte[2] = 0x49; // I
outgoing.data.byte[3] = 0x44; // D
outgoing.data.byte[4] = 0x4e; // N
outgoing.data.byte[5] = 0x3f; // ?
outgoing.data.byte[6] = 0x00;
outgoing.data.byte[7] = 0x00;
Can0.sendFrame(outgoing);
}
void loop(){
CAN_FRAME incoming;
static unsigned long lastTime = 0;
if (Can0.available() > 0)
{
Can0.read(incoming);
for (int count = 0; count < incoming.length; count++)
{
Serial.print(incoming.data.bytes[count], HEX);
}
Serial.print("\r\n");
}
if ((millis() - lastTime) > 1000)
{
lastTime = millis();
sendData();
}
}