Longan-Labs / Serial_CAN_Arduino

MIT License
53 stars 22 forks source link

Extra bytes break library #4

Open ir-fuel opened 4 years ago

ir-fuel commented 4 years ago

Hi.

I am using this library and the serial CAN module to talk to a race car dashboard that supports custom CAN in and out.

This works 95% of the time, but sometimes the data stream contains extra bytes (anywhere between 3 and 7) that don't make sense to me. In theory we can just discard those extra bytes, and skip to the beginning of the next CAN message. In practice what happens is this library gets out of sync and starts sending back CAN messages which are offset in the byte stream.

example:

normal message:

0x0 0x0 0x0 0xA1 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8

I receive the above in a loop, no problem. Then all of the sudden this arrives:

0xC1 0xA4 0x3 0x5

(this is an example, can be any random sequence and the number of bytes also varies)

followed by:

0x0 0x0 0x0 0xA1 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8

What happens in the library. It builds the following CAN command:

0xC1 0xA4 0x3 0x5 0x0 0x0 0x0 0xA1 0x1 0x2 0x3 0x4

and sends it back to the caller. Of course I can't do anything with that as I don't understand the command. From there on the entire CAN stream is offset 4 bytes and I never get a correct command again.

What I did to fix this is to check that when reading a sequence of bytes that the first 4 are 0x0 0x0 0x0 0xA1 and the last 2 are 0x7 0x8, but this of course only works because I know that the input data will always start with this and that the last 2 bytes are 2 fixed values. This is in no way a generic solution for the 'out of sync' problem.

Any idea?

BrianDen commented 2 years ago

I have the same problem here.. how did you write your code to check first 4 bytes?