QNX RTOS v4 Knowledge Base
QNX RTOS v4 Knowledge Base
Title |
Detecting any parity and framing errors on a RS-232 serial line |
Ref. No. |
QNX.000009289 |
Category(ies) |
Development, Character I/O |
Issue |
We have a simple application that collects data from a robot across an RS-232C connection. In the application, we'd like to detect any parity and framing errors that may occur on the serial line. How do we do this?
|
Solution |
Parity and framing errors can be detected by using the POSIX terminal I/O settings. Here's an example of how to setup the termios structure on the serial port so that an application will catch these errors:
#include <string.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <termios.h> #include <sys/dev.h> #include <sys/uio.h>
#define SERDEV1 "/dev/ser1" #define BAUD 9600
int open_device (char *device); void serial_device_init(int file_des); void read_device (int file_des);
main() { x09int file_des;
x09file_des = open_device (SERDEV1); x09serial_device_init (file_des); x09for(;;) x09x09read_device (file_des); }
int open_device (char *device) { x09int file_des; x09struct _dev_info_entry info;
x09if ((file_des = open(device, O_RDWR)) = -1{ x09x09printf("error opening %s: %sn", x09x09x09device, strerror(errno)); x09x09exit(1); x09} x09if(dev_info(file_des, &info) != -1) x09x09printf("NODE %d, TTY %dn", info.nid, info.tty); x09else x09x09printf("dev_info failedn",strerror(errno)); x09return(file_des); } void serial_device_init(int file_des) { x09int i; x09struct termios termios_p; x09speed_t speed = BAUD;
// get the control structure for /dev/ser1
x09tcgetattr(file_des, &termios_p); x09/* get control structure for /dev/ser1 */
// Set the baud (will he set with tcsetattr())
x09cfsetispeed(&termios_p, speed); // input x09cfsetospeed(&termios_p, speed); // output
// Control modes: // // CS8----> 8 bits-per-byte // CREAD--> Enable receiver. If this bit is not set, //x09no characters are received. // PARENB-> Parity generation and detection is enabled and //x09parity bit is added to each character.
x09termios_p.c_cflag &= ~CSIZE; x09termios_p.c_cflag |= CS8|CREAD|PARENB;
// Input modes: // // INPCK --> Enables parity checking // IGNPAR -> Parity and framing errors are ignored // PARMRK -> If PARMARK is set, and IGNPAR is not set, //x09a byte with a framing or parity error will //x09be read by the application as the following //x09three-byte sequence: // //x09x09Byte 1 ' |
|