系统相关
首页 > 系统相关> > Linux下 虚拟串口的生成(程序)

Linux下 虚拟串口的生成(程序)

作者:互联网

将python的内容改为如下:


一、先新建一个文档,内容如下

#! /usr/bin/env python

#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)
            if master==master1:
                print "read %d data:" % len(data)
                print data
                #os.write(master2, data)
            else:
                print "to write %d data:" % len(data)
                print data.strip()
                os.write(master1, data.strip())

命名为:com.py

二、进入文件夹,运行程序

cd  VIns_Code/GVINS

python com.py

结果如下:

 当有数据向这个串口传输时,就会出现信息 !

标签:master1,__,master2,data,串口,虚拟,Linux,print,os
来源: https://blog.csdn.net/hltt3838/article/details/122419645