Hi,
My apologies for how simple this question is, but I'm having a bit of a mental block (not uncommon!) and am going round in circles.
I have a CANBUS shield and after a bit of trial and error I am successfully using an example sketch to display the data that is received to the serial monitor with an Arduino UNO.
The sketch:
// demo: CAN-BUS Shield, receive data
#include <mcp_can.h>
#include <SPI.h>
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
MCP_CAN CAN0(10); // Set CS to pin 10
void setup()
{
Serial.begin(115200);
CAN0.begin(CAN_500KBPS); // init can bus : baudrate = 500k
pinMode(2, INPUT); // Setting pin 2 for /INT input
Serial.println("MCP2515 Library Receive Example...");
}
void loop()
{
if(!digitalRead(2)) // If pin 2 is low, read receive buffer
{
CAN0.readMsgBuf(&len, rxBuf); // Read data: len = data length, buf = data byte(s)
rxId = CAN0.getCanId(); // Get message ID
Serial.print("ID: ");
Serial.print(rxId, HEX);
Serial.print(" Data: ");
for(int i = 0; i<len; i++) // Print each byte of the data
{
if(rxBuf[i] < 0x10) // If data byte is less than 0x10, add a leading zero
{
Serial.print("0");
}
Serial.print(rxBuf[i], CAN);
Serial.print(" ");
}
Serial.println();
}
}
And the data I receive displayed in the serial monitor is:
MCP2515 Library Receive Example...
ID: 1000 Data: 20 48 02 33 04 05 012 123
ID: 1003 Data: 55 130 123 40 16 00 02 162
ID: 1000 Data: 20 48 02 33 04 05 012 123
ID: 1003 Data: 55 130 123 40 16 00 02 162 ...etc
The above is actually being generated from a second Arduino & CANBUS shield but would normally be coming from an Emerald engine management ECU link here. I have read from the ECU directly too and received all 4 sets of data OK, I'm just simulating it with a 2nd Arduino here to save taking the ECU off the car every time, plus I'm only really interested in some of the data from ID 1000 & ID 1003 anyway.
So my first question is, rather than printing this data in the serial monitor, how do I actually get the numbers into the sketch to use them? I'm fairly confident that once I have them, I'll be able to do calculations etc on them, I'm just stuck at what is probably a very low hurdle!
Secondly, how do I split out (or in some cases, join up) the data? For instance, the Coolant Temperature is byte 1 of ID 1003 so I just need the 130 on it's own in that case. Whereas the RPM is made up of bytes 0 & 1 of ID 1000, so I actually need to see 2048 rather than the separate 20 & 48 that it comes in as.
Searching seems to suggest that Strings & CharAt might be what I need, but I don't understand how to implement them