> #define MODEMDEVICE        "/dev/ttyS1"
I believe that you are loop-backing in the right physical serial port. Now
your device file is changed to ttyS1.

termios flags are changed !

EAGAIN (Resource temporarily unavailable) is a temporary condition that
may change in the later executions.


- Jos Collin

> 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)
> read(3, 0xbfa8d5dd, 1)                  = -1 EAGAIN (Resource
temporarily
> unavailable)
> read(3, 0xbfa8d5dd, 1)                  = -1 EAGAIN (Resource
temporarily
> unavailable)
> On Tue, Nov 15, 2011 at 7:52 PM, Jos Collin <[email protected]> wrote:
>> > bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings
>> */
>> needs #include <string.h> to suppress the warning.
>> >   int w = write(fd,"hello world\n",12);
>> I think you can wait for a few milliseconds before read.
>> >res = read(fd,buf,255);
>> I don't know what you are getting after executing the code. You can try
making your serial read non blocking to know what is happening.
fcntl(fd, F_SETFL, FNDELAY);
>> //......
>> res = read(fd,buf,255);
>> //.....
>> if (read_ret < 0)
>> {
>>  if (EAGAIN == errno)
>>  {
>>    printf("no data, read again.\n");
>>  }
>>  else
>>  {
>>    printf("read error.\n");
>>   }
>> }
>> FYI, I haven't tested your code with serial loop-back.
>> Happy Hacking,
>> Jos Collin
>> _______________________________________________
>> Indian Libre User Group Cochin Mailing List
>> http://www.ilug-cochin.org/mailing-list/
>> http://mail.ilug-cochin.org/mailman/listinfo/mailinglist_ilug-cochin.org
#[email protected]
> _______________________________________________
> Indian Libre User Group Cochin Mailing List
> http://www.ilug-cochin.org/mailing-list/
> http://mail.ilug-cochin.org/mailman/listinfo/mailinglist_ilug-cochin.org
#[email protected]







_______________________________________________
Indian Libre User Group Cochin Mailing List
http://www.ilug-cochin.org/mailing-list/
http://mail.ilug-cochin.org/mailman/listinfo/mailinglist_ilug-cochin.org
#[email protected]

Reply via email to