delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2009/10/29/05:00:03

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-2.1 required=5.0 tests=AWL,BAYES_00,SPF_PASS
X-Spam-Check-By: sourceware.org
Message-ID: <4AE96795.4080705@gmx.de>
Date: Thu, 29 Oct 2009 10:59:49 +0100
From: Matthias Andree <matthias DOT andree AT gmx DOT de>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: write() to /dev/ttyS0 has no effect
References: <187469a20910290204n67867327w7b8759e1aae85b3e AT mail DOT gmail DOT com>
In-Reply-To: <187469a20910290204n67867327w7b8759e1aae85b3e@mail.gmail.com>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

Børge Strand-Bergesen schrieb:

> 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?

"Works for me", albeit with MSP430 behind a FTDI USB/serial converter and
without CRTSCTS and lower bit rate (57600).

> 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 */

This must be before the first #include.

> #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

What's that good for? You're not using that.

> 	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???

Not needed.

> 	
> 	tcgetattr(fd,&oldtio);					// Save current port settings
> 
> 	// Non-canonical init.
> 	bzero(&newtio, sizeof(newtio));

bzero() isn't standard. Use memset(), sample below.

> 	newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

Does the µC support CRTSCTS? If not, that's why it fails.

Also, BAUDRATE may not fit into speed_t according to /usr/include/sys/termios.h,
so you're losing the "extended baud rate" flag and are actually programming 75
Baud instead of 115200. Use cfsetXspeed (X is i or o) to manipulate newtio
instead, example (in C++):

                struct termios newtio;
                /* configure serial interface to 57600 8N1 no-canonical */
                memset(&newtio, 0, sizeof(newtio));
                newtio.c_cflag = CS8 | CLOCAL | CREAD;
                newtio.c_iflag = 0;
                newtio.c_oflag = 0;
                newtio.c_lflag = 0;
                newtio.c_cc[VTIME] = 0;
                newtio.c_cc[VMIN] = 1;  /* at least 1 characters */
                if (cfsetispeed(&newtio, B57600)) throw("cfsetispeed");
                if (cfsetospeed(&newtio, B57600)) throw("cfsetospeed");
                if (tcflush(fd, TCIFLUSH)) throw("tcflush");
                if (tcsetattr(fd, TCSANOW, &newtio)) throw("tcsetattr");


> 	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!
> 		}
> 
> 	}

You're apparently not reading from the serial line. Is that intentional?

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

This is unreached (dead) code.

> }

HTH

--
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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019