This is the mail archive of the cygwin 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]
Other format: [Raw text]

write() to /dev/ttyS0 has no effect


Hi,

I'm writing some C code to control an external MCU over UART.
Everything works like a charm using TeraTerm or cat >>/dev/ttyS0. gcc
is 3.4.4. In a different program (not inserted), I am able to use
read() to get information from the MCU. Cygwin is "CYGWIN_NT-5.1
1.5.25(0.156/4/2) 2008-06-12 19:34".

However, it seems like no information is sent when I call write(). Are
there any known bugs with Cygwin when it comes to this?

I have inserted my code below. Thanks for any help!

"f" is a valid command to the MCU. The MCU will disregard any \r or
\n. I have tried hitting the enter button, not just 'a' on the
keyboard.


Borge


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
	
#define BAUDRATE B115200
#define MODEMDEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
	

FILE *keyboard;
int status;

main()
{
	int fd,c, res;
	struct termios oldtio,newtio;

	keyboard = fopen("/dev/tty", "r");      //open the terminal keyboard
	if (!keyboard)
	{
		fprintf(stderr, "Unable to open /dev/tty\n");
		exit(1);
	}

	fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
	if (fd <0) {perror(MODEMDEVICE); exit(-1); }

	fcntl(fd, F_SETFL, 0); // Needed???
	
	tcgetattr(fd,&oldtio);					// Save current port settings

	// Non-canonical init.
	bzero(&newtio, sizeof(newtio));
	newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
	newtio.c_iflag = IGNPAR;
	newtio.c_oflag = 0;
	newtio.c_lflag = 0;
	newtio.c_cc[VTIME] = 0;					// inter-character timer unused
	newtio.c_cc[VMIN] = 5; 					// blocking read until 5 chars received

	tcflush(fd, TCIFLUSH);
	tcsetattr(fd,TCSANOW,&newtio);

	while (1)
	{
		status = getc(stdin);
		if (status == 'a') {
			char outbuf[] = "f";
			printf("%s", outbuf);			// This printout is ok
			write(fd, outbuf, 1);			// This doesn't seem to get sent down the uart!
		}

	}
	tcsetattr(fd,TCSANOW,&oldtio);			// Restore port settings
}

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


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