Skip to content

Instantly share code, notes, and snippets.

@danielkucera
Created January 28, 2024 23:04
Show Gist options
  • Save danielkucera/0496537760f619b59ce62c7e17d63e0c to your computer and use it in GitHub Desktop.
Save danielkucera/0496537760f619b59ce62c7e17d63e0c to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import serial
import time
echo_back=False
#echo_back=True
ser = serial.Serial('/dev/ttyUSB0',
300,
timeout=2,
bytesize=serial.SEVENBITS,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE ) # open serial port
START = b'/'
END = b'!'
SOH = b'\x01'
STX = b'\x02'
ETX = b'\x03'
EOT = b'\x04'
ACK = b'\x06'
NAK = b'\x15'
def send_check(data):
print("w", data.hex())
ser.write(data) # write a string
if echo_back:
s = ser.readline()
print("r", s.hex())
if data != s:
print("Echo back failed:", s, s.hex())
#return False
return True
else:
time.sleep((1/ser.baudrate)*9*len(data)+0.1)
return True
# B1 Wakeup
def wakeup():
start = time.time()
while time.time() < start + 2.2:
send_check(b'\00')
time.sleep(1.5)
# 6.4.3.1
def data_readout():
# send ACK and set mode
if not send_check(ACK + b"050\r\n"):
raise("ACK failed")
ser.baudrate = 9600
s = ser.read_until(expected=ETX, size=None)
print(s[1:-1].decode("utf-8"), s.hex())
send_check(SOH + b"\xb0" + ETX + b"q")
#wakeup()
if not send_check(b"/?!\r\n"):
raise("start failed")
# readback ID
s = ser.readline()
print(s, s.hex())
if len(s) < 1:
#raise("no id received")
pass
data_readout()
ser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment