kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* plz help with the serial port pgm....
@ 2011-11-23  5:48 shibin k reeny
  2011-11-23  7:21 ` Dave Hylands
  0 siblings, 1 reply; 2+ messages in thread
From: shibin k reeny @ 2011-11-23  5:48 UTC (permalink / raw)
  To: kernelnewbies

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 

^ permalink raw reply	[flat|nested] 2+ messages in thread

* plz help with the serial port pgm....
  2011-11-23  5:48 plz help with the serial port pgm shibin k reeny
@ 2011-11-23  7:21 ` Dave Hylands
  0 siblings, 0 replies; 2+ messages in thread
From: Dave Hylands @ 2011-11-23  7:21 UTC (permalink / raw)
  To: kernelnewbies

Hi shibin

On Tue, Nov 22, 2011 at 9:48 PM, shibin k reeny <shibinkreeny@gmail.com> wrote:
> rs232 read pgm is not working when i connect in loopback but write is
> wroking plz help...

Rather than trying to debug your program, I'll point you at this document:
http://tldp.org/HOWTO/Serial-Programming-HOWTO/

and point at some sample code which I wrote a while ago, which I know works:
http://svn.hylands.org/host/sertest/sertest.c

-- 
Dave Hylands
Shuswap, BC, Canada
http://www.davehylands.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-11-23  7:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-11-23  5:48 plz help with the serial port pgm shibin k reeny
2011-11-23  7:21 ` Dave Hylands

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).