python - Python 'ser.write' 在 Windows 上永久停止程序,可能的错误?
问题描述
我在我的 python 代码中与 arduino 通信,串行传输多个字节。下面的代码是我的主要功能,直到第一个或第二个'ser.wrte'函数之后才起作用。令人困惑的是,该程序在 mac 上正常工作,没有错误,但任何笔记本电脑运行 Windows 时都存在上述问题(版本 7 和 10 已测试但未成功)是否有任何基于代码的原因,或者是由于与 Windows 相关串口问题?
#Communicates to arduino via serial port
def pump():
print('****************')
if len(seq) == 0:
print("EmptySeqError: please store patterns in the sequence before sending it!")
return
#######################
port = "COM17"
macport = "/dev/cu.usbmodem1411"
baudrate = 9600
#######################
try:
ser = serial.Serial(port, baudrate, timeout=0.1) #, write_timeout=0.1)
except serial.SerialException:
print('SerialException: cannot create serial object')
return
else:
if ser.isOpen():
print('serial port {} is opened successfully!'.format(ser.name))
for json in seq:
MSB = int(json['Hex Code MSB'], 16)
LSB = int(json['Hex Code LSB'], 16)
V = int(json['Voltage'], 16) #For voltage transmission
Vlsb = V & 0xff #Voltage LSB
Vmsb = (V>>8) & 0xff #Voltage MSB
print(V, 'in hex =', hex(V))
print(Vlsb, 'in hex =', hex(Vlsb))
print(Vmsb, 'in hex =', hex(Vmsb))
#X = int(json['Xhigh'], 16)
#Y = int(json['Ybase'], 16)
#Z = int(json['Zsec'], 16)
HD = int(json['High Delay'], 16)
LD = int(json['Low Delay'], 16)
print("**************")
print("(MSB/LSB) number of bytes written: ", ser.write(bytes([MSB, LSB])))
print("(Voltage) number of bytes written: ", ser.write(bytes([Vmsb,Vlsb])))
print("(HD/LD) number of bytes written: ", ser.write(bytes([HD, LD])))
print("serial is closing...")
sys.stdout.flush()
ser.close()
#End of sending
解决方案
推荐阅读
- node.js - 前端(SPA)和后端解耦:JWT 认证 + 持久登录
- php - Magento 2.3 非常慢,有超过 100 条购物车线
- angular - 数据未在角度选择器中动态加载
- python - 如何将python模块加载到codestar
- vba - 如何在 VBA for Word 中搜索/查找多种格式样式?
- bison - 如何用 flex 和 bison 打破以下循环依赖
- javascript - 具有可缩放 d3 圆形包装的工作代码库示例
- html - 在 ngb-carousel 中嵌入 youtube iframe
- python - 使用 Python 进行 Elasticsearch 数据插入
- python - 如何使用 Python 停止特定的 docker 容器