cul_seriallog.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/python3 -u
  2. #
  3. # pip3 install pyyaml paho-mqtt pyserial
  4. import serial
  5. import sys
  6. from time import sleep
  7. import datetime
  8. import paho.mqtt.client as mqtt
  9. import json
  10. #import simplejson as json
  11. serialPort = '/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0'
  12. serialBaud = 38400
  13. serialTimeout = 1
  14. culInitCmd = 'X21\r\n' # CUL init command for normal operation
  15. culInitTimeout = 3
  16. verbosemode = True
  17. #try:
  18. serLine = ""
  19. ser = serial.Serial(port=serialPort,baudrate=serialBaud,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=serialTimeout)
  20. sleep(culInitTimeout)
  21. ser.write('V\r\n'.encode('utf-8')) # get CUL version info
  22. serLine = ser.readline()
  23. print(serLine.decode('utf-8').rstrip('\r\n'))
  24. sleep(0.1)
  25. print('Initializing CUL with command: ' + culInitCmd.rstrip('\r\n'))
  26. ser.write(culInitCmd.encode('utf-8')) # den CUL im normalen Mode initialisieren
  27. while True:
  28. serLine = ser.readline()
  29. if len(serLine) > 0:
  30. serLine = serLine.decode('utf-8')
  31. serLine = serLine.rstrip('\r\n')
  32. isValidCommand = False
  33. if serLine[:2] == 'is': # msg is reply from CUL to send command
  34. isValidCommand = False
  35. # now do nothing...
  36. elif serLine[:1] == 'i': # is a IT compatible command
  37. isValidCommand = True
  38. inCmd = serLine[:-2] # remove last 2 bytes, as this is only signal strength info
  39. if isValidCommand:
  40. sys.stdout.write('[')
  41. sys.stdout.write(str(datetime.datetime.now()).split('.')[0])
  42. sys.stdout.write('] ')
  43. print(inCmd)
  44. elif verbosemode:
  45. sys.stdout.write('[')
  46. sys.stdout.write(str(datetime.datetime.now()).split('.')[0])
  47. sys.stdout.write('] ')
  48. print(serLine)
  49. #except KeyboardInterrupt, e:
  50. # print('\n')