From: Kim Poulsen <kpo@tbit.dk>
To: cygwin@sourceware.cygnus.com
Subject: Re: Serial port programming
Date: Mon, 23 Aug 1999 22:31:00 -0000 [thread overview]
Message-ID: <37C22DB3.BF500713@tbit.dk> (raw)
In-Reply-To: <19990823110515.C13837@cygnus.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@sourceware.cygnus.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@tbit.dk
Denmark URL: http://www.tbit.dk/
--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com
WARNING: multiple messages have this Message-ID
From: Kim Poulsen <kpo@tbit.dk>
To: cygwin@sourceware.cygnus.com
Subject: Re: Serial port programming
Date: Tue, 31 Aug 1999 23:49:00 -0000 [thread overview]
Message-ID: <37C22DB3.BF500713@tbit.dk> (raw)
Message-ID: <19990831234900.U7a8yWTmEl7SPvPzSj4NlT1lXw_6iG2MnLsbQ9b-LoU@z> (raw)
In-Reply-To: <19990823110515.C13837@cygnus.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@sourceware.cygnus.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@tbit.dk
Denmark URL: http://www.tbit.dk/
--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com
next prev parent reply other threads:[~1999-08-23 22:31 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-08-22 23:32 Kim Poulsen
1999-08-22 23:52 ` Baron Shatturday
[not found] ` <199908230653.IAA17307@mailhostnew.tbit.dk>
1999-08-23 0:04 ` Kim Poulsen
1999-08-31 23:49 ` Kim Poulsen
1999-08-23 2:32 ` Corinna Vinschen
1999-08-23 2:50 ` Kim Poulsen
1999-08-23 8:03 ` Chris Faylor
1999-08-23 22:31 ` Kim Poulsen [this message]
1999-08-23 22:35 ` Geoff Appleby
1999-08-31 23:49 ` Geoff Appleby
1999-08-24 9:17 ` Chris Faylor
1999-08-31 23:49 ` Chris Faylor
1999-08-31 23:49 ` Kim Poulsen
1999-08-31 23:49 ` Chris Faylor
1999-08-31 23:49 ` Kim Poulsen
1999-08-23 8:00 ` Chris Faylor
1999-08-31 23:49 ` Chris Faylor
1999-08-31 23:49 ` Corinna Vinschen
[not found] ` <199908230653.XAA03904@cygnus.com>
1999-08-23 7:59 ` Chris Faylor
1999-08-31 23:49 ` Chris Faylor
1999-08-31 23:49 ` Kim Poulsen
1999-08-31 23:49 ` Baron Shatturday
2000-05-15 9:23 serial " j_s_saini
2000-05-29 7:43 Serial " El
2001-03-30 17:16 serial " Tim Holmes
2001-03-30 17:20 ` Christopher Faylor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=37C22DB3.BF500713@tbit.dk \
--to=kpo@tbit.dk \
--cc=cygwin@sourceware.cygnus.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).