赞
踩
二话不说先上效果图:
开发流程:
1、电压采集
使用某宝上电压采集模块,该模块支持RS485连接上位机;
根据采集模块Moubus通讯规则,使用python 编写获取电压值,上代码
- # encoding: utf-8
- '''
- @author: halen.li
- @contact: 1056008549@qq.com
- @file: GET_V.py
- @time: 2021/6/26 0026 8:05
- @desc:
- '''
- import serial
- import serial.tools.list_ports
- import re
- from time import sleep
- import struct
-
- def GetCom():
- port_list=list(serial.tools.list_ports.comports())
- if len(port_list)==0:
- print("NO Find COM Port")
- else:
- for i in range(0,len(port_list)):
- matchport = re.match(r'(.*) - USB-SERIAL (.*?).*',str(port_list[i]),re.M|re.I)
- return (matchport.group(1))
-
-
- def OpenCom(portx):
- try:
- ser = serial.Serial(portx, 9600, timeout=1)
- if (ser.is_open):
- WritePort(ser)
- sleep(0.1)
- len_return_data = ser.inWaiting()
- if len_return_data:
- return_data = ser.read(len_return_data)
- str_return_data = str(return_data.hex())
- voltage_data = str_return_data[6:14]
- result = struct.unpack('!f', bytes.fromhex(voltage_data))[0]
- ser.close()
- return(round(result,3))
- ser.close()
- except Exception as e:
- print("Open com error:",e)
- def WritePort(ser):
- data = '01 03 00 00 00 02 C4 0B'
- send_data = bytes.fromhex(data)
- ser.write(send_data)
-
- def GetVoltage():
- portx = GetCom()
- Vol = []
- while True:
- Vol_v = OpenCom(portx)
- if Vol_v != None and Vol_v > 0.1:
- Vol.append(Vol_v)
- if len(Vol) == 5:
- break
- else:
- break
- return Vol
2、界面开发
使用QtDesigner设计界面
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。