Python Forum
pySerial .readline() help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: pySerial .readline() help (/thread-1733.html)



pySerial .readline() help - AlexSneedMiller - Jan-23-2017

Hello,

I am writing a Python v3.6.0 program on a Windows PC that takes an input from the Serial port and then parses the input, and replies on the Serial port based on the input. 

I tied COM1 and COM2 together. My Python script running is connected to COM1 and the Arduino serial monitor connected to COM2. I am using the pySerial library's command ".readline()" to send the serial communication bytes to my Python script. 

According to the pySerial docs. the .readline() command will return a string after it receives a '\n' character. However, when I send a string of text through the Arduino serial port monitor without a newline character at the end of it, the .readline() method returns that string. If I do send a string with a newline character, the newline character does get sent with the string to the rest of my script from the .readline() method. 

Does anyone know why how the .readline() method knows to return the 'line' even though there is no newline character at the end of it?

import serial

ser = serial.Serial(port='COM1', baudrate=9600, timeout=0)  # open serial port 
print("port opened on " + ser.name)

def waiting():
while(1):
parse_serial_port(ser.readline()) #this returns after strings are sent via the Arduino Serial Port monitor even without a newline character
Documentation for the pySerial library: htt p://pyserial.readthedocs.io/en/latest/shortintro.html#readline


Thanks,
Alex


RE: pySerial .readline() help - j.crater - Jan-23-2017

Hello,
Is it possible that Arduino's serial library adds a carriage return character automatically, when you do Serial.println()?
From what you have described this is how your setup appears to behave.