PS: Ubuntu下找了好久没有找到像Windows中 vspd 那种能够模拟串口的工具。无意间在网上看到了别人用Python写的工具,试了一下挺好用的,这里记录一下。
python代码:
下载链接: virtualCom.py
#coding = utf-8
import pty
import os
import select
def mkpty():
master1, slave = pty.openpty()
slaveName1 = os.ttyname(slave)
master2, slave = pty.openpty()
slaveName2 = os.ttyname(slave)
print '\nslave device names: ', slaveName1, slaveName2
return master1, master2
if __name__ == "__main__":
master1, master2 = mkpty()
while True:
rl, wl, el = select.select([master1, master2], [], [], 1)
for master in rl:
data = os.read(master, 128)
print "read %d data." %len(data)
if master == master1:
os.write(master2, data)
else:
os.write(master1, data)
使用方式:
创建了12 和 13端口,我们用qt测试一下:
点击发送按钮,双方都可以收到数据:
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx