hello there
I have a CANBUS mcp 2515 and I am successfully using an example sketch to display the data that is received from a( viscosity)sensor that communicat using a can protocol to the serial monitor with an Arduino UNO.
The sketch:
Code: [Select]
// 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 < 0x10) // If data byte is less than 0x10, add a leading zero
- {*
- Serial.print("0");*
- }*
_ Serial.print(rxBuf*, CAN);_
_ Serial.print(" ");_
_ }_
_ Serial.println();_
_ }_
_}_
And the data I receive displayed in the serial monitor is:
MCP2515 Library Receive Example...
ID: 1CFD083F Data: D5 05 47 E8 FF FF 31 28
ID: 18FEEE3F Data: FF FF E9 25 FF FF FF FF
_...etc*_
Over and over
The above is actually being generated from a sensor.
So my first question is, rather than printing this data in the serial monitor, how do I actually get the hex 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 Viscosity data is byte 1 and 2 of ID 1CFD083F so I just need the d505 on it's own in that case. Whereas the Density is made up of bytes 3 & 4 of ID 1CFD083F, so I actually need to see 47E8 rather than the separate 47 & E8 that it comes in as.
Any help , links or anything.
and thanks