c++ - tcsetaddr and learning to program serial ports -


i trying learn how use serial ports using c/c++ (mostly c++) in xubuntu linux 12.10. found couple helpful tutorials but, case in tutorials, text canned , doesn't address lot of "gotchas". trying simple: open serial port (sent in command-line parameter), initial port settings , print them out, initialize port new settings.

here have far:

#include <termios.h> #include <iostream> #include <string> #include <cstdlib> #include <unistd.h> #include <fcntl.h> #include <cstdio> using namespace std;  int main(int count, char* parms[]) {     if (count != 2)         exit(1);      //open port     string fname = parms[1];     cout << fname << ": ";     int fd = open(fname.c_str(), o_rdwr | o_noctty | o_ndelay   );     if (!fd)     {         cout << "error" << endl;         exit(1);         }      termios getportsettings, setportsettings;      //get current port settings     tcgetattr(fd,&setportsettings);     getportsettings = setportsettings;     speed_t ibaud = cfgetispeed(&getportsettings),         obaud = cfgetospeed(&getportsettings);      cout << "ibaud: " << ibaud << " obaud: " << obaud << endl;     cout << "\npress enter change port status";     cin.get();      //set baud rate 19200     cfsetispeed(&setportsettings,b19200);     cfsetospeed(&setportsettings,b19200);      //set no parity     setportsettings.c_cflag &= ~parenb;      //one stop bit     setportsettings.c_cflag &= ~cstopb;      //clear out current word size     setportsettings.c_cflag &= ~csize;      //set 8 data bits     setportsettings.c_cflag |= cs8;           //apply settings     int ret = tcsetattr(fd,tcsaflush,&setportsettings);     if (ret < 0)     {         cout << "error ("<< ret <<") in applying settings!\n";         exit(1);     };      tcgetattr(fd,&setportsettings);     ibaud = cfgetispeed(&getportsettings);     obaud = cfgetospeed(&getportsettings);      cout << "ibaud: " << ibaud << " obaud: " << obaud << endl;      //close port     close(fd);     return 0; } 

when test, testing out serial ports /dev/ttys0 though /dev/ttys31 (it shouldn't matter choose here right?). able open every interface , settings them (i want find out how extract other settings, baud rates low hanging fruits , need ease myself bitwise math needed extract else).

basically, every time make call tcgetattr(), fails generic -1 return code. eventually, want try hook 2 of computers , send data between 2 using simple null modem cable or usb cable (the code same, difference device file right?), , first step in doing so.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -