cygwin 1.5.23-2 : I can't use select() with serial device and socket

Florent Morin morinflorent@gmail.com
Fri Jan 19 09:41:00 GMT 2007


Excuse me, this is my second code :

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

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

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

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

    char buffer_write[BUFFER_SIZE] = TO_WRITE;
    int data_write;

    int serial_fd;

    fd_set read_set;
    fd_set write_set;

    int written = 0;

    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");

    while (1) {
        FD_ZERO(&read_set);
        FD_SET(serial_fd, &read_set);

        FD_ZERO(&write_set);
        FD_SET(serial_fd, &write_set);

        if (select(serial_fd + 1,
                   &read_set,
                   &write_set,
                   NULL,
                   NULL) < 0) {
            perror("select");
            return EXIT_FAILURE;
        }

        if (FD_ISSET(serial_fd, &read_set)) {

            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);
        } else
            printf("Serial port not ready to read.\n");

        if ((FD_ISSET(serial_fd, &write_set)) && (written == 0)) {
            printf("Begin writing %s...\n", buffer_write);
            written = 1;
            data_write = write(serial_fd, buffer_write, strlen(buffer_write));
            if (data_write < 0)
                perror("write");
            else
                printf("%d caracters written.\n", data_write);
        } else {
            if (FD_ISSET(serial_fd, &write_set))
                printf("Serial port ready to write but no.\n");
            else
                printf("Serial port not ready to write.\n");
        }

        printf("\n---------------------------------------------\n");
        sleep(1);
    }

    close(serial_fd);

    return EXIT_SUCCESS;
}


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

Thanks,

Florent

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



More information about the Cygwin mailing list