plz help with the serial port pgm....

shibin k reeny shibinkreeny at gmail.com
Wed Nov 23 00:48:11 EST 2011


rs232 read pgm is not working when i connect in loopback but write is
wroking plz help...

pgm

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

#define _POSIX_SOURCE 1

#define BAUDRATE        B9600
#define MODEMDEVICE        "/dev/ttyS1"

int main(void)
{
    int ret, fd;
    struct termios tio;
    char buf[255];

    /* Open device for reading and writing */
    fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd < 0) {
        fprintf(stderr, "Error opening device %s\n", MODEMDEVICE);
        goto done;
    }
    /* get current port attributes */
    ret = tcgetattr(fd, &tio);
    if (ret < 0) {
        fprintf(stderr, "Error retreiving attributes, ret=%d\n", ret);
        goto done;
    }
    tcflush(fd, TCIFLUSH);

    /* set port input speed */
    ret = cfsetispeed(&tio, BAUDRATE);
    if (ret) {
        fprintf(stderr, "Error setting input Baud rate, ret=%d\n", ret);
        goto done;
    }
    /* set port output speed */
    ret = cfsetospeed(&tio, BAUDRATE);
    if (ret) {
        fprintf(stderr, "Error setting output Baud rate, ret=%d\n", ret);
        goto done;
    }

    tio.c_cflag &= ~CRTSCTS; /* HW flow ctl OFF */
    tio.c_cflag &= ~PARENB; /* no parity */
    tio.c_cflag &= ~CSTOPB; /* 1 stop bit */
    tio.c_cflag &= ~CSIZE; /* 1 stop bit */
    tio.c_cflag |= CS8; /* char size; 8N1 */
    tio.c_cflag |= CREAD; /* enable receiver */
    /* set port attributes */
    ret = tcsetattr(fd, TCSANOW, &tio);
    if (ret < 0) {
        fprintf(stderr, "Error setting attributes, ret=%d\n", ret);
        goto done;
    }
    fcntl(fd, F_SETFL, FNDELAY);
    ret = write(fd, "ab\r", 3);
    if (ret < 0)
        fprintf(stderr, "write() of 3 bytes failed!, ret=%d\n", ret);

    printf("no of data written = %d\n",ret);
    usleep(5000);
    while (1) {
        ret = read(fd, buf, sizeof(char));
        if (ret > 0) {
            printf("%c",buf[0]);

        }
    }
done:
    close(fd);
    return 0;
}


##################################
output../a.out
no of data written = 3




########################
strace ./a.out
read(3, 0xbfa8d5dd, 1)                  = -1 EAGAIN (Resource temporarily
unavailable)
read(3, 0xbfa8d5dd, 1)                  = -1 EAGAIN (Resource temporarily
unavailable)
read(3, 0xbfa8d5dd, 1)                  = -1 EAGAIN (Resource temporarily
unavailable)
read(3, 0xbfa8d5dd, 1)                  = -1 EAGAIN (Resource temporarily
unavailable)
read(3, 0xbfa8d5dd, 1)                  = -1 EAGAIN (Resource temporarily
unavailable)
read(3, 0xbfa8d5dd, 1)                  = -1 EAGAIN (Resource temporarily
unavailable)
read(3, 0xbfa8d5dd, 1)                  = -1 EAGAIN (Resource temporarily
unavailable)
read(3, 0xbfa8d5dd, 1)                  = -1 EAGAIN (Resource temporarily
unavailable)
read(3, 0xbfa8d5dd, 1)                  = -1 EAGAIN (Resource temporarily
unavailable)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20111123/3bef154b/attachment.html 


More information about the Kernelnewbies mailing list