Mail Archives: cygwin/1998/12/17/01:35:10
Here's kind of a stupid question: How do I open a serial port (COM1)
using Cygwin-b20.1? I've tried a number of things, but nothing seems to
work. First a little background: the end result is I am trying to port
a program that uses select() to poll the keyboard and a COM port until
one of the two has input. After reading the archives, I discovered that
select() and serial ports were broken, so I just downloaded the new dll
and replaced the old one. Here's what I've tried:
set CYGWIN=notty (actually, just didn't set CYGWIN).
I opened "COM1" using open() and tried to read() from it after
tcsetattr(). Also tried using select()--it could read the keyboard
fine, but not the COM port.
set CYGWIN=tty
Opened "/dev/tty00" the same way as above. I discovered that this seems
to be the keyboard device. I tried ttyS0 and cua0, but these don't
exist. And of course, "ls /dev" shows nothing.
Can anyone tell me what I'm doing wrong? Last week I also tried notty,
and opened COM1 using fopen(). When connected to another computer with
a null modem cable, I was able to receive characters using Cgywin-b20.0.
I have searched the archives for almost 2 hours and read many messages,
but haven't found answers to my questions yet.
By the way, here's the code I'm playing with:
-------------------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#define BUFFERSIZ 30
void main( void ) {
int i, j,
idBarc,
iStat,
iRet ;
fd_set rfds ;
struct timeval tv ;
struct termios termios ;
char cDevNm[20]="/dev/tty01",
pcBuf[101],
cBuf[BUFFERSIZ]="Enter something: " ;
/*
* Open the serial port for the barcode reader device */
if ( (idBarc = open( cDevNm, O_RDWR )) == -1 ) {
printf( "Cannot open device %s\n", cDevNm ) ;
exit( 0 ) ;
}
else {
printf( "Opened barcode device %s\n", cDevNm ) ;
}
/*
* Get the serial port attributes */
if( (iStat = tcgetattr( idBarc, &termios ) ) == -1 ) {
printf( "Cannot get port attributes...\n" ) ;
exit( 0 ) ;
}
else {
printf( "Got serial port attributes\n" ) ;
printf( "===== Here's the original setup =====\n" ) ;
printf( " termios_iflag=%o\n", termios.c_iflag ) ;
printf( " termios_oflag=%o\n", termios.c_oflag ) ;
printf( " termios_cflag=%o\n", termios.c_cflag ) ;
printf( " termios_lflag=%o\n", termios.c_lflag ) ;
printf( "=====================================\n" ) ;
}
/*
* Set the serial port attributes */
termios.c_iflag = IGNBRK ;
/* ICRNL ; */
/* termios.c_oflag = (tcflag_t) (NULL) ; */
termios.c_cflag = CREAD |
CS7 |
PARENB |
B9600 ;
termios.c_lflag = ICANON ;
printf( "===== Here's what I want to set =====\n" ) ;
printf( " termios_iflag=%o\n", termios.c_iflag ) ;
printf( " termios_oflag=%o\n", termios.c_oflag ) ;
printf( " termios_cflag=%o\n", termios.c_cflag ) ;
printf( " termios_lflag=%o\n", termios.c_lflag ) ;
printf( "=====================================\n" ) ;
if( (iStat = tcsetattr( idBarc, TCSANOW, &termios ) ) == -1 ) {
printf( "Cannot set port attributes...\n" ) ;
exit( 0 ) ;
}
else {
printf( "Set serial port attributes\n" ) ;
}
/*
* Flush the buffer */
if( (iStat = tcflush( idBarc, TCIOFLUSH ) ) == -1 ) {
printf( "Cannot flush the buffer...\n" ) ;
exit( 0 ) ;
}
/*
* Get the serial port attributes */
if( (iStat = tcgetattr( idBarc, &termios ) ) == -1 ) {
printf( "Cannot get port attributes...\n" ) ;
exit( 0 ) ;
}
else {
printf( "Got serial port attributes\n" ) ;
printf( "==== Here's what actually got set ===\n" ) ;
printf( " termios_iflag=%o\n", termios.c_iflag ) ;
printf( " termios_oflag=%o\n", termios.c_oflag ) ;
printf( " termios_cflag=%o\n", termios.c_cflag ) ;
printf( " termios_lflag=%o\n", termios.c_lflag ) ;
printf( "=====================================\n" ) ;
}
/*
* Read from the COM port */
printf( "Reading from the COM port\n" ) ;
iStat = read( idBarc, (void *) pcBuf, 100 ) ;
printf( "iStat=%d\n", iStat ) ;
if( iStat != 0 )
printf( "--->Read this: %s\n", pcBuf ) ;
/*
* Initialize a few things */
FD_ZERO( &rfds ) ;
FD_SET( 0, &rfds ) ;
FD_SET( idBarc, &rfds ) ;
/*
* Wait up to 5 seconds */
tv.tv_sec = 5 ;
tv.tv_usec = 0 ;
iRet = select( 2, &rfds, NULL, NULL, &tv ) ;
/*
* Find out where input came from */
if( iRet ) {
printf( "Data is available now " ) ;
/* FD_ISSET( 0, &rfds ) will be true now */
if( FD_ISSET( 0, &rfds ) ) printf( "from the keyboard\n" ) ;
if( FD_ISSET( idBarc, &rfds ) ) printf( "from the scanner\n" ) ;
printf( "\n" ) ;
}
else
printf( "No data within 5 seconds\n" ) ;
/*
* Close the COM port */
close( idBarc ) ;
exit( 0 ) ;
}
-------------------------------------------------------------------------------
Thanks,
Dan
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".
- Raw text -