Searching and parsing data from serial.read

Econjack and all

Thank you very much for you help with this - I was able to use that as a seed to develop my first Arduino project, learn about Arduino and to expand the code to do everything I needed it to do - Not sure where the weekend went though - but it was a good challenge.

Several learnings and a couple of questions from the work. I'll ask my 2 questions first because I can't imagine anyone is interested in the J1587 description that will take ages to read.

  1. When assembling a string - what is the purpose of appending it with '\0' ????

  2. When reading a serial input I'm unable to detect the end of the message with '\n' so I get a 1 second pause for the read to timeout (default 1000ms). I've tried '\r' as well and that doesn't work. I can get round this by reducing the default timeout, but would like to know what I'm doing wrong and why I can't detect the end of the message buffer.

If you have nothing else to do - this is what I discovered on my first journey with Arduino.

I can't, as first thought, simply search for the parameter identifier (eg "BE ") and then read the two bytes after that, although I only realized this after I'd got it working. The problem is that "BE" could be a perfectly valid data word for another identifier that I'm not using, but the code would see the "BE" value and trigger the data extraction on the wrong data - it wouldn't happen a lot but it would happen.

So I went back to the drawing board but now with a fraction more knowledge. I have to process the data message strictly to the J1587 standard which defines how every message is presented.

Using the example messages, there are different types of parameter and data. The first byte is always "80" to define the engine so ignore that, the rest the can vary as follows.

80 5C 66 BE E0 2E 5C 66
80 B7 40 06 5C 64 BE 30 2F
80 FF 62 A2 B7 1D 17 E4 52 52

(The first byte immediately follows engine ID)

  1. If the parameter byte is 127 or less, then it's a single byte variable. In the first message 5C is a single byte variable whose value is 66. If the parameter byte is 128 or more then it's data is a double byte variable. "BE" is a double byte variable whose data is 2EE0 (least significant followed by most significant)
  2. If the first parameter of the message is 192, then it's a complex multi-byte message for engine diagnostics that I'm not interested in, so ignore the rest of that message.
  3. If the first parameter of the message is 255 (FF) then this identifies that the rest of the message contains parameters from 2nd "page" of possible J1587 parameters (256-512), other than the extra FF at the beginning of the line the rest of the rules apply.
  4. The message can be of any length between 3 and 19 bytes.

I'm only looking for 3 pieces of data i) 92 is engine load (single byte), ii) 190 is engine rpm (double byte) and iii) 439 is turbo boost pressure (double byte and page 2). So my final code has to process the messages to find 1 example from each of the possible types.

I've not tried this in anger yet, I've got a second arduino that is just outputting a constant stream of messages to test my project on a second arduino. Trying it on the engine will be in a week or two's time when I'm next with it, that's when I find out it's all wrong!.

Tim

J1587_decoder_rev_1.1.ino (4.31 KB)