delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2009/10/29/04:04:56

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-0.6 required=5.0 tests=AWL,BAYES_00,SARE_MSGID_LONG40,SPF_PASS
X-Spam-Check-By: sourceware.org
MIME-Version: 1.0
From: =?ISO-8859-1?Q?B=F8rge_Strand=2DBergesen?= <borge DOT strand AT gmail DOT com>
Date: Thu, 29 Oct 2009 10:04:24 +0100
Message-ID: <187469a20910290204n67867327w7b8759e1aae85b3e@mail.gmail.com>
Subject: write() to /dev/ttyS0 has no effect
To: cygwin AT cygwin DOT 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

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

- Raw text -


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