delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/08/24/01:33:13

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT sourceware DOT cygnus DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT sourceware DOT cygnus DOT com>
List-Archive: <http://sourceware.cygnus.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sourceware DOT cygnus DOT com>
List-Help: <mailto:cygwin-help AT sourceware DOT cygnus DOT com>,
<http://sourceware.cygnus.com/ml/#faqs>
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
Message-ID: <37C22DB3.BF500713@tbit.dk>
Date: Tue, 24 Aug 1999 07:29:23 +0200
From: Kim Poulsen <kpo AT tbit DOT dk>
Organization: Ericsson Telebit A/S
X-Mailer: Mozilla 4.61 [en] (Win98; I)
X-Accept-Language: en
MIME-Version: 1.0
To: cygwin AT sourceware DOT cygnus DOT com
Subject: Re: Serial port programming
References: <37C0EAC4 DOT 63151D86 AT tbit DOT dk> <37C113FB DOT 9676DF40 AT vinschen DOT de> <37C11931 DOT 422C65E6 AT tbit DOT dk> <19990823110515 DOT C13837 AT cygnus DOT com>

Chris,

The problem with fopen() is that it returns a FILE* where open() return
an int.  fopen() is way to advanced for use with serial port I/O as
the concept of a FILE is meaningless on a serial channel i.e. there is
no name or path etc. associated with a raw serial stream.  In
addition read() and write() uses an int file descriptor and not a FILE*.
  The open(), read(), and write() functions are the most low level
I/O handling ANSI C can handle.  The f*() functions are all built on
these functions.  If I use fopen() I have no way of controlling the
channel characteristics such as baud rate, bits, parity etc. This
I can do with the int returned from the open() function.
  If however you should have some running code (the code below _will_
run) using fopen() I'd sure like to see it for the educational value.

Thanks for your response.
  /Kim

PS:  Why are you posting everything twice ?

Chris Faylor wrote:
> 
> 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 AT sourceware DOT cygnus DOT com

-- 
 Kim Poulsen, B.Sc.E.E, System Developer HW
 Ericsson Telebit A/S     Tel: + 45 86 28 81 76
 Fabriksvej 11            Fax: + 45 86 28 81 86
 DK-8260 Viby J           E-mail: info AT tbit DOT dk
 Denmark                  URL: http://www.tbit.dk/

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com

- Raw text -


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