Skip to content

Commit 5e4af6c

Browse files
committed
Add support for Parity Bit
Now also Parity Bits like ODD and EVEN can be enforced.
1 parent 1891cce commit 5e4af6c

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

rs232-linux.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ const char * comGetPortName(int index) {
138138
}
139139

140140
/*****************************************************************************/
141-
int comOpen(int index, int baudrate)
141+
int comOpen(int index, int baudrate_and_parity)
142142
{
143143
if (index >= noDevices || index < 0)
144144
return 0;
@@ -161,7 +161,15 @@ int comOpen(int index, int baudrate)
161161
config.c_cflag &= ~(PARENB | PARODD | CSTOPB | CSIZE | CRTSCTS);
162162
config.c_cflag |= CLOCAL | CREAD | CS8;
163163
config.c_lflag &= ~(ICANON | ISIG | ECHO);
164-
int flag = _BaudFlag(baudrate);
164+
switch (baudrate_and_parity & PARITY_BITMASK)
165+
{
166+
case PARITY_NONE: break;
167+
case PARITY_ODD: config.c_cflag |= PARENB|PARODD; break;
168+
case PARITY_EVEN: config.c_cflag |= PARENB; break;
169+
case PARITY_SPACE: config.c_cflag |= PARENB|CMSPAR; break;
170+
case PARITY_MARK: config.c_cflag |= PARENB|CMSPAR|PARODD; break;
171+
}
172+
int flag = _BaudFlag(baudrate_and_parity & BAUDRATE_BITMASK);
165173
cfsetospeed(&config, flag);
166174
cfsetispeed(&config, flag);
167175
// Timeouts configuration

rs232-win.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,18 @@ const char * comGetInternalName(int index)
203203
}
204204

205205
/*****************************************************************************/
206-
int comOpen(int index, int baudrate)
206+
int comOpen(int index, int baudrate_and_parity)
207207
{
208208
DCB config;
209209
COMMTIMEOUTS timeouts;
210-
if (index < 0 || index >= noDevices)
210+
if (index < 0 || index >= noDevices)
211211
return 0;
212212
// Close if already open
213213
COMDevice * com = &comDevices[index];
214214
if (com->handle) comClose(index);
215215
// Open COM port
216216
void * handle = CreateFileA(comGetInternalName(index), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
217-
if (handle == INVALID_HANDLE_VALUE)
217+
if (handle == INVALID_HANDLE_VALUE)
218218
return 0;
219219
com->handle = handle;
220220
// Prepare read / write timeouts
@@ -227,14 +227,21 @@ int comOpen(int index, int baudrate)
227227
SetCommTimeouts(handle, &timeouts);
228228
// Prepare serial communication format
229229
GetCommState(handle, &config);
230-
config.BaudRate = baudrate;
231-
config.fBinary = true;
232-
config.fParity = 0;
230+
config.BaudRate = baudrate_and_parity & BAUDRATE_BITMASK;
231+
config.fBinary = 1;
232+
config.fParity = 1;
233233
config.fErrorChar = 0;
234234
config.fNull = 0;
235235
config.fAbortOnError = 0;
236236
config.ByteSize = 8;
237-
config.Parity = 0;
237+
switch (baudrate_and_parity & PARITY_BITMASK)
238+
{
239+
case PARITY_NONE: config.Parity = 0; break;
240+
case PARITY_ODD: config.Parity = 1; break;
241+
case PARITY_EVEN: config.Parity = 2; break;
242+
case PARITY_SPACE: config.Parity = 3; break;
243+
case PARITY_MARK: config.Parity = 4; break;
244+
}
238245
config.StopBits = 0;
239246
config.EvtChar = '\n';
240247
// Set the port state

rs232.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,16 @@ extern "C" {
5757
* Website: www.fredslab.net <br>
5858
* Twitter: \@marzacdev <br>
5959
*/
60-
60+
61+
#define PARITY_NONE 0x00000000 //see comOpen()
62+
#define PARITY_EVEN 0x10000000 //see comOpen()
63+
#define PARITY_ODD 0x20000000 //see comOpen()
64+
#define PARITY_SPACE 0x30000000 //see comOpen()
65+
#define PARITY_MARK 0x40000000 //see comOpen()
66+
67+
#define PARITY_BITMASK 0xF0000000
68+
#define BAUDRATE_BITMASK 0x0FFFFFFF
69+
6170
/*****************************************************************************/
6271
/**
6372
* \fn int comEnumerate()
@@ -76,31 +85,31 @@ extern "C" {
7685
/**
7786
* \fn int comTerminate()
7887
* \brief Release ports and memory resources used by the library
79-
*/
88+
*/
8089
void comTerminate();
8190

8291
/**
8392
* \fn const char * comGetPortName(int index)
8493
* \brief Get port user-friendly name
8594
* \param[in] index port index
8695
* \return null terminated port name
87-
*/
96+
*/
8897
const char * comGetPortName(int index);
8998

9099
/**
91100
* \fn const char * comGetInternalName(int index)
92101
* \brief Get port operating-system name
93102
* \param[in] index port index
94103
* \return null terminated port name
95-
*/
104+
*/
96105
const char * comGetInternalName(int index);
97106

98107
/**
99108
* \fn int comFindPort(const char * name)
100109
* \brief Try to find a port given its user-friendly name
101110
* \param[in] name port name (case sensitive)
102111
* \return index of found port or -1 if not enumerated
103-
*/
112+
*/
104113
int comFindPort(const char * name);
105114

106115
/*****************************************************************************/
@@ -109,10 +118,13 @@ extern "C" {
109118
* \brief Try to open a port at a specific baudrate
110119
* \brief (No parity, single stop bit, no hardware flow control)
111120
* \param[in] index port index
112-
* \param[in] baudrate port baudrate
121+
* \param[in] baudrate port baudrate plus parity (see PARITY_*).
122+
* i.E. 9600|PARITY_EVEN.
123+
* Optionally also only a baudrate may be specified. In this
124+
* case Parity is PARITY_NONE
113125
* \return 1 if opened, 0 if not available
114-
*/
115-
int comOpen(int index, int baudrate);
126+
*/
127+
int comOpen(int index, int baudrate_and_parity);
116128

117129
/**
118130
* \fn void comClose(int index)

0 commit comments

Comments
 (0)