Click here to Skip to main content
15,888,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python

Hi,

I am trying to read some serial data (a string at this stage) in Python and I can send a string (" hello world! ") and am now trying to read a string.

[edit]Added Python tag - OriginalGriff[/edit]

What I have tried:

at this stage:
Python
res =ser.readlines()
print(res)

previously:
Python
res =ser.read()
print(res)

which produces the first character of what is sent, I was thinking looping until the whole string is read...
Python
stringlength = len ser.res
for i in range(stringlenght)

Just tried the below:
Python
res =ser.read(5)
print(res)
The issue with this is firstly you have to know how many characters are to be read and also it produces a leading b
Quote:
b'hello'
I'm guessing that means binary coded and the ' tags could cause issues I am assuming there is a way to suppress this? Also further fiddling has lead to trying to loop until it see /n...
Posted
Updated 14-Aug-18 3:43am
v4
Comments
glennPattonWork3 6-Aug-18 5:14am    
Cheers Griff

See Short introduction — pySerial 3.4 documentation[^] and pySerial API — pySerial 3.4 documentation[^].

Serial.read(size=1) reads the specified number of bytes which is one by default. That is why you get only one character.

readline() reads until a new line character is received. This function can't be used when the other side is not responding with new line terminated answers. It requires also to specify a timeout when openening the port.

readlines() calls readline() until an EOF occurs. But such never happens with serial ports so that the function terminates when the timeout has elapsed.

It is a general problem of serial communication to detect the end of a "package" when those does not have a fixed size.

The common method when using strings is using the new line character or a character not occuring in strings like a null byte as end of package indicator.

Binary data can be prefixed with a header of fixed size containing the length of the data. So the receiver knows how many data bytes he has to expect. A receiver reads than the header bytes first and the remaining payload bytes afterwards.

Conclusion:
You have to know the used data format (called protocol) or define it when implementing both communication sides.
 
Share this answer
 
A bit of fiddling and this does pretty much what I want (got me out of the hole I was in):
Python
#Will send and read from Com1 13/08/2018  

import serial
import time

#Set up Serial Port 1 to send and print out com port 
ser = serial.Serial(0)
ser.baudrate = 9600
ser.parity = serial.PARITY_NONE
ser.BYTESIZES = serial.EIGHTBITS
ser.stopbits = serial.STOPBITS_ONE
print(ser.portstr)
ser.close()


def Send():
    ser.open()
    #write text hello world to serial port
    print('Type a string:')
    text = input()
    ser.write(text.encode())
    ser.close()

def Receive():
    #open serial port and read characters from it
    ser.open()
    print('Receiving')
    res =ser.readline()
    res = res[:-2]
    print(res)
    ser.close()


while True:
    usrInput = input("Choose A: to receive, B: to Send")
    if usrInput == "A":
            Receive()
    if usrInput == "B": 
            Send()

With thanks to Ben Barker!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900