User Tools

Site Tools


xnorit:public:dependencies:xnorserial

This is an old revision of the document!


XnorSerial Communication Library:

Embedded devices library

Instantiate 'XnorSerial'object

  1. Include the library globally.
  2. Instantiate byte array variable. This is used as a shared memory for communication. Size of array is free to choose.
  3. 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 Serial-number accordingly! XS.SerialEventProc(); can also be called inside a loop when interrupt based serial events are not supported. 
 
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 ! <case sensitive> </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 embedded device the host has to send an SOH-byte followed by 2 protocol bytes. Those 2 bytes configure the start index and amount of data we request from the embedded device'shared' byte array.

 Read requests

  • First byte:
    The SOH-byte, can be send in conjunction with other data-bytes or as separate byte.
  • Second 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'.
  • Third byte:
    Determines the amount of bytes we want to read.
  • For example:
    When the second byte is set to '01' and we read '02' bytes from the array. We read the values at index 1 & 2.
    When the second byte is set to '00' and we read '03' bytes from the array. We read the values at index 0, 1 & 2.
  • Image explanation:
  • Purple: Request starting from byte 0, request 6 bytes in total. '06' is the ACK byte, '04' the EOT byte send by the embedded hardware.
  • Orange: Request starting from byte 0, request 3 bytes in total.
  • Red: Request starting from byte 3, request 3 bytes in total.





Write request:

 Write request

To write data to the embedded device, the host has to send an SOH-byte before sending 2 protocol bytes.
The last protocol byte is then followed by data bytes. The first 2 bytes keep the same logic as in read mode, following bytes are written to the 'shared' byte array.

  • First byte:
    The SOH-byte, can be send in conjunction with other data-bytes or as separate byte.
  • Second 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'.
  • Third byte:
    Determines the amount of bytes we want to write.
  • For example:
    When the second byte is set to '01' and we write '02' bytes to the array. We write the values at index 1 & 2.
    When the second byte is set to '00' and we write '03' bytes to the array. We write the values at index 0, 1 & 2.
  • Image explanation:
  • Orange: Requesting 5 bytes from the embedded device's bus_memory starting at index 10.
  • Purple: Writing 5 bytes to the embedded device's bus_memory starting at index 10. Writing bytes values 1,2,3,4,5
  • Note how before writing the data in-between the ACK and EOT bytes are all equal to zero, while after writing they represent the same 1,2,3,4,5 values that we wrote to them. Additionally you can also note how a write action does not return data, except for the ACK reply and EOT byte automatically send by the embedded device.


xnorit/public/dependencies/xnorserial.1617700335.txt.gz · Last modified: (external edit)