linux虚拟串口

linux虚拟串口(用于调试程序)

1 将下列Python代码保存成VitrualCom.py:

#! /usr/bin/env python

#coding=utf-8



import pty
import os
import select



def mkpty():
    # open virtual port
    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)

2 在终端下面输入:

python VirtualCom.py &

3 便会显示:

slave device names:  /dev/pts/1 /dev/pts/2

这样/dev/pts/1与/dev/pts/2串口便相连接在一起了。

然后就可以利用这两个设备名在本机上进行虚拟串口之类的调试,使用完后用ps查看这个python进程的pid号,然后kill掉即可。

出处 http://blog.sina.com.cn/s/blog_6cb543ef0100x90j.html

你可能感兴趣的:(ubuntu)