Hi there!
I am quite new to the Arduino scene and my coding skills are quite ... dusty over the years (don't know what's the right term in english for that :D)
So currently I am trying to catch CAN-BUS messages and working with the bytes 0 and 1 and also with 2 and 3.
The catching is working fine so far (tried a bit with existing libs). Also I stored the values in an array.
But know:
How can I combine the Values of the Array on Index 7 and 6 / 5 and 4 and store them in a new variables.
First here is the Code:
#include <CAN.h>
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("CAN Receiver");
// Start the CAN bus at 250 kbps
if (!CAN.begin(250E3)) {
Serial.println("Starting CAN-BUS failed!");
while (1);
}
}
void loop() {
int i_Byte;
int a_Buffer[8];
int i_NOx;
int i_O2;
// Try to parse packet
int i_PacketSize = CAN.parsePacket();
if (i_PacketSize) {
// Received a packet
Serial.print("Received ");
if (CAN.packetExtended()) {
Serial.print("extended ");
}
if (CAN.packetRtr()) {
// Remote transmission request, packet contains no data
Serial.print("RTR ");
}
Serial.print("packet with id 0x");
Serial.print(CAN.packetId(), HEX);
if (CAN.packetRtr()) {
Serial.print(" and requested length ");
Serial.println(CAN.packetDlc());
} else {
Serial.print(" and length ");
Serial.println(i_PacketSize);
// Only print packet data for non-RTR packets
while (int i_Index = CAN.available()) {
i_Index--;
byte b_ByteValue = CAN.read();
//Serial.print(b_ByteValue, HEX);
//Serial.print(" ");
//Serial.println();
a_Buffer[i_Index] = b_ByteValue;
Serial.print(a_Buffer[i_Index], HEX);
Serial.print(" ");
}
Serial.println();
Serial.println();
for (int i = 0; i < 8; i = i + 1) {
Serial.println(a_Buffer[i], HEX);
}
}
Serial.println();
}
}
So and this is my serial console output:
09:23:55.861 -> Received extended packet with id 0x18F00F52 and length 8
09:23:55.861 -> AF F 32 81 45 1F 1F 1F
09:23:55.861 ->
09:23:55.861 -> 1F
09:23:55.861 -> 1F
09:23:55.861 -> 1F
09:23:55.861 -> 45
09:23:55.861 -> 81
09:23:55.861 -> 32
09:23:55.861 -> F
09:23:55.861 -> AF
09:23:55.861 ->
So quite nice so far.
Now I want to combine Index 6 and 7 of the array to 0FAF and there is my second problem with the missing leading 0 (think this is wy the values is <16, right?)
Can anybody can give me a hint, to an function, or says "Na ur code is sh*t!"?
Would be kind