delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2007/01/19/04:39:35

X-Spam-Check-By: sourceware.org
Message-ID: <25e2d6fe0701190139r5e18aa89jc96e2229caaf8649@mail.gmail.com>
Date: Fri, 19 Jan 2007 10:39:09 +0100
From: "Florent Morin" <morinflorent AT gmail DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: cygwin 1.5.23-2 : I can't use select() with serial device and socket
In-Reply-To: <25e2d6fe0701180902m35903447w69c1ff91ef4eb8d1@mail.gmail.com>
MIME-Version: 1.0
References: <25e2d6fe0701180840k6bd5b27di630bca3a0a010ac3 AT mail DOT gmail DOT com> <45AFA5A5 DOT 3406349E AT dessent DOT net> <25e2d6fe0701180902m35903447w69c1ff91ef4eb8d1 AT mail DOT gmail DOT com>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

This code works fine on Linux and Cygwin (Windows XP SP2) :

------------------------------- CODE
----------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#define BUFFER_SIZE     256
#define DEVICE          "/dev/ttyS0"
#define TO_WRITE        "test communication"

int main (void)
{
    char buffer_read[BUFFER_SIZE];
    int data_read;

    char buffer_write[BUFFER_SIZE] = TO_WRITE;
    int data_write;

    int serial_fd;

    serial_fd = open(DEVICE, O_NONBLOCK | O_NOCTTY | O_RDWR);

    if (serial_fd < 0) {
        perror("open");
        return EXIT_FAILURE;
    } else
        printf("Serial port opened.\n");


    printf("Begin writing %s...\n", buffer_write);

    data_write = write(serial_fd, buffer_write, strlen(buffer_write));

    if (data_write < 0)
        perror("write");
    else
        printf("%d caracters written.\n", data_write);

    memset(&buffer_read, 0, BUFFER_SIZE);

    printf("Begin reading...\n");

    while ((data_read = read(serial_fd,
                             buffer_read,
                             BUFFER_SIZE - 1)) < 0) {
        if (errno == EAGAIN)
            memset(&buffer_read, 0, BUFFER_SIZE);
        else {
            perror("read");
            return EXIT_FAILURE;
        }
    }

    if (data_read < 0)
        perror("read");
    else
        printf("Data readed (%d) : %s\n", data_read, buffer_read);


    close(serial_fd);

    return EXIT_SUCCESS;
}

---------------------------------------------------------- /CODE
--------------------------------------------

This code works fine on Linux but doesn't work on Cygwin :

-------------------------------------- CODE
-----------------------------------------------

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#define BUFFER_SIZE     256
#define DEVICE          "/dev/ttyS0"
#define TO_WRITE        "test communication"

int main (void)
{
    char buffer_read[BUFFER_SIZE];
    int data_read;

    char buffer_write[BUFFER_SIZE] = TO_WRITE;
    int data_write;

    int serial_fd;

    serial_fd = open(DEVICE, O_NONBLOCK | O_NOCTTY | O_RDWR);

    if (serial_fd < 0) {
        perror("open");
        return EXIT_FAILURE;
    } else
        printf("Serial port opened.\n");


    printf("Begin writing %s...\n", buffer_write);

    data_write = write(serial_fd, buffer_write, strlen(buffer_write));

    if (data_write < 0)
        perror("write");
    else
        printf("%d caracters written.\n", data_write);

    memset(&buffer_read, 0, BUFFER_SIZE);

    printf("Begin reading...\n");

    while ((data_read = read(serial_fd,
                             buffer_read,
                             BUFFER_SIZE - 1)) < 0) {
        if (errno == EAGAIN)
            memset(&buffer_read, 0, BUFFER_SIZE);
        else {
            perror("read");
            return EXIT_FAILURE;
        }
    }

    if (data_read < 0)
        perror("read");
    else
        printf("Data readed (%d) : %s\n", data_read, buffer_read);


    close(serial_fd);

    return EXIT_SUCCESS;
}



------------------------------------------------ /CODE
----------------------------------------------------------

The second code write on socket, but it says that serial port isn't
ready to read.

Can someone explain it to me ?

Thanks.

Florent.

2007/1/18, Florent Morin <morinflorent AT gmail DOT com>:
> I use unix names. I will post an example code tomorrow.
>
> 2007/1/18, Brian Dessent <brian AT dessent DOT net>:
> > Florent Morin wrote:
> >
> > > I have a problem using cygwin. My program does this :
> > > - It accept a socket connection,
> > > - it listen on it,
> > > - it open serial device read/write (O_RDWR),
> > > - it create 2 fd_sets,
> > > - listening loop :
> > >   - adding file descriptors to sets,
> > >   - call select(),
> > >   - if something is on serial port, I write it to socket,
> > >   - if something is on socket, i write it to serial
> > >
> > > It works fine on Linux.
> > >
> > > With windows, only read or write works fine.
> > >
> > > If I begin on reading on serial, I can't write after (access denied).
> > > If I begin on writing on serial, I can't read after (access denied).
> >
> > There's probably not enough information here to help.  It would be
> > easier if you provided a simplified standalone testcase that we can
> > compile and run.  Are you opening the serial device using the standard
> > unix name (/dev/ttyS0) and not the DOS name ("COM1")?  The latter will
> > succeed but probably not work with things like ioctl or select, because
> > in order to emulate those APIs Cygwin has to know to treat the handle
> > specifically as a serial device, which it only does if you open it using
> > the unix form.
> >
> > Brian
> >
> > --
> > Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> > Problem reports:       http://cygwin.com/problems.html
> > Documentation:         http://cygwin.com/docs.html
> > FAQ:                   http://cygwin.com/faq/
> >
> >
>

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


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