Go Down

Topic: (New)SoftwareSerial parity bit? (Read 9642 times) previous topic - next topic



I try to use the SoftwareSerial or the NewSoftwareSerial libraries to communicate with a device which only accepts: 8bits of data, 1 bit of even parity and 2 stop bits.

I understood that (New)SoftwareSerial only works with 8 data bits and 1 start/stop bit.

Any way to modify (New)SoftwareSerial to have parity bits and more than one stop bit?


Feb 05, 2011, 08:48 pm Last Edit: Feb 05, 2011, 08:54 pm by robtillaart Reason: 1
First question: what device are you talking about?

You can adapt the library to receive and send the extra bits:
First overload the begin() method so the number of stopbits and parity can be set (store these values in an internal var);

Code: [Select]
void NewSoftSerial::begin(long speed, byte stopbits=1, char parity = 'N')  // this makes 1 stopbit and None Parity the default
 _stopbits = stopbits;
 _parity = parity;

Then change : void NewSoftSerial::recv()  so that depending on the number of stopbits set it reads 0, 1 or 2 stopbits AND handle parity

Search for :
   // skip the stop bit
   DebugPulse(_DEBUG_PIN2, 1);

this becomes something like:
Code: [Select]

  // handle parity bits
   if (_parity != 'N')
     tunedDelay(_rx_delay_stopbit);  // better read and check the parity bit :)
   // skip the stop bits
   for (int i=0; i< _stopbits; i++) tunedDelay(_rx_delay_stopbit);

   DebugPulse(_DEBUG_PIN2, 1);

Then change: void NewSoftSerial::write(uint8_t b)   so that it handles stopbits and parity correctly. This is more complex as you must first calculate the value of the paritybit and send it directly after sending the 8 databits. Take care of the inverse flag !  Same for the stopbits. Left as an exercise for the reader ;)

succes Rob
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up