Go Down

Topic: Extracting Data from SoftwareSerial (Read 1 time) previous topic - next topic

Nick Gammon

I wrote a library for sending serial data with a prefix and suffix, including a sumcheck:

http://www.gammon.com.au/forum/?id=11428

The struct idea is sound enough, but you should use "sizeof" to let the compiler work out the length, rather than doing it yourself.
http://www.gammon.com.au/electronics

Robin2

Nick, I think it's a little more robust than your comment implies.

If the initial C is not detected but one of the Vals "accidentally" contains a C it would ALSO be necessary for an F to "accidentally" exist after the end of the proper data in a position so that it is the right number of characters after the "incorrect" C.

It would be an easy matter to include a CRC character at the end of the data (before the F) as an additional safeguard.

...R


Since 'C' is 67 in ASCII, let's hope val1, val2, val3 or val4 never contain the number 67. And don't get me started on the 'F' part. :)

Nick Gammon

Let's say you start listening in the middle of the data stream. The "wrong" 'C' arrives and you then read 24 bytes and don't get an 'F'. So you read again. Meanwhile the other end has started sending again (quite likely since you might have only received 10 bytes before the "real" 'F'). Since the other end is retransmitting you get the wrong 'C' again, and so on. Nothing would really break you out of this loop.

A simple solution would be to send in ASCII and not binary, take the slight performance hit, and then put non-numeric characters at each end, eg.

Code: [Select]

<123,456,789,22,33,44,55,66,77,88,99>


If the numbers are small, something like "77," is actually less bytes than a long, anyway.
http://www.gammon.com.au/electronics

Robin2

Nick, Thanks again for your insightful comments.

I should have said that the system I'm developing only allows the PC to send bytes when it is requested to by the Arduino so my sketch won't face a continuous flow of uncoordinated data. And whether the data is good or bad the Arduino dumps all characters after the specified quantity so the receive buffer is empty ready for the next PC transmission.

Which all goes to demonstrate that the logic of sending data is complex even if the code to implement it isn't very long.

By sending Longs and Ints to it the code on the Arduino is very much simpler and there is no time cost for converting ascii numbers to binary. And 857333 takes 6 bytes as ascii but only 4 as a Long. And writing and testing PC code in JRuby is much easier than writing and testing Arduino code.

...R

Nick Gammon


By sending Longs and Ints to it the code on the Arduino is very much simpler and there is no time cost for converting ascii numbers to binary.


Better get the endian-ness right then. It might be by default, and then again it might not.
http://www.gammon.com.au/electronics

Robin2

I was aware of endian-ness and the first way I tried worked :)

...R

Go Up