Mailing-List: contact cygwin-help@sourceware.cygnus.com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie.com@sourceware.cygnus.com>
List-Archive: <http://sourceware.cygnus.com/ml/cygwin/>
List-Post: <mailto:cygwin@sourceware.cygnus.com>
List-Help: <mailto:cygwin-help@sourceware.cygnus.com>,
	<http://sourceware.cygnus.com/ml/#faqs>
Sender: cygwin-owner@sourceware.cygnus.com
Delivered-To: mailing list cygwin@sourceware.cygnus.com
Message-ID: <37C11931.422C65E6@tbit.dk>
Date: Mon, 23 Aug 1999 11:49:37 +0200
From: Kim Poulsen <kpo@tbit.dk>
Organization: Ericsson Telebit A/S
X-Mailer: Mozilla 4.61 [en] (Win98; I)
X-Accept-Language: en
MIME-Version: 1.0
CC: Cygwin Mailing list <cygwin@sourceware.cygnus.com>
Subject: Re: Serial port programming
References: <37C0EAC4.63151D86@tbit.dk> <37C113FB.9676DF40@vinschen.de>
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

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.

#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);
    }
  }
}

This code should run as expected.
A good link on the web is this one :
   http://revolution.rebel.net/~mad/serialtutor/

The tip was provided to me by Řistein Aanensen from Norway.  Credits
to him.

Regards
  Kim
-- 
 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

