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]

Re: Serial port programming


On Mon, Aug 23, 1999 at 11:49:37AM +0200, Kim Poulsen wrote:
>Corinna Vinschen wrote:
>> Kim Poulsen wrote:
>> >   I've got the following problem:
>> > I need to access the serial ports of my PC through an ANSI C program.
>> > How do I do that ?  I have already tried using fopen("/dev/com2", "r")
>> > and fopen("com2", "r") but these only causes a core dump.
>> > [...]
>> AFAIK this is a known problem in b20.1. Try to use a newer snapshot.
>
>I have the problem solved.  A a contribution to the mailing list I
>submit the solution to the problem below.

I'm not sure how this solves your problem.  You aren't using fopen.  That
appears to be it.

As is so often the case, with these kinds of problems, simply running the
program using gdb would have probably provided worlds of information for
debugging the problem.

If the code sample below is getting you running, then fine.  It is not
a generic solution for people who want to use stdio for serial I/O,
however.  AFAIK, that does work.

-chris

>#include <fcntl.h>
>#include <termios.h>
>#include <stdio.h>
>#include <unistd.h>
>
>#define BAUDRATE B9600
>#define MODEMDEVICE "com2"
>
>main()
>{
>  int fd,c, n;
>  char str[2];
>  struct termios options;
>
>  fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
>  if (fd <0) {perror(MODEMDEVICE); exit(-1); }
>
>  options.c_cflag = BAUDRATE;
>  options.c_cflag &= ~CSIZE; /* Mask the character size bits */
>  options.c_cflag |= CS8;    /* Select 8 data bits */
>
>  /* Write something */
>  n = write(fd, "UART is functional\n\r", 19);
>  if (n < 0)
>    puts("write() of 19 bytes failed!"); 
>
>  /* Make read() return immediately */
>  fcntl(fd, F_SETFL, FNDELAY);
>
>  /* Read something until 'Q' recieved */
>  while(str[0] != 'Q') {
>    if(read(fd, str, 1) != -1) {
>      printf("%s\n", str);
>    }
>  }
>}

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