There may be other ways to do this, but I use the stuff in
#include <termios.h>
For example for my code that reads data from the Arbotix Commander (XBee), some of the Init code looks like:
bool Commander::begin(char *pszDevice, speed_t baud) { int err; // Create our lock to make sure we can do stuff safely if (pthread_mutex_init(&lock, NULL) != 0) return false; _fCancel = false; // Flag to let our thread(s) know to abort. #ifdef CMDR_USE_XBEE _pszDevice = pszDevice; _baud = baud; // Lets do our init of the xbee here. // We will do all of the stuff to intialize the serial port plus we will spawn off our thread. struct termios tc, tcNew; if ((fdXBee = open(_pszDevice, O_RDWR | O_NONBLOCK)) == -1) { printf("Open Failed\n"); return 0; } if (tcgetattr(fdXBee, &tc)) { perror("tcgetattr()"); return 0; } // build new version bzero(&tcNew, sizeof(tcNew)); //newtios.c_cflag |= _dwBaudRate | CRTSCTS | CS8 | CLOCAL | CREAD; tcNew.c_cflag = tc.c_cflag; tcNew.c_cflag &= ~CBAUD; tcNew.c_cflag |= _baud; tcNew.c_iflag = IGNPAR; tcNew.c_oflag = 0; /* set input mode (non-canonical, no echo,...) */ tcNew.c_lflag = 0; tcNew.c_cc[VTIME] = 0; /* inter-character timer unused */ tcNew.c_cc[VMIN] = 1; /* blocking read until 1 chars received */ tcflush(fdXBee, TCIFLUSH); tcsetattr(fdXBee, TCSANOW, &tcNew); // Now we need to create our thread for doing the reading from the Xbee pthread_barrier_init(&_barrier, 0, 2); err = pthread_create(&tidXBee, NULL, &XBeeThreadProc, this); if (err != 0) return false; // sync startup pthread_barrier_wait(&_barrier);
I have another version of similar code in my WrapperSerial file, but the above is modeled to be very much like how the Arduino IDE does it. this code creates a second thread to read in the data...
If you wish to look at the full code of these files, they are up in my (KurtE) Raspberry_Pi project up on github in the library directory.
Hope that helps
Kurt