ORSSerialPort 中的虚拟串口

Virtual serial ports in ORSSerialPort

我正在使用 ORSSerialPort. In some occasions, I would like to control a serial device that is not directly connected to my machine, or mock the communication of such device locally for testing. Using socat 重写一个 OSX 控制应用程序,它可以创建虚拟串行端口,例如从本地或网络上的真实串行设备进行通信。

比如我这里创建一对虚拟设备/dev/master/dev/slavesudo socat -d -d -d -d -lf /tmp/socat pty,link=/dev/master,ixoff=0,ixon=0,ispeed=9600,ospeed=9600,echo=0,crtscts=0,user=gerwin,group=staff pty,link=/dev/slave,rawer,echo=0,user=gerwin,group=staff

使用这样的设置,我可以使用 pySerial 连接到虚拟端口。但是,使用 ORSSerialPort 对我不起作用。

单步执行代码时,这似乎源自 ORSSerialPort,它完全依赖 IOKit,其中 'virtual' 设备没有出现在它的雷达上。同样在使用路径初始化实例时,例如/dev/master,对应的 io_object_t 不存在——并且初始化 returns nil。

我想避免深入研究 IOKit 和内核内容 (as suggested here)。

查看代码,我发现在代码的深处,sendData 写入文件描述符。是否有任何可行的方法可以使用 'plain' 文件描述符初始化 ORSSerialPort,跳过详细的 ioctl 设置,并将其视为纯字符流(假设这是 pySerial 实现它的方式)。还有其他选择吗?


2016 年 2 月 10 日更新:

我在 ORSSerialPort github issue list 上与创作者 armadsen 讨论了情况:

I think the basic approach is to "swap" the initializers in InternalSerialPort and SerialPort so that the path initializer is the designated/required one, and the IOKit initializer is the optional one. It's already the case (as you've noted) that the path (not the io_object_t) is used internally to get a file descriptor and read/write to/from the port. In other words, my hope is that this is not a terribly complicated change.

我在下面发布了最终结果的答案...


2016 年 2 月 10 日更新:

我在 ORSSerialPort github issue list 上与创作者 armadsen 讨论了情况:

I think the basic approach is to "swap" the initializers in InternalSerialPort and SerialPort so that the path initializer is the designated/required one, and the IOKit initializer is the optional one. It's already the case (as you've noted) that the path (not the io_object_t) is used internally to get a file descriptor and read/write to/from the port. In other words, my hope is that this is not a terribly complicated change.

在此之后,我做了一个概念验证实现,适用于 "just filedescriptors" 和 socat,可以在这里找到:

https://github.com/gerwindehaan/ORSSerialPort/tree/3.0