Reduce serial reading to once a second, but then read all available lines

This commit is contained in:
Ollo 2025-01-06 11:45:51 +01:00
parent 10bbb68dc0
commit 5b9e8a3754

View File

@ -24,6 +24,8 @@ serial_device=os.environ['SERIAL_DEVICE']
try:
if (os.environ['PY_LOGGING']):
customLevel = os.environ['PY_LOGGING'].upper()
else:
customLevel = 'ERROR'
except (KeyError):
customLevel = 'WARNING'
@ -77,6 +79,7 @@ def on_message(client, userdata, msg):
def on_connect(client, obj, flags, reason_code, properties):
client.subscribe(mqtt_topic + "cmd/led")
client.subscribe(mqtt_topic + "cmd/rpm")
client.publish(mqtt_topic + "logging", payload=customLevel)
client.publish(mqtt_topic + "online", payload="true", qos=1, retain=True)
logging.debug("Connected: " + str(reason_code))
@ -122,6 +125,14 @@ try:
try:
line = ser.readline() # read a '\n' terminated line
logging.debug("Read one line: " + str(line) )
if (ser.in_waiting > 0):
for i in range(10):
if (ser.in_waiting > 0):
line = ser.readline() # read a '\n' terminated line
logging.debug("" + str(i+1) + ". line: " + str(line) )
else:
logging.debug("No additional lines")
except (serial.SerialException):
SerialCommunicationFailureCounter = SerialCommunicationFailureCounter + 1
client.publish(mqtt_topic + "serialcom/failure", str(SerialCommunicationFailureCounter))
@ -194,9 +205,9 @@ try:
if (parseError == False):
client.publish(mqtt_topic + "debug", None)
else:
client.publish(mqtt_topic + "debug", line) # line was invalid and could not be parsed
else:
time.sleep(0.5)
client.publish(mqtt_topic + "debug", "invalid:" + str(l)) # line was invalid and could not be parsed
# Always wait between two cycles
time.sleep(0.9)
except (KeyboardInterrupt, SystemExit):
logging.error("User aborted service ...")