Continuously microcontroller UART data in Python

Hi,

How to receive UART data from microcontroller continuously in Python. It is sending around 150 characters every second.

Open the relevant serial device and read binary data from it. In a loop.

Untested sketch:

with open('/dev/ttyS0', 'rb') as ser:
    while True:
        bs = ser.read(2048)
        print(repr(bs))

It is actually a bit more complicated than that:

  • you need to ensure the serial port is configure to read the data
    correctly: bits per second, data bits, parity, stop bits, etc
  • you might need to ensure a nonblocking read, to collect what’s there

Cheers,
Cameron Simpson cs@cskk.id.au

I am able to run this command ‘ser = serial.Serial(“COM7”, 9600)’ but not able to run the one which you have suggested.

This is my program which does not work.

ser = serial.Serial(“COM7”, 9600)
with open(’/dev/COM7’, ‘rb’) as ser:
while True:
bs = ser.read(2048)
print(repr(bs))

I am able to run this command ‘ser = serial.Serial(“COM7”, 9600)’ but
not able to run the one which you have suggested.

This is my program which does not work.

ser = serial.Serial(“COM7”, 9600)

Ok, so that returns you a Serial object from the serial module. I do not
know where that module comes from - it is not part of the standard
library. What information do you have about it?

with open(’/dev/COM7’, ‘rb’) as ser:

This line of mine tries to open a serial device and return a file named
“ser”. Keep your line and remove my line - you only want one and you’ve
clearly got access to some kind of serial module.

On the premise/guess that a Serial instance looks somewhat like a file,
it probably stil has a read method. So keep the while-loop:

while True:
    bs = ser.read(2048)
    print(repr(bs))

but unindent it because it is no longer inside a with-statement.

If this fails, remember to cut/past the whole error message.

If you have documentation for the serial module, please also provide its
URL.

Finally, you can usually ask objects and classes what you can do with
them. At your interactive prompt (">>> " for me) go:

>>> from serial import Serial
>>> help(Serial)

and see what it says.

Cheers,
Cameron Simpson cs@cskk.id.au

It is working with the following code.

ser = serial.Serial(“COM7”, 9600)
while True:
bs = ser.read(512)
print(repr(bs))

But this prints everything including ‘\n’ and ‘\r’. How to filter ‘\n’ and ‘\r’ and then print ?

If you are reading binary data, you don’t want to filter out newlines

and carriage returns. If you do, you will corrupt the data.

If you are reading text:

bs = ser.read(512)  # read the data

bs = bs.replace(b'\n', b' ').replace(b'\r', b' ')

That replaces newlines and carriage returns with spaces. If you want to

replace them with something else, change the b’ ’ to the bytes you want

to use instead. To delete them, use b’’ (empty bytes) instead.

If you get this error:

TypeError: replace() argument 1 must be str, not bytes

then change the b’\n’ to ‘\n’ etc:

bs = bs.replace('\n', ' ').replace('\r', ' ')

This statement works. Thanks for the help.

bs = bs.replace(b'\n', b' ').replace(b'\r', b' ')

Out of interest, I’ve seen a few postis here of students using this
serial module to talk things. Does anyone know which module they’re
using and where it comes from?

Cheers,
Cameron Simpson cs@cskk.id.au

I assume they’re referring to pyserial.

Ah, thanks. - Cameron Simpson cs@cskk.id.au