Hello everyone!
This question is related to my topic HERE, but as this is a followup problem I choose to keep it separate - maybe this helps others researching in the future.
What I basically am doing is reading a Vehicle CAN-Bus and wanting to display the various information that can be included in the 8Byte HEX Message.
To do so I choose to bitRead() all the bits from the HEX-Message, and write it into a new Array (called Complete_Message in my Sketch).
Then I use a section that the user "cattledog" gave me to cut out a section of the Complete Array and convert this part into a decimal value, maybe representing the engine RPM.
The Problem:
If I Simply fill an Array with 1s and 0s (like I did in the Array "TestComplete_Message") everything works fine.
If I then let this Loop:
for(byte i = 0; i<len; i++)
{
byte u=0;
for ( byte Bit=8; Bit>0; Bit--)
{
Complete_Message[(i*8)+u]=bitRead(rxBuf[i],Bit-1); // Put all the 0s and 1s in ONE Array after each other
Serial.print("Filling Position: "); // Debugging to see if the Message is structured correctly
Serial.print((i*8)+u);
Serial.print(" with Digit: ");
Serial.println(bitRead(rxBuf[i],Bit-1));
u++;
}
}
fill my array something goes wrong. I cant work with the array as with the Test.
I tried to terminate it, but no matter what I did, it don't work
Here is my complete code:
// CAN Receive Example
//
#include <mcp_can.h>
#include <SPI.h>
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string
char Complete_Message[64]="";
char TestCompleteMessage[] = "1111100000111000111110000011100011111000001110001111100000111000";
#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(5); // Set CS to pin 10
void setup()
{
Serial.begin(115200);
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
Serial.println("MCP2515 Library Receive Example...");
}
void loop()
{
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
Serial.println("CAN-Message Arrived!");
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if((rxId & 0x40000000) == 0x40000000)
{ // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.println(msgString);
} else
for(byte i = 0; i<len; i++)
{
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
Serial.println();
{
//len
for(byte i = 0; i<len; i++)
{
byte u=0;
for ( byte Bit=8; Bit>0; Bit--)
{
Complete_Message[(i*8)+u]=bitRead(rxBuf[i],Bit-1); // Put all the 0s and 1s in ONE Array after each other
Serial.print("Filling Position: "); // Debugging to see if the Message is structured correctly
Serial.print((i*8)+u);
Serial.print(" with Digit: ");
Serial.println(bitRead(rxBuf[i],Bit-1));
u++;
}
}
Serial.print("Size of Complete_Message: ");
Serial.println(sizeof(Complete_Message));
Serial.print("Complete Message= ");
Complete_Message[strlen(Complete_Message)+1] = '\0';
Serial.println(Complete_Message);
}
// The follwing part is just for testing of the Bits are in the correct order!
Serial.println();
Serial.print("0-7: ");
for ( byte i=0; i<8; i++)
{
Serial.println(Complete_Message[i]);
}
// The following part should filter out the bits needed to assmeble the CAN-Value
char * ptr = CompleteMessage; // Pointer for array
char input_binary_string[9] = "";
strncpy(input_binary_string, ptr + 0, 7); //extract 8 bits from 6 chars in
Serial.println(input_binary_string);
long value = strtol(input_binary_string, NULL, 2);
Serial.println(value);
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
and the Debug Info:
Entering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
MCP2515 Library Receive Example...
CAN-Message Arrived!
Standard ID: 0x008 DLC: 8 Data: 0x12 0xFF 0x01 0x01 0x01 0x01 0x01 0x01
Filling Position: 0 with Digit: 0
Filling Position: 1 with Digit: 0
Filling Position: 2 with Digit: 0
Filling Position: 3 with Digit: 1
Filling Position: 4 with Digit: 0
Filling Position: 5 with Digit: 0
Filling Position: 6 with Digit: 1
Filling Position: 7 with Digit: 0
Filling Position: 8 with Digit: 1
Filling Position: 9 with Digit: 1
Filling Position: 10 with Digit: 1
Filling Position: 11 with Digit: 1
Filling Position: 12 with Digit: 1
Filling Position: 13 with Digit: 1
Filling Position: 14 with Digit: 1
Filling Position: 15 with Digit: 1
Filling Position: 16 with Digit: 0
Filling Position: 17 with Digit: 0
Filling Position: 18 with Digit: 0
Filling Position: 19 with Digit: 0
Filling Position: 20 with Digit: 0
Filling Position: 21 with Digit: 0
Filling Position: 22 with Digit: 0
Filling Position: 23 with Digit: 1
Filling Position: 24 with Digit: 0
Filling Position: 25 with Digit: 0
Filling Position: 26 with Digit: 0
Filling Position: 27 with Digit: 0
Filling Position: 28 with Digit: 0
Filling Position: 29 with Digit: 0
Filling Position: 30 with Digit: 0
Filling Position: 31 with Digit: 1
Filling Position: 32 with Digit: 0
Filling Position: 33 with Digit: 0
Filling Position: 34 with Digit: 0
Filling Position: 35 with Digit: 0
Filling Position: 36 with Digit: 0
Filling Position: 37 with Digit: 0
Filling Position: 38 with Digit: 0
Filling Position: 39 with Digit: 1
Filling Position: 40 with Digit: 0
Filling Position: 41 with Digit: 0
Filling Position: 42 with Digit: 0
Filling Position: 43 with Digit: 0
Filling Position: 44 with Digit: 0
Filling Position: 45 with Digit: 0
Filling Position: 46 with Digit: 0
Filling Position: 47 with Digit: 1
Filling Position: 48 with Digit: 0
Filling Position: 49 with Digit: 0
Filling Position: 50 with Digit: 0
Filling Position: 51 with Digit: 0
Filling Position: 52 with Digit: 0
Filling Position: 53 with Digit: 0
Filling Position: 54 with Digit: 0
Filling Position: 55 with Digit: 1
Filling Position: 56 with Digit: 0
Filling Position: 57 with Digit: 0
Filling Position: 58 with Digit: 0
Filling Position: 59 with Digit: 0
Filling Position: 60 with Digit: 0
Filling Position: 61 with Digit: 0
Filling Position: 62 with Digit: 0
Filling Position: 63 with Digit: 1
Size of Complete_Message: 64
Complete Message=
0-7:
If I then just add ",BIN" to the line
Serial.println(Complete_Message[i],BIN);
the output is this:
0-7: 0
0
0
1
0
0
1
0
I tried what I could.
Where is the difference between assigning values to a char Array directly vs. filling it with data in a loop?
I also tried to initialize the array with 64 0s and then overwriting it in the loop. No luck either.
Glad for some insight...
Thanks for reading!