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


i'm having trouble read more 1 character using program, cant seem figure out went wrong program, i'm new python.

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:     line in ser.read():          print(str(count) + str(': ') + chr(line) )         count = count+1  ser.close() 

here results get

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

actually expecting this

connected to: com5 1:12431 2:12431 

something above mentioned able read multiple characters @ same time not 1 one.

i see couple of issues.

first:

ser.read() going return 1 byte @ time.

if specify count

ser.read(5) 

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

if know input terminated eol characters, better way use

ser.readline() 

that continue read characters until eol received.

second:

even if ser.read() or ser.readline() return multiple bytes, since iterating on return value, still handling 1 byte @ time.

get rid of the

for line in ser.read(): 

and say:

line = ser.readline() 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -