C++ - 在没有物理设备的情况下测试串口

标签 c++ serial-port pty virtual-serial-port

我有一个程序可以将串行设备拆分为多个虚拟串行端口并将所有数据路由到它们。

                    ---- /dev/ttyS1.a [data]->
                    |
[data]-> /dev/ttyS1 ---- /dev/ttyS1.b [data]->
                    |
                    ---- /dev/ttyS1.c [data]->

我的工作程序(为了可读性和简单性而编写的伪代码):

poll(...) {
    // Route data from master to vsp
    master.read(buf)
    for(virtual serial ports as vsp) {
        vsp.write(buf)
    }
    // Route data from vsp to master (if need be)
    for(virtual serial ports as vsp) {
        if(vsp.needs_to_write()) {
            vsp.read(buf)
            master.write(buf)
        }
    }
}

我的机器上有一个物理串行端口设备,它不断地提供数据,这就是我测试我的程序最初是否工作的方式,但我想编写一个测试来模拟/模拟写入和读取两个方向并验证两端的数据。由于我从我的物理串行端口设备接收到的数据写入看似随机的数据,因此很难验证正在写入的数据是否正是正在写入的数据。

我怎样才能做到这一点? (伪代码)

1. fork process that feeds a known char sequence into /dev/ttyS2 in a loop
2. use my program to read from the COM `master.read(buf)` and then write to the vsp `vsp.write(buf)`
3. how can I verify that after writing to the buf that the vsp has the correct data?

感谢任何帮助我对如何自动测试这个感到困惑。

编辑 1: 没人能帮忙吗?

最佳答案

我觉得你可以用com0com来测试。您可以连接虚拟串行端口,然后写入一个并从其他端口读取它收到的内容。 enter link description here

关于C++ - 在没有物理设备的情况下测试串口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22202950/

相关文章:

windows - 自定义波特率

ubuntu - 通过串口发送一个 300-400 毫秒的脉冲

python - 带有类型推导的 swig python 模板函数

c++ - Eclipse 无法识别我的 GCC

c - 如何在 C 语言中通过串行端口发送 0xff 字节?

c - 为什么从伪终端读取失败?

C/Linux 编程 : Pseudo-Terminals: how to redirect from current stdio to pty and redirect back after usage

c++ - 传递 C++ 对象以便 Lua 可以使用它

c++ - 用值替换字符串中的特定字符

c++ - 当调用 forkpty 时,X 会抛出错误。 (C++)