puzzle by serial

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

Translate This Thread From English to

puzzle by serial

[code]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>

#define FALSE  -1
#define TRUE   0


int OpenDev(char *Dev)
{
        int        fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY
        if (-1 == fd)
        {
                perror("Can't Open Serial Port");
                return -1;
        }
        else
                return fd;
}
int main(int argc, char **argv)
{
        int fd, i = 10, attrfd;
        int nwrite;
        static struct termios terattr;
        char buff[512] = "hello world";
        char *dev  = "/dev/ttyS0";
        fd = OpenDev(dev);
        printf("fd == %d\n", fd);
        if(-1 == fd)
                return -1;
        attrfd = tcgetattr(fd, &terattr);
        if(-1 == attrfd)
                perror("attrfd");
        printf("iflag == %d\n", terattr.c_iflag);
        printf("oflag == %d\n", terattr.c_oflag);
        printf("cflag == %d\n", terattr.c_cflag);
        printf("lflag == %d\n", terattr.c_lflag);


}
[/code]

compile and run the program, it will put out:
[code]
fd == 3
attrfd: Input/output error
iflag == 134513255
oflag == 134519244
cflag == 10906172
lflag == 12074996
[/code]

i have a problem that:
1) it can open /dev/ttyS0, why tcgetattr return error, the reference is that
Input/output error. but i dont konw why get the error, is that there is
something wrong with the serial driver?
2) tcgetattr return error, why it can get terattr, and it seem them
wrong(but im not sure it)
3)



Site Timeline