X-Recipient: archive-cygwin AT delorie DOT com X-SWARE-Spam-Status: No, hits=-0.8 required=5.0 tests=AWL,BAYES_00,SARE_MSGID_LONG40,SPF_PASS X-Spam-Check-By: sourceware.org MIME-Version: 1.0 In-Reply-To: <4AE96795.4080705@gmx.de> References: <187469a20910290204n67867327w7b8759e1aae85b3e AT mail DOT gmail DOT com> <4AE96795 DOT 4080705 AT gmx DOT de> From: =?ISO-8859-1?Q?B=F8rge_Strand=2DBergesen?= Date: Thu, 29 Oct 2009 12:05:33 +0100 Message-ID: <187469a20910290405o6091b108u58856ee06ea98c98@mail.gmail.com> Subject: Re: write() to /dev/ttyS0 has no effect To: cygwin AT cygwin DOT com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Matthias, vielen Dank! It works now. Your questions were mostly about unrelated code that somehow survived as I stripped things down to make the example. Removing CRTSCTS did the trick. I see that you are using non-canonical reads. Do you know if canonical works on Cygwin? In my finished program I will indeed be reading input from the UART. But "res =3D read(fd,inbuf,255);" returns on several occations when it contents are not terminated by \n, \r or 0. As you probably see from my code, I have copied the Serial HOWTO code. B=F8rge On Thu, Oct 29, 2009 at 10:59, Matthias Andree wro= te: > B=F8rge 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 >> #include >> #include >> #include >> #include >> #include >> >> #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() >> { >> =A0 =A0 =A0 int fd,c, res; >> =A0 =A0 =A0 struct termios oldtio,newtio; >> >> =A0 =A0 =A0 keyboard =3D fopen("/dev/tty", "r"); =A0 =A0 =A0//open the t= erminal keyboard > > What's that good for? You're not using that. > >> =A0 =A0 =A0 if (!keyboard) >> =A0 =A0 =A0 { >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 fprintf(stderr, "Unable to open /dev/tty\n"); >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 exit(1); >> =A0 =A0 =A0 } >> >> =A0 =A0 =A0 fd =3D open(MODEMDEVICE, O_RDWR | O_NOCTTY ); >> =A0 =A0 =A0 if (fd <0) {perror(MODEMDEVICE); exit(-1); } >> >> =A0 =A0 =A0 fcntl(fd, F_SETFL, 0); // Needed??? > > Not needed. > >> >> =A0 =A0 =A0 tcgetattr(fd,&oldtio); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// Save current port settings >> >> =A0 =A0 =A0 // Non-canonical init. >> =A0 =A0 =A0 bzero(&newtio, sizeof(newtio)); > > bzero() isn't standard. Use memset(), sample below. > >> =A0 =A0 =A0 newtio.c_cflag =3D BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; > > Does the =B5C support CRTSCTS? If not, that's why it fails. > > Also, BAUDRATE may not fit into speed_t according to /usr/include/sys/ter= mios.h, > so you're losing the "extended baud rate" flag and are actually programmi= ng 75 > Baud instead of 115200. Use cfsetXspeed (X is i or o) to manipulate newtio > instead, example (in C++): > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0struct termios newtio; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0/* configure serial interface to 57600 8N1= no-canonical */ > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0memset(&newtio, 0, sizeof(newtio)); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0newtio.c_cflag =3D CS8 | CLOCAL | CREAD; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0newtio.c_iflag =3D 0; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0newtio.c_oflag =3D 0; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0newtio.c_lflag =3D 0; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0newtio.c_cc[VTIME] =3D 0; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0newtio.c_cc[VMIN] =3D 1; =A0/* at least 1 = characters */ > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (cfsetispeed(&newtio, B57600)) throw("c= fsetispeed"); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (cfsetospeed(&newtio, B57600)) throw("c= fsetospeed"); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (tcflush(fd, TCIFLUSH)) throw("tcflush"= ); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (tcsetattr(fd, TCSANOW, &newtio)) throw= ("tcsetattr"); > > >> =A0 =A0 =A0 newtio.c_iflag =3D IGNPAR; >> =A0 =A0 =A0 newtio.c_oflag =3D 0; >> =A0 =A0 =A0 newtio.c_lflag =3D 0; >> =A0 =A0 =A0 newtio.c_cc[VTIME] =3D 0; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // inter-character timer unused >> =A0 =A0 =A0 newtio.c_cc[VMIN] =3D 5; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// blocking read until 5 chars received >> >> =A0 =A0 =A0 tcflush(fd, TCIFLUSH); >> =A0 =A0 =A0 tcsetattr(fd,TCSANOW,&newtio); >> >> =A0 =A0 =A0 while (1) >> =A0 =A0 =A0 { >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 status =3D getc(stdin); >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (status =3D=3D 'a') { >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 char outbuf[] =3D "f"; >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("%s", outbuf); =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // This printout is ok >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 write(fd, outbuf, 1); =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // This doesn't seem to get sent down the u= art! >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 } >> >> =A0 =A0 =A0 } > > You're apparently not reading from the serial line. Is that intentional? > >> =A0 =A0 =A0 tcsetattr(fd,TCSANOW,&oldtio); =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0// Restore port settings > > This is unreached (dead) code. > >> } > > HTH > > -- > Problem reports: =A0 =A0 =A0 http://cygwin.com/problems.html > FAQ: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 http://cygwin.com/faq/ > Documentation: =A0 =A0 =A0 =A0 http://cygwin.com/docs.html > Unsubscribe info: =A0 =A0 =A0http://cygwin.com/ml/#unsubscribe-simple > > -- 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