1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #!/usr/bin/python3 -u
- #
- # pip3 install pyyaml paho-mqtt pyserial
- import serial
- import sys
- from time import sleep
- import datetime
- import paho.mqtt.client as mqtt
- import json
- #import simplejson as json
- serialPort = '/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0'
- serialBaud = 38400
- serialTimeout = 1
- culInitCmd = 'X21\r\n' # CUL init command for normal operation
- culInitTimeout = 3
- verbosemode = True
- #try:
- serLine = ""
- ser = serial.Serial(port=serialPort,baudrate=serialBaud,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=serialTimeout)
- sleep(culInitTimeout)
- ser.write('V\r\n'.encode('utf-8')) # get CUL version info
- serLine = ser.readline()
- print(serLine.decode('utf-8').rstrip('\r\n'))
- sleep(0.1)
- print('Initializing CUL with command: ' + culInitCmd.rstrip('\r\n'))
- ser.write(culInitCmd.encode('utf-8')) # den CUL im normalen Mode initialisieren
- while True:
- serLine = ser.readline()
- if len(serLine) > 0:
- serLine = serLine.decode('utf-8')
- serLine = serLine.rstrip('\r\n')
-
- isValidCommand = False
-
- if serLine[:2] == 'is': # msg is reply from CUL to send command
- isValidCommand = False
- # now do nothing...
- elif serLine[:1] == 'i': # is a IT compatible command
- isValidCommand = True
- inCmd = serLine[:-2] # remove last 2 bytes, as this is only signal strength info
-
- if isValidCommand:
- sys.stdout.write('[')
- sys.stdout.write(str(datetime.datetime.now()).split('.')[0])
- sys.stdout.write('] ')
- print(inCmd)
- elif verbosemode:
- sys.stdout.write('[')
- sys.stdout.write(str(datetime.datetime.now()).split('.')[0])
- sys.stdout.write('] ')
- print(serLine)
-
- #except KeyboardInterrupt, e:
- # print('\n')
|