Problem to read on a serial port

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
hi

i need to read from a  serial port (c programming)

we use a sixnet Mini-VersaTRAK mIPm
http://www.sixnetio.com/html_files/products_and_groups/mipm_vt.htm

under bash:
~ # stty -a </dev/ttyS2
speed 9600 baud; rows 0; columns 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z;
rprnt = ^R; werase = ^W; lnext = ^V; flush = ^U; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon
-ixoff -iuclc -ixany -imaxbel
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0
vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop
-echoprt echoctl echoke


i send a command via serial port to a led display (alpha sign
communications)

void ledDisplayExist()
{
    char msg[]=', '', '', '', '', '', 'Z', '0', '0',
'', 'H', 'A', ''  };
    int length = sizeof(msg);
    writeopen(msg, length);
    readopen();
}

i write msg with this function:

int writeopen(char msg[], int size)
{
    printf("write\n");
    int fd1;
    int wr;
    fd1 = open(ledisplay, O_RDWR | O_NOCTTY | O_NDELAY );
    if (fd1 == -1)
        fprintf(stderr, " %s open_port: Unable to open %s\n",
strerror(errno), ledisplay);
    else
    {
        fcntl(fd1, F_SETFL, 0);
        wr=write(fd1, msg, size);
        if (wr < 0)
            fputs("write() of n bytes failed!\n", stderr);
        close(fd1);
    }
    return (fd1);
}

i try to read with this function:

int readopen()
{
    printf("read\n");
    int fd1;
    int rd;
    char buff[255];
    fd1 = open(ledisplay, O_RDWR | O_NOCTTY | FNDELAY );
    if (fd1 == -1)
        fprintf(stderr, "%s open_port: Unable to open %s\n",
strerror(errno), ledisplay);
    else
    {
        fcntl(fd1, F_SETFL, FNDELAY);
        printf(" Port 1 has been sucessfully opened and %d is the file
descriptor\n",fd1);
        rd=read(fd1, buff, 20);
        printf(" Bytes recieved are %d \n",rd);
        close(fd1);
    }
    return (fd1);
}

that return always return me:  Bytes recieved are -1 (i write without
problem, the problem is the read)

any idea?

thanks


Re: Problem to read on a serial port

Quoted text here. Click to load it
ledisplay);
Quoted text here. Click to load it
                                                ^^^^^^^
That should be O_NDELAY-------------------------^^^^^^^

Quoted text here. Click to load it
ledisplay);
Quoted text here. Click to load it

If you're not going to set CLOCAL before setting FNDELAY, then
you might as well just do the open without O_NDELAY and skip
the fcntl() call.  The same comment applies to the writeopen()
function above.

Quoted text here. Click to load it
descriptor\n",fd1);
Quoted text here. Click to load it


--
Grant Edwards                   grante             Yow!  Clear the
                                  at               laundromat!! This
We've slightly trimmed the long signature. Click to see the full one.
Re: Problem to read on a serial port

Quoted text here. Click to load it

I am not familiar with the FNDELAY and O_NDELAY options to open. They are
not standard Linux.

Ian
--
Ian Bell

Re: Problem to read on a serial port

Quoted text here. Click to load it

O_NDELAY is on my man page for open(2):

O_NONBLOCK or O_NDELAY When possible, the file is opened in
       non-blocking mode.  Neither the open nor any subsequent
       operations on the file descriptor which is returned will
       cause the calling process to wait.  For the handling of
       FIFOs (named pipes), see also fifo(4).  This mode need
       not have any effect on files other than FIFOs.

It's the commonly accepted way to open a serial port when you
don't want the open() to block waiting for carrier detect.

Setting the FNDELAY flag is then the way you set the file
descripter back to blocking mode after you've set the CLOCAL
bit in the termios struct to tell it to ignore carrier detect.

--
Grant Edwards                   grante             Yow!  World War III? No
                                  at               thanks!
We've slightly trimmed the long signature. Click to see the full one.

Site Timeline