⽤python虚拟串⼝
在linux下调试串⼝程序,⽆奈下⾯的硬件还没到位,所以,想着⾃⼰模拟⼀个串⼝⽤⽤。试了下下⾯这段代码:
#!/usr/bin/env python
#coding=utf-8
import pty
import os
import select
def mkpty():
#Open a new tty
master1, slave = pty.openpty()
python怎么读取串口数据
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=read list, wait until ready to reading
#      wl=write list, wait until ready to writing
#      el=exception list, wait for an "exceptional condition"
#      timeout = 1s
rl, wl, el = select.select([master1, master2], [], [], 1)
for device in rl:
data = os.read(device, 128)
print"read %d data."%len(data)
if device == master1:
os.write(master2, data)
else:
os.write(master1, data)
pty是假串⼝的意思,但是⽀持硬件串⼝的所有操作。so。。。
另外⼀个,模拟同事通过串⼝发来的数据。所有写到master的数据,都被⾃动的发往slave。所以,我们在slave这边就可以收到想要的数据。
#!/usr/bin/env python
#coding=utf-8
import pty
import os
import time
import array
import random
def mkpty():
#make pair of pseudo tty
master, slave = pty.openpty()
slaveName = os.ttyname(slave)
print'\nslave device names:', slaveName
return master
if__name__ == "__main__":
master = mkpty()
buf = array.array('B', [0] * 7)
buf[0] = 0x00
buf[1] = 0x02
buf[2] = 0x8a
buf[3] = 0x2d
buf[4] = 0xc5
buf[5] = 0x3f
buf[6] = 0x00
while True:
if buf[1] < 40:
buf[1] = buf[1] + 1
else:
buf[1] = 1
buf[0] = buf[0] + 1
if buf[0] == 255:
buf[0] = 0
#        buf[5] = random.randint(40,50)
buf[2] = random.randint(0,250)
buf[6] = ( buf[0]+buf[1]+buf[2]+buf[3]+buf[4]+buf[5]) %256        os.write(master,  buf)
#        print buf
time.sleep(0.02)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。