This is an old revision of the document!
Table of Contents
XnorSerial Communication Library:
Embedded devices library
Instantiate 'XnorSerial'object
- Include the library globally.
- Instantiate byte array variable. This is used as a shared memory for communication. Size of array is free to choose.
- Instantiate XnorSerial object, passing the HardwareSerial port by reference, followed by the previously instantiated byte array.
#include "XnorSerial.h" byte bus_memory[4]; XnorSerial XS(&Serial1, bus_memory); void setup() { Serial1.begin(9600); } void serialEvent1(){XS.SerialEventProc();} // Event handler for Serial1, edit number accordingly! XS.SerialEventProc(); can also be called inside a loop. void loop(){...}
Example code above creates an bus_memory byte array that is 4 bytes long. All data stored inside this array is (read/write) accessible trough the Serial1 port.
Bugs:
None found up to this point.
Downloads
- Download of embedded device code: xnorserial.h <color #edb21c>Warning: rename file to XnorSerial.h !</color>
Host device
Protocol:
The serial host device (or master) has to send data following the correct protocol to read/write data from the embedded device. The data to send exists out of minimum 2 bytes, for a read request. More than 2 bytes defaults to a write request. Each communication must start with a SOH ascii byte (0x01), this byte can be send in conjunction with additional byte-data or as a separated byte. After sending the SOH-byte the embedded master device pauses all other operation for 2 seconds, if no data is received within those 2 seconds the communication will timeout and normal master operation will continue. After receiving an SOH-byte the embedded device will reply an ACK ascii byte (0x06), to initiate communication has started. After completing communication the embedded device will end communication with an EOT ascii byte (0x04).
Read request:
To read data from the slave device the master has to send 2 bytes. Those 2 bytes configure the start index and amount of data we request from the slaves 'shared' byte array.
- First byte:
Determines the start index.
When we want to read from the first index from the array we use '0',
When we want to read from the last index in an byteArray of size 4, we use '3'. - Second byte:
Determines the amount of bytes we want to read.
- For example:
When the first byte is set to '01' and we read '02' bytes from the array. We read the values at index 1 & 2.
When the first byte is set to '00' and we read '03' bytes from the array. We read the values at index 0, 1 & 2.
Write request:
To write data to the slave device, the master just has to send more than 2 bytes. The first 2 bytes keep the same logic as in read mode, following bytes are written to the 'shared' byte array.
- First byte:
Determines the start index.
When we want to write to the first index from the array we use '0',
When we want to write to the last index in an byteArray of size 4, we use '3'. - Second byte:
Determines the amount of bytes we want to write.
- For example:
When the first byte is set to '01' and we write '02' bytes to the array. We write the values at index 1 & 2.
When the first byte is set to '00' and we write '03' bytes to the array. We write the values at index 0, 1 & 2.
