In a previous post I had posted code that transmitted a message through an infrared link between two Arduinos. The main issue with this code is that if the infrared beam is intermittently blocked during the transmission, e.g. by waving your fingers in front of the IR LEDs, bits will be dropped and the message received won’t match the message sent. In this post I describe a simple protocol that provides error detection of the received message. In my weather station application, the communications is only one-way and so there is no mechanism for the receiver to re-request a garbled message; the message is sent periodically with updated temperature and barometric pressure data.
The message is put in a simple “envelope”. The format of the packet sent is as follows:
- STX,ETXにデータは囲まれてはいますがデータは垂れ流し状態なのでスタート部分が不明です。 キャンセル 完了する fuzzball. 2019/04/11 19:51 編集.
- The function receiveMessage reads from the serial port, discarding all characters until it receives a STX. Once the STX is received it reads characters, adding them to the message buffer and calculating the check-sum. If an ETX is received, it assumes the next character is the check-sum.
- The BCC is obtained by first ignoring the STX, BCC and ETX codes from the string and summing the remaining hexadecimal ASCII codes (aside from the STX, BCC and ETX characters, the code is 12 characters long). Next, take the 2’s complement of the result. Finally, the least significant byte of the 2’s complement is used as the BCC.
<STX><message buffer><ETX><chksum>
where:
Arduino Serial Read Stx Etx
- <STX> is the “start of transmission” marker and is the character 0x02.
- <message buffer> is the text message we want to send.
- <ETX> is the “end of transmission” marker and is the character 0x03.
- <chksum> is a single byte check-sum of all of the characters in the <message buffer>. You can see in the code that this check-sum is calculated by looping through the message buffer and adding the values of the individual characters.
Re: Detect STX and ETX hex in received string by jagexCoder (Novice) on Nov 29, 2012 at 01:58 UTC: thanks for the assistance guys, I would like to further add that the packets (the payload, not STX/ETX) that are coming in are coming as a 'String of characters' in ASCII hex format. So the STX will be sent as a '2' and ETX will be sent as a '3'.
A check-sum is not going to completely guarantee error detection but it is simpler to implement than a Cyclic Redundancy Check (CRC) check, and is sufficient for my application.
Below is a sample program that transmits “Hello” over the IR link. The function sendMessagePacketconstructs the packet:
The code to read from the serial port and implement error checking is in the function readMessage in the program SerialReaderProtocol shown below: Plants vs zombies heroes free gems.
The function receiveMessagereads from the serial port, discarding all characters until it receives a STX. Once the STX is received it reads characters, adding them to the message buffer and calculating the check-sum. If an ETX is received, it assumes the next character is the check-sum. Once the check-sum is received, it compares it with the calculated check-sum; if the two check-sums do not match, the message is discarded.
I am using these functions to send text data only and so I am not expecting the bytes that represent STX (0x02) and ETX (0x03) to be in the message buffer. Note that once the message is received, it is null-terminated.
Arduino Serial Stx Etx Manual
Arduino Serial Stx Etx Pro
Note on compiling the code above: WordPress won’t render the null character in the HTML code listing above and so you need to replace NULL in the code listing with the <single quote><backslash>0<single quote>. I’ll see if I can fix the rendering issue.