Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Serial: How to use the read or readline function to read more than 1 character at a time

I'm having trouble to read more than one character using my program, I can't seem to figure out what went wrong with my program.

import serial

ser = serial.Serial(
    port='COM5',\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)

print("connected to: " + ser.portstr)
count=1

while True:
    for line in ser.read():

        print(str(count) + str(': ') + chr(line) )
        count = count+1

ser.close()

here are the results I get

connected to: COM5
1: 1
2: 2
3: 4
4: 3
5: 1

actually I was expecting this

connected to: COM5
1:12431
2:12431

something like the above mentioned which is able read multiple characters at the same time not one by one.

like image 263
user2294001 Avatar asked Apr 18 '13 08:04

user2294001


People also ask

How do you read data serially in Python?

Python Serial Read vs Readlineread() will return one byte at a time. serial. readline() will return all bytes until it reaches EOL. If an integer is specified within the function, it will that return that many bytes.

Is Pyserial the same as serial?

PySerial is a library which provides support for serial connections ("RS-232") over a variety of different devices: old-style serial ports, Bluetooth dongles, infra-red ports, and so on.

What does Pyserial flush do?

"Flush input buffer, discarding all its contents." Typically only used after changing the serial port parameters (e.g. port initialization) or for error recovery.


3 Answers

I see a couple of issues.

First:

ser.read() is only going to return 1 byte at a time.

If you specify a count

ser.read(5)

it will read 5 bytes (less if timeout occurrs before 5 bytes arrive.)

If you know that your input is always properly terminated with EOL characters, better way is to use

ser.readline()

That will continue to read characters until an EOL is received.

Second:

Even if you get ser.read() or ser.readline() to return multiple bytes, since you are iterating over the return value, you will still be handling it one byte at a time.

Get rid of the

for line in ser.read():

and just say:

line = ser.readline()
like image 121
jwygralak67 Avatar answered Oct 22 '22 11:10

jwygralak67


I use this small method to read Arduino serial monitor with Python

import serial
ser = serial.Serial("COM11", 9600)
while True:
     cc=str(ser.readline())
     print(cc[2:][:-5])
like image 37
Thushara Madushan Avatar answered Oct 22 '22 11:10

Thushara Madushan


Serial sends data 8 bits at a time, that translates to 1 byte and 1 byte means 1 character.

You need to implement your own method that can read characters into a buffer until some sentinel is reached. The convention is to send a message like 12431\n indicating one line.

So what you need to do is to implement a buffer that will store X number of characters and as soon as you reach that \n, perform your operation on the line and proceed to read the next line into the buffer.

Note you will have to take care of buffer overflow cases i.e. when a line is received that is longer than your buffer etc...

EDIT

import serial

ser = serial.Serial(
    port='COM5',\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)

print("connected to: " + ser.portstr)

#this will store the line
line = []

while True:
    for c in ser.read():
        line.append(c)
        if c == '\n':
            print("Line: " + ''.join(line))
            line = []
            break

ser.close()
like image 12
1337holiday Avatar answered Oct 22 '22 13:10

1337holiday