I'm using PCAN-RS-232 to convert CAN message to serial and vice versa. the problem is that sometimes our serial commands are more than 8 bytes so when I want to send these messages from CAN I have to send them as 2 separate CAN Message and when I got two of them to merge them together and write them in serial. To understand which message should merge to other messages I used the first byte of Data that means If I put it 0x11 it should get another message from CAN and merge its data to the previous one and send them together. I can get the first message correctly when the length of the message is equal or less than 8 but when I am sending 2 CAN message the second message is not correctly receiving and the response of "CAN_UserRead(CAN_BUS1, &CanRxMsg);" is always "CAN_ERR_FAIL"
(I also increased the length of the buffer so that is not the problem)
Code: Select all
void ProcessMsgFromCan()
//*************************************************
//! @brief
//! Reads message from CAN.
//!
//! @param void
//!
//! @return void
//!
//-------------------------------------------------
{
u8_t j; //iterator over CAN
u8_t counter;
counter = 0;
SerXmtBufPtr = 0;
if (CAN_UserRead (CAN_BUS1, &CanRxMsg) == CAN_ERR_OK) // read one CAN message
{
for (j = 0; j < 7; ++j) {//Read the first message and add it in buffer
SerXmtBuf[SerXmtBufPtr + (j * 2)] = hex2ascii((CanRxMsg.Data8[j + 1] & 0xF0) >> 4);
SerXmtBuf[SerXmtBufPtr + 1 + (j * 2)] = hex2ascii(CanRxMsg.Data8[j + 1] & 0x0F);
}
if (CanRxMsg.Data8[0] == 0x11) { //check if the first byte of first CAN message is equal to 0x11
flag = 1;
}
}
SerXmtBufPtr = SerXmtBufPtr + 14; //Length of message is 14 because we are adding 7 bytes to serial
if (flag) { //IF the First byte of First CAN message is equal to 0x11
if (CAN_UserRead(CAN_BUS1, &CanRxMsg2) == CAN_ERR_OK){ //read second CAN message
for (j = 0; j < 7; ++j) { //Read the second message and add it to buffer
SerXmtBuf[SerXmtBufPtr + (j * 2)] = hex2ascii((CanRxMsg2.Data8[j + 1] & 0xF0) >> 4);
SerXmtBuf[SerXmtBufPtr + 1 + (j * 2)] = hex2ascii(CanRxMsg2.Data8[j + 1] & 0x0F);
}
counter = counter + 1;
}
}
SerXmtBufPtr = SerXmtBufPtr + (counter * 14);
SerXmtBuf[SerXmtBufPtr] = '\r'; // Append <CR>
SerXmtBufPtr++;
SER_Write(SER_PORT1, &SerXmtBuf, SerXmtBufPtr);
SerXmtBufPtr = 0;
counter = 0;
}
When I am sending these 2 CAN messages
[0x11,0x21,0x22,0x23,0x24,0x25,0x26,0x27]
and
[0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07]]
the serial result is
0x21 0x22 0x23 0x24 0x25 0x26 0x27 \r
but what I want is
0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x01 0x02 0x03 0x04 0x05 0x06 0x07 \r