在C中的Linux中清除串行端口的数据? [英] Clear data at serial port in Linux in C?

查看:13
本文介绍了在C中的Linux中清除串行端口的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试发送和接收程序,代码为

main()函数如下:

<上一页>#include "lib.h"诠释 fd;int initport(int fd){结构 termios 选项;//获取端口的当前选项...tcgetattr(fd, &options);//将波特率设置为 19200...cfsetispeed(&options, B9600);cfsetospeed(&options, B9600);//启用接收器并设置本地模式...options.c_cflag |= (CLOCAL | CREAD);options.c_cflag &= ~PARENB;options.c_cflag &= ~CSTOPB;options.c_cflag &= ~CSIZE;选项.c_cflag |= CS8;//设置端口的新选项...tcsetattr(fd, TCSANOW, &options);返回 1;}int main(int argc, char **argv) {fd = open("/dev/pts/2", O_RDWR | O_NOCTTY | O_NDELAY);如果(fd == -1){perror("open_port: 无法打开/dev/pts/1 - ");返回 1;} 别的 {fcntl(fd, F_SETFL, 0);}printf("波特=%d ", getbaud(fd));初始化端口(fd);printf("波特=%d ", getbaud(fd));字符 sCmd[254];sCmd[0] = 0x41;sCmd[1] = 0x42;sCmd[2] = 0x43;sCmd[3] = 0x00;如果(!writeport(fd,sCmd)){printf("写入失败 ");关闭(fd);返回 1;}printf("写入:%s ", sCmd);睡眠(500000);字符结果[254];fcntl(fd, F_SETFL, FNDELAY);如果(!readport(fd,sResult)){printf("读取失败 ");关闭(fd);返回 1;}printf("readport=%s ", sResult);关闭(fd);返回0;}

lib.h 包含的读写代码如下:

用 C 语言解析和读取数据帧?

并得到了问题:

为了用串口进行测试,我使用了socat (https://help.ubuntu.com/community/VirtualSerialPort ) 在 Linux 上创建一对串行端口并使用这些端口测试我的程序.

程序第一次发送数据,程序接收到数据就ok了.但是,如果我再次读取甚至将新数据重新写入串口,返回数据始终为空,直到我停止虚拟串口并重新启动它,然后写入和读取数据都可以,但仍然,只有一次.

(在实际情况下,发送部分将由另一个设备完成,我只是负责从串口读取数据.我编写了两个部分只是为了测试我的读取代码.)

有人有什么想法吗?

解决方案

要么你的评论有误,要么你的代码有误:

//将波特率设置为 19200...cfsetispeed(和选项,B9600);cfsetospeed(和选项,B9600);

这表示它将波特率设置为 19200,但实际上将其设置为 9600.也许你想要这个:

//将波特率设置为 19200...cfsetispeed(和选项,B19200);cfsetospeed(和选项,B19200);

I am testing the sending and receiving programs with the code as

The main() function is below:

#include "lib.h"

int fd;

int initport(int fd) {
    struct termios options;
    // Get the current options for the port...
    tcgetattr(fd, &options);
    // Set the baud rates to 19200...
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
    // Enable the receiver and set local mode...
    options.c_cflag |= (CLOCAL | CREAD);

    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;

    // Set the new options for the port...
    tcsetattr(fd, TCSANOW, &options);
    return 1;
}

int main(int argc, char **argv) {

    fd = open("/dev/pts/2", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1) {
        perror("open_port: Unable to open /dev/pts/1 - ");
        return 1;
    } else {
        fcntl(fd, F_SETFL, 0);
    }

    printf("baud=%d
", getbaud(fd));
    initport(fd);
    printf("baud=%d
", getbaud(fd));

    char sCmd[254];
    sCmd[0] = 0x41;
    sCmd[1] = 0x42;
    sCmd[2] = 0x43;
    sCmd[3] = 0x00;

    if (!writeport(fd, sCmd)) {
        printf("write failed
");
        close(fd);
        return 1;
    }

    printf("written:%s
", sCmd);

    usleep(500000);
    char sResult[254];
    fcntl(fd, F_SETFL, FNDELAY); 

    if (!readport(fd,sResult)) {
        printf("read failed
");
        close(fd);
        return 1;
    }
    printf("readport=%s
", sResult);
    close(fd);
    return 0;
}


The lib.h contains read and write code as at:

Parse and read data frame in C?

and got the issue:

In order to test with serial port, I used the socat (https://help.ubuntu.com/community/VirtualSerialPort ) to create a pair serial ports on Linux and test my program with these port.

The first time the program sends the data and the program receives data is ok. However, if I read again or even re-write the new data into the serial port, the return data is always null until I stop the virtual serial port and start it again, then the write and read data is ok, but still, only one time.

(In the real case, the sending part will be done by another device, I am just taking care of the reading data from the serial port. I wrote both parts just to test my reading code.)

Does anyone have any ideas?

解决方案

Either your comment or your code is wrong:

// Set the baud rates to 19200... 
cfsetispeed(&options, B9600); 
cfsetospeed(&options, B9600); 

This says it will be setting the baud rate to 19200, but it really sets it to 9600. Maybe you want this:

// Set the baud rates to 19200... 
cfsetispeed(&options, B19200);
cfsetospeed(&options, B19200);

这篇关于在C中的Linux中清除串行端口的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆