This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Serial Communication in Win98/Cygwin



Hello,

I believe I have (hopefully) read many times through all the emails in the
archives and web pages relating to accessing the serial ports with Cygwin
under Win9x. Up until a few days ago, I had no idea how to change com port
parameters under Unix, so I've risen a long way on the learning curve.
Still I'm having problems, and seem to have run out of tips on the web and
in the archives.


My goal is to open a 'file' stream to COM1, set it to 8N1 at 4800 bps, and
read data strings from it.

From my Cygwin tcsh, I can cat </dev/com1 and see garbage data appear on
the screen from my device sending in serial data. The reason for the
garbage is due to incorrect port settings I beleive.

My code snippet is below. I'm using fopen to obtain a FILE * to /dev/com1,
then fileno() to obtain the file descriptor. With the file descriptor, I'm
obtaining the termios structure for the stream, modifying the settings,
and submitting the updates termios structure as per the Gnu libc docs.

The problem is I never read() or  getc() any data from the serial port
stream - it always returns EOF (-1) despite being able to cat from the raw
device.

Am I doing this the correct, 'clean' way? How should I be reading and
writing data from the serial port?

I'm using B20.1 (full binary release as of January 2000), with gcc 2.95.2.
I also have the mingw libraries installed although I am NOT using then
during the compile (no -mno-cygwin flag).


Regards,
Tim Baggett
tim@acca.nmsu.edu



#include <stdio.h>
#include <termios.h>

main()
{
	int c;
	FILE *GPS;
	int	gps;	// file descriptor for GPS

	struct termios *gps_config;

	if ((GPS = fopen("/dev/com1","wr")) == NULL ) {
		printf("Couldn't open /dev/com1 for read/write\n");
		exit(1);
	}

	gps = fileno( GPS );
	if( gps<0 ) {
		printf("Error getting fileno\n");
		exit(1);
	}

	/* Make sure it is a terminal. */
	if (!isatty (gps)) {
		printf ("'gps' Not a terminal.\n");
		exit (1);
	}


	// Get serial input stream attributesA
	if( tcgetattr( gps, gps_config ) ) {
		printf("Error tcgetattr\n");
		exit(1);
	}

	
	// Set baud rate to 4800 bpsA
	cfsetospeed( gps_config, B4800);
	cfsetispeed( gps_config, B4800);

	// Set 1 stop bit
	gps_config->c_cflag &= ~CSTOPB;	//clear bit

	// Set No Parity
	gps_config->c_cflag &= ~PARENB;	//clear bit

	// Set 8 bits / word
	gps_config->c_cflag &= ~CSIZE;	//clear size bits
	gps_config->c_cflag |= CS8;	//set 8 bits/word

	// noncanonical, not parity check, no flow controlA
	gps_config->c_lflag &= ~ICANON;
	gps_config->c_iflag &= ~INPCK;
	gps_config->c_iflag &= ~IXOFF;

	if( tcsetattr( gps, TCSANOW, gps_config ) ) {
		printf("Error tcsetattr\n");
		exit(1);
	}

	while (1) {
//		c = getc(GPS);
		read (gps, &c, 1);
		if ( c<0 ) continue;
		printf("%i ",c);
	}
	
	fclose( GPS );

}



--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]