Reading from serial port

Hi, I’m looking for help on how to trigger an action when data is received on a serial port. I’ve got a sensor that reads the serial port successfully, but if I understand correctly, a sensor polls the source at a specific interval. Is there any way to cause an action to be performed as soon as data appears on the port as opposed to polling?

Thanks

Stan

Based on the fact the Serial integration uses pyserial-asyncio, I doubt it only receives data at specific intervals.

In addition, there’s no reference to SCAN_INTERVAL in the Serial sensor’s source-code.

Interesting. I’ll see if I can set up a test to confirm.

Oh. I didn’t realize you hadn’t actually tried the Serial integration. Pretty sure your test will confirm the data is received in real-time.

ok, some progress, but not much. I can’t seem to get the serial sensor to read the data from the serial port, and I think it’s because the data I’m reading does not have a new line character. The lines are all terminated with a ‘\r’ but not a ‘\n’. If I’m right, is there any way to fix this?

Thanks

Stan

I recall another post where the lack of a terminating newline was the reason why the data wasn’t received. In that particular case, data was transmitted from an Arduino. The person had access to the source code and simply modified it.

Unless you have the ability to modify how the information is transmitted, I don’t see how you can get around the requirement for a terminating newline.

Thanks for the confirmation. Maybe I’ll submit a feature request. Assuming that the HA code uses pyserial, it should be really easy to add.

It uses the readline() method which expects a newline to serve as EOL (end of line).


To meet your requirements, it would have to forego using that convenient method and read the received data, one byte at a time, into a buffer until the desired EOL is reached (a carriage return in your case). Then the buffer contents are read and flushed.

If you are handy with python, you can alter the code yourself. If not, you can submit a feature request but be aware that it may never be fulfilled (they’re not on any roadmap and only get done if they attract a developer’s attention).