71 lines
1.6 KiB
Python
71 lines
1.6 KiB
Python
# test seriale
|
|
import serial
|
|
import time
|
|
|
|
from datetime import datetime
|
|
|
|
# constants
|
|
waitReconn=2
|
|
sampleTime=0.20
|
|
|
|
def doConnect():
|
|
|
|
global ser
|
|
|
|
|
|
try:
|
|
if ser.isOpen():
|
|
ser.close()
|
|
|
|
ser = serial.Serial("COM4",
|
|
baudrate=9600,
|
|
bytesize = 8,
|
|
timeout=1
|
|
)
|
|
print('serial opened!')
|
|
except Exception as exc:
|
|
print(f'Exception serial.init.01\r\n{str(exc)}')
|
|
time.sleep(waitReconn)
|
|
|
|
print('---------------\r\nStart Serial\r\n---------------')
|
|
ser = serial.Serial("COM4",
|
|
baudrate=9600,
|
|
bytesize = 8,
|
|
timeout=1
|
|
)
|
|
doConnect()
|
|
|
|
cmdCall='$i\r\n'
|
|
serCall=cmdCall.encode()
|
|
|
|
while True:
|
|
|
|
try:
|
|
if (ser.is_open):
|
|
ser.reset_input_buffer()
|
|
ser.write(serCall)
|
|
|
|
time.sleep(sampleTime)
|
|
|
|
sRaw = ser.read(12).decode()
|
|
if len(sRaw)> 4:
|
|
lAns = sRaw.split('\r')
|
|
val=lAns[1]
|
|
|
|
# val = x.replace(cmdCall,'')
|
|
dtEve = datetime.utcnow().strftime('%H:%M:%S')
|
|
print(f'{dtEve} | Read: {val}')
|
|
else:
|
|
print('no data')
|
|
time.sleep(waitReconn)
|
|
doConnect()
|
|
|
|
else:
|
|
doConnect()
|
|
|
|
except Exception as exc:
|
|
print(f'Exception read.01\r\n{str(exc)}')
|
|
time.sleep(waitReconn)
|
|
doConnect()
|
|
|
|
ser.close |