如何检查linux中的串口是否正常工作

时间:2015-03-09 08:03:29

标签: c++ linux serialization ubuntu-10.04

我将在我的C ++程序linux ubuntu 10.4中使用串口,​​这是我的开放端口功能:

    int Recorder::OpenPort()
    {
     int intFd ;
    struct termios options;   

    intFd=open("/dev/ttyS0", O_RDWR | O_NOCTTY| O_NDELAY);
    if (intFd==-1){
        perror("open_port: Unable to open /dev/ttyS0 - ");
    }
    fcntl(intFd, F_SETFL, FNDELAY);    /*configuration the port*/ 

    tcgetattr(intFd, &options);

    //set baud rates at 115200 
    cfsetispeed(&options, B115200 );                
    cfsetospeed(&options, B115200);

    //mask the character size to 8 bit data  & no parity.CHARACTER SIZE SETTING
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;        
    options.c_cflag &= ~CSIZE;
    options.c_cflag |=  CS8;

    //setting hardware flow control
    options.c_cflag |= CRTSCTS;    

    //Enable the receiver and set local mode .should always be enabled (enable receiver and dont change owner of port)
    options.c_cflag |= (CLOCAL | CREAD);

    //flush output buffer
    options.c_lflag |= FLUSHO;

    //choosing raw data disable echoing 
    options.c_lflag &=  ~(ICANON | ECHO | ECHOE | ISIG);

    //not setting software  flow control
    options.c_iflag &=  ~(IXON | IXOFF | IXANY); 


    options.c_oflag &=  ~OPOST; 

//  //output changes :for mapping NL to CR-NL. 

    //  options.c_oflag |= (OPOST | ONLCR );        
        // 
    //  options.c_oflag |= (NL1 | CR1 | FFDLY);


    tcsetattr(intFd, TCSANOW, &options);

    return intFd;
}

这是我查看序列号的示例:

int  serial=0;
    ioctl(intFd, TIOCMGET, &serial);
    //for ubuntu 7.04 and 10.04
        if(serial==16390 || serial==16422 || serial==16454 || serial==16486){//no connection with serial port

我检查了我的程序很多次,串口是16486每次都意味着我没有连接串口,我检查了我的串口线,它没问题?那么我怎样才能解决我的问题?

0 个答案:

没有答案