You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
3.8 KiB
144 lines
3.8 KiB
4 months ago
|
import sys
|
||
|
from helper.SerialPort import *
|
||
|
from collections import Counter
|
||
|
import time
|
||
|
import threading
|
||
|
|
||
|
from conf import setting
|
||
|
|
||
|
# 天平收发数据文件
|
||
|
|
||
|
|
||
|
class Balance:
|
||
|
lockSend=threading.Lock()
|
||
|
def __init__(self):
|
||
|
self.ser = SerialPort(setting.BALANCE_SERIAL, 9600)
|
||
|
# self.ser = SerialPort('com5', 9600)
|
||
|
self.balanceData = 0
|
||
|
|
||
|
# # 启动服务
|
||
|
# def start(self):
|
||
|
# self.balanceData = 0
|
||
|
# self.taskKillFlag = False
|
||
|
# p = threading.Thread(target=self.readDataThread)
|
||
|
# p.start()
|
||
|
#
|
||
|
# # 停止服务
|
||
|
# def stop(self):
|
||
|
# self.taskKillFlag = True
|
||
|
# self.balanceData = 0
|
||
|
|
||
|
def readDataThread(self):
|
||
|
while not (self.taskKillFlag):
|
||
|
try:
|
||
|
dataStr = self.readRealTimeVal()
|
||
|
if (dataStr):
|
||
|
if (dataStr[0] == '+'):
|
||
|
self.balanceData = float(dataStr[1:])
|
||
|
else:
|
||
|
self.balanceData = float(dataStr[1:]) * -1
|
||
|
else:
|
||
|
self.balanceData = -999999
|
||
|
|
||
|
time.sleep(0.1)
|
||
|
except Exception as e:
|
||
|
print(str(e))
|
||
|
|
||
|
def getData(self):
|
||
|
try:
|
||
|
dataStr = self.readRealTimeVal()
|
||
|
if (dataStr):
|
||
|
if (dataStr[0] == '+'):
|
||
|
self.balanceData = float(dataStr[1:])
|
||
|
else:
|
||
|
self.balanceData = float(dataStr[1:]) * -1
|
||
|
else:
|
||
|
self.balanceData = -999999
|
||
|
except Exception as e:
|
||
|
print(str(e))
|
||
|
self.balanceData = -999999
|
||
|
return self.balanceData
|
||
|
|
||
|
def readRealTimeVal(self):
|
||
|
try:
|
||
|
data = None
|
||
|
content = self.write('001?\r\n')
|
||
|
#print('11111')
|
||
|
#print(content)
|
||
|
if content:
|
||
|
list_data = content.split('\r\n')
|
||
|
# print(list_data)
|
||
|
for i in list_data:
|
||
|
if "WT:" in i:
|
||
|
data = i
|
||
|
break
|
||
|
if data:
|
||
|
data = data.replace(' ', '')
|
||
|
data = data.replace('WT:', '')
|
||
|
data = data.replace('g', '')
|
||
|
if '-' not in data:
|
||
|
data = '+' + data
|
||
|
#print(data)
|
||
|
return data
|
||
|
|
||
|
else:
|
||
|
return data
|
||
|
except Exception as e:
|
||
|
print(str(e))
|
||
|
|
||
|
# 去皮
|
||
|
def tare(self):
|
||
|
self.write('T\r\n')
|
||
|
|
||
|
# 归零
|
||
|
def zero(self):
|
||
|
self.write('Z')
|
||
|
|
||
|
def write(self, data):
|
||
|
try:
|
||
|
self.lockSend.acquire()
|
||
|
self.ser.Write(data.encode())
|
||
|
#print(data)
|
||
|
content = bytes.decode(self.ser.Read())
|
||
|
# print(content)
|
||
|
return content
|
||
|
except Exception as e:
|
||
|
print(str(e))
|
||
|
return None
|
||
|
finally:
|
||
|
self.lockSend.release()
|
||
|
|
||
|
def read(self):
|
||
|
# 过滤天平串口返回
|
||
|
content = self.write('001?\r\n')
|
||
|
print(content)
|
||
|
if content:
|
||
|
list_data = content.split('\r\n')
|
||
|
# print(list_data)
|
||
|
data = None
|
||
|
for i in list_data:
|
||
|
if "WT:" in i:
|
||
|
data = i
|
||
|
break
|
||
|
if data:
|
||
|
data = data.replace(' ', '')
|
||
|
data = data.replace('WT:', '')
|
||
|
data = data.replace('g', '')
|
||
|
if '-' not in data:
|
||
|
data = '+' + data
|
||
|
print(data)
|
||
|
return data
|
||
|
|
||
|
else:
|
||
|
return None
|
||
|
|
||
|
balance = Balance()
|
||
|
# if __name__ == '__main__':
|
||
|
# # balance.write('#')
|
||
|
# balance.tare()
|
||
|
# while 1:
|
||
|
# data = balance.getData()
|
||
|
# print(data)
|
||
|
# time.sleep(1)
|
||
|
# # print(a.readRealTimeVal())
|