Pages: [1]   Go Down
Author Topic: (New)SoftwareSerial parity bit?  (Read 4739 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
    tunedDelay(_rx_delay_stopbit);
    DebugPulse(_DEBUG_PIN2, 1);

this becomes something like:
Code:
  // 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 smiley-wink

succes Rob
« Last Edit: February 05, 2011, 02:54:17 pm by robtillaart » Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: