|
Prev: Need help installing Paravirtualized KVM CentOS5 VM on Fedora 9
Next: Serial ports ttyUSB{0..9}
From: Guillaume Dargaud on 18 Jun 2008 13:23 Hello all, I want to read/write on a serial port from a user program. I found this helpfull HowTo: http://tldp.org/HOWTO/IO-Port-Programming-2.html But from reading the first lines in it, it seems that it applies to kernel programming, not userland. Is there a different set of calls to use for usermode programs ? -- Guillaume Dargaud http://www.gdargaud.net/
From: John Taylor on 18 Jun 2008 13:27 On Wed, 18 Jun 2008 19:23:28 +0200, Guillaume Dargaud wrote: > Hello all, > I want to read/write on a serial port from a user program. I found this > helpfull HowTo: > http://tldp.org/HOWTO/IO-Port-Programming-2.html But from reading the > first lines in it, it seems that it applies to kernel programming, not > userland. > > Is there a different set of calls to use for usermode programs ? Section 2.2 appears to be a usermode version. It seems that you just open /dev/port
From: Guillaume Dargaud on 18 Jun 2008 16:55 >> Is there a different set of calls to use for usermode programs ? > Section 2.2 appears to be a usermode version. > It seems that you just open /dev/port Correct. I gave up too quickly ! But how do you set baud rate, stop bits and the like ? -- Guillaume Dargaud http://www.gdargaud.net/
From: Robert Heller on 18 Jun 2008 19:00 At Wed, 18 Jun 2008 22:55:29 +0200 "Guillaume Dargaud" <USE_MY_WEB_FORM(a)gdargaud.net> wrote: > > >> Is there a different set of calls to use for usermode programs ? > > > Section 2.2 appears to be a usermode version. > > It seems that you just open /dev/port > > Correct. I gave up too quickly ! > But how do you set baud rate, stop bits and the like ? man termios -- Robert Heller -- Get the Deepwoods Software FireFox Toolbar! Deepwoods Software -- Linux Installation and Administration http://www.deepsoft.com/ -- Web Hosting, with CGI and Database heller(a)deepsoft.com -- Contract Programming: C/C++, Tcl/Tk
From: Bob Hauck on 18 Jun 2008 19:15 On Wed, 18 Jun 2008 22:55:29 +0200, Guillaume Dargaud <USE_MY_WEB_FORM(a)gdargaud.net> wrote: >>> Is there a different set of calls to use for usermode programs ? > >> Section 2.2 appears to be a usermode version. >> It seems that you just open /dev/port > > Correct. I gave up too quickly ! > But how do you set baud rate, stop bits and the like ? See "man termios". Read especially about tcgetattr() and tcsetattr(). This code sets the serial port to raw mode (no special characters, no terminal handling, no flow control) and configures the specified speed, data bits, parity, and stop bits. It seems to work well enough for what I use it for. It also appears to work on Solaris but hasn't been tested very much there. #define BSD_COMP /* makes solaris happy */ #include <ctype.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <sys/ioctl.h> #include <termios.h> #include <unistd.h> /* lots of stuff deleted */ /***************************************************************************** * Configure a serial port. Some parameters may be limited by the * capabilities of the hardware. *****************************************************************************/ int ser_set_attr (int fd, long baud, int data, int parity, int stop) { int e_baud = B9600; int e_data = CS8; int e_parity = 0; int e_stop = 0; struct termios options; /* get the current options */ if (tcgetattr (fd, &options)) { int err = errno; fprintf (stderr, "tcgetattr failed: %d\n", err); } /* Set up the basic raw I/O options. */ options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); options.c_oflag &= ~OPOST; options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); options.c_cflag &= ~(CSIZE|PARENB); options.c_cflag |= CS8; options.c_cc [VMIN] = 1; options.c_cc [VTIME] = 0; switch (baud) { case 0: e_baud = B0; break; case 50: e_baud = B50; break; case 75: e_baud = B75; break; case 110: e_baud = B110; break; case 134: e_baud = B134; break; case 150: e_baud = B150; break; case 200: e_baud = B200; break; case 300: e_baud = B300; break; case 600: e_baud = B600; break; case 1200: e_baud = B1200; break; case 1800: e_baud = B1800; break; case 2400: e_baud = B2400; break; case 4800: e_baud = B4800; break; case 9600: e_baud = B9600; break; case 19200: e_baud = B19200; break; case 38400: e_baud = B38400; break; case 57600: e_baud = B57600; break; case 115200: e_baud = B115200; break; case 230400: e_baud = B230400; break; case 460800: e_baud = B460800; break; #if defined (__linux__) case 500000: e_baud = B500000; break; case 576000: e_baud = B576000; break; case 921600: e_baud = B921600; break; case 1000000: e_baud = B1000000; break; case 1152000: e_baud = B1152000; break; case 1500000: e_baud = B1500000; break; case 2000000: e_baud = B2000000; break; case 2500000: e_baud = B2500000; break; case 3000000: e_baud = B3000000; break; case 3500000: e_baud = B3500000; break; case 4000000: e_baud = B4000000; break; #endif default: e_baud = B0; break; } /* set the baud rate */ cfsetispeed (&options, e_baud); cfsetospeed (&options, e_baud); switch (data) { case 5: e_data = CS5; break; case 6: e_data = CS6; break; case 7: e_data = CS7; break; case 8: e_data = CS8; break; default: e_data = CS8; break; } /* set the data bits */ options.c_cflag &= ~CSIZE; /* Clear the size bits */ options.c_cflag |= e_data; /* Select data bits */ switch (tolower (parity)) { case 'n': e_parity = 0; break; case 'o': e_parity = PARODD | PARENB; break; case 'e': e_parity = PARENB; break; default: e_parity = 0; break; } /* set the parity */ options.c_cflag &= ~(PARODD | PARENB); /* Clear the parity bits */ options.c_cflag |= e_parity; /* Select parity bits */ switch (stop) { case 1: e_stop = 0; break; case 2: e_stop = CSTOPB; break; default: e_stop = 0; break; } /* set the stop bit */ options.c_cflag &= ~CSTOPB; /* Clear the stop bit */ options.c_cflag |= e_stop; /* Select stop bit */ /* enbable the receiver and set to local mode */ options.c_cflag |= (CLOCAL | CREAD); /* Disable input flow control */ options.c_iflag &= ~(IXON | IXOFF | IXANY); /* Disable output flow control */ options.c_oflag &= ~OPOST; return tcsetattr (fd, TCSANOW, &options); } -- -| Bob Hauck -| http://www.haucks.org/
|
Next
|
Last
Pages: 1 2 Prev: Need help installing Paravirtualized KVM CentOS5 VM on Fedora 9 Next: Serial ports ttyUSB{0..9} |