# section: main {{{1
######################################################
# section: serial {{{2
-lsser=serial_ports()
-ser=serial.Serial()
-ser.port=lsser[0].device
-ser.baudrate=9600
-ser.parity=serial.PARITY_NONE
-ser.stopbits=serial.STOPBITS_ONE
-ser.bytesize=serial.EIGHTBITS
-#ser.timeout=40 # msec
-ser.open()
-print("Connected to",repr(ser.portstr))
-print("Press Ctrl-C to interrupt")
+#lsser=serial_ports()
+#ser=serial.Serial()
+#try:
+# ser.port=lsser[0].device
+# ser.open()
+# print("Connected to",repr(ser.portstr))
+# print("Press Ctrl-C to interrupt")
+#except:
+# print("Unable to connect to serial port")
+# exit(0)
-telegram=b'\x27\x0D\x22\x20\x0B\x03\x34\x35\x70\x02\x04\xBE\x71\x72\x73\xE5\x5B'
-DL_IDLE=0
-DL_SENDING=1
-DL_RECEIVING=2
-state=DL_IDLE
+telegram=b'\x10\x33\x27\x0D\x22\x20\x0B\x03\x34\x35\x70\x02\x04\xBE\x71\x72\x73\xE5\x5B'
frame=bytearray()
+count=0
index=0
try:
while True:
-# byte=bytes([telegram[index]])
- byte=ser.read()
+ byte=bytes([telegram[index]])
+ index=index+1
+# byte=ser.read()
# print(byte)
- frame.extend(byte)
- if index==1:
- framelen=ord(byte)+4
- numbytes=framelen-1
- state=DL_RECEIVING
- elif index==0:
-# print("START")
- pass
- if state==DL_RECEIVING:
-# print(numbytes)
- numbytes=numbytes-1
- if numbytes==0:
+ if count==0:
+# timestamp=time.timestamp.now()
+ if byte==b'\x27' or byte==b'\x26' or byte==b'\x24':
+ print("START")
+ frame.extend(byte)
+ count=count+1
+ else:
+ print("FLUSHING")
+ else: # receiving
+ frame.extend(byte)
+ count=count+1
+ if count == frame[1]+4:
if crc.verifyCrc(frame):
- print("CRC ok")
+ print("CRC OK")
process_telegram(frame[:-2])
else:
- print("CRC error")
- state=DL_IDLE
+ print("CRC ERR")
frame=bytearray()
+ count=0
index=0
- continue # frame done
- index=index+1
except KeyInterrupt:
print("Interrupted")
+
ser.close()
print("Disconnected")