On Thursday 27 January 2005 07:37, Jose Thadeu Cavalcante wrote:
Hi,
How can I access serial port by programming? The serial ports may be accessed by using the device special file, /dev/ttyS[01]. Also look at the termios(3) man page. The key to serial port programming is to first set the speed, parity, echo et. al. to what you want. Also make sure that there is not a getty process running on that port.
Several years ago, I was doing a satellite communications software via the serial port, and to test it, I set up a loop back on ports 0 and 1. (This was complicated because it was on a 64 bit Alpha where the 2 serial ports used a single 25 pin connector, so I had to hand craft my null modem. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Thursday 27 January 2005 14:37, Jose Thadeu Cavalcante wrote:
Hi,
How can I access serial port by programming?
Thanks Thadeu
IF in C use the fopen statement on the device ttyS0 viz: FILE *fd = fopen("/dev/ttyS0","rw"); to open for both reading and writing. ttyS0 corresponds to the first serial port, ttyS1 to the second etcetera . If you are using a third party serial hardware then the device name may change to something else e.g. a cyclades board will have device names ttyC01 and up. Lastly you must check the permissions on the device by typing : ls -l /dev/ttyS0 There are lots of other issues - see the setserial command for example and ignore any references to the deprecated devices /dev/cua? See also 'man termios' for some system calls that control serial port attributes. Oh and also look at the stty command. Look at the file /proc/tty/driver/serial to see what the kernel thinks about the serial port in question. Regards Paul -- Paul Hewlett (Linux #359543) Email:`echo az.oc.evitcaten@ttelweh | rev` Tel: +27 21 852 8812 Cel: +27 72 719 2725 Fax: +27 86 672 0563 --
On Thursday 27 January 2005 08:34, Paul Hewlett wrote:
On Thursday 27 January 2005 14:37, Jose Thadeu Cavalcante wrote:
IF in C use the fopen statement on the device ttyS0 viz:
FILE *fd = fopen("/dev/ttyS0","rw"); You don't really want to use C stdio for a serial device per se. The open(2) system call is better. int fd; fd = open("/dev/ttyS0", O_NOTTY); The you use the termios calls to set the baudrate, et. al. struct termios tios; /* termios structure */ rv = tcgetattr(fd, &tios); /* get current attributes to restore later */
Remember, that the terminal attributes are probably not going to be what you want on that device. I mentioned a loop back earlier, but another test is to use a modem. The modem responds to some ASCII commands. You could write a little program to set the tty to noecho, and send a modem command to the modem. The modem should respond. Also note that unlike a block device, the terminal sends and receives a stream of data. When you read, you may get less data than you plan to in a single read. -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
participants (3)
-
Jerry Feldman
-
Jose Thadeu Cavalcante
-
Paul Hewlett