业务上是 有两个按钮 一个是打开 一个是关闭
记得先看仪器的说明说调成一直循环向电脑发送数据
误区:一开始在关闭的时候总想着杀死线程也找到了方案. 后来看杀死线程处理不太友好.
所以加了off来控制线程的存活
没写注释是英文起的都非常通俗易懂 比较难看懂的英语已经用拼音来了.
我也建议. 如果开发时候碰到一些自己都不明白的单词 建议用拼接吧.
变量名的存在就是让人看起来通俗易懂 如果你自己都不明白什么意思反而用了不恰当的单词还不如拼音
import serial
import serial.tools.list_ports
import threading
from time import sleep
class EletronicScale(object):
"""
称重仪
"""
def __init__(self):
self.com = None
self.serial = None
self.off = None
def open_port(self):
if self.off != None and self.off != False:
print("已连接请勿重复连接")
return
try:
self.port_list = list(serial.tools.list_ports.comports())
for prot in self.port_list:
if prot.vid == 6790:
self.com = prot.device
if self.com != None:
self.serial = serial.Serial(self.com, 9600, timeout=0.5)
self.off = True
t = threading.Thread(target=self.xunhuanjieshoushuju)
t.start()
print("连接成功")
else:
print("请连接称重仪")
except Exception as e:
raise e
def xunhuanjieshoushuju(self):
while True:
if self.off == False:
break
data = self.serial.read_all()
if len(data) > 10:
print("Weight%d" % (float(data.decode("utf8")[1:8]))
sleep(0.02)
def threading_data(self):
t = threading.Thread(target=self.xunhuanjieshoushuju)
t.start()
def close_port(self):
self.off = False
sleep(0.3)
self.serial.close()
print("关闭成功")
if __name__ == '__main__':
ele = EletronicScale()
ele.open_port()
ele.serial.isOpen()
t = threading.Thread(target=ele.xunhuanjieshoushuju)
t.start()
while True:
cur = int(input("1111"))
if cur == 2:
ele.off = False
sleep(0.3)
ele.close_prot()
print("关闭端口")
print(t.is_alive())
if cur == 1:
ele.off = True
ele.open_port()
t = threading.Thread(target=ele.xunhuanjieshoushuju)
t.start()
print("开启线程")
print(t.is_alive())
python对接称重仪(获取串口数据)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.