Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • So I need to add Serial.write('ab') and then write the number bits. And in matlab fread 2+number-bits and check if the first 2 are ab. If not, re-read. Is this the thing you suggest? What do you mean by collision with my data.. If I just read 1+num-bits, and they shift, I would still get an error if not all bits are there, or the first one is not "a"? Commented Jun 24, 2014 at 11:04
  • By collision I mean that if your data happened to be the characters 'ab' it would mess up a reading. Your correct about the arduino side, just send "ab" before the data. The matlab code should detect that the sequence "ab" happened and then look for the data bits next, but if another "ab" happens before the data bits are done dump what has already come in and start over. That way it will realign with the data if a byte is dropped. Commented Jun 24, 2014 at 17:37
  • Collision you mean the probability, that in my actual values one of the bytes contains the header by incidence? So a longer header would reduce the possibility of having real data = termination string. Correct? Commented Jun 24, 2014 at 17:57
  • Exactly. With two bytes there is a 1 in 2^16 probability of random data generating the header. A longer header than 2 bytes that might result in less data making it across since your packets are so small; Sending an extra character per packet would probably cost more time than losing one in 65536 packets. Commented Jun 24, 2014 at 18:06
  • Ok fine. I would implement this as Serial.write(219); Serial.write(128); Since what is sent is binary (thus the same as some char), and for matlab it is easier to directly read (&compare) the uint with 8 bit. I mark this as solution, since I can remove the shifts. The whole thing is useless however, since there is some strange time delay that adds up. See: stackoverflow.com/questions/24368670/… Commented Jun 24, 2014 at 20:58