Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pySerial .readline() help
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pyserial Readline() conversion to array bprosman 1 1,911 Apr-11-2023, 12:44 AM
Last Post: deanhystad
  Greek letters with .readline() and tkinter KinkgOfKeks 7 1,751 Mar-24-2023, 05:13 PM
Last Post: deanhystad
Star Pyserial not reading serial.readline fast enough while using AccelStepper on Arduino MartyTinker 4 4,084 Mar-13-2023, 04:02 PM
Last Post: deanhystad
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,526 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  readline inside and outside functions paul18fr 2 2,043 May-20-2021, 01:15 PM
Last Post: csr
  TypeError: file must have 'read' and 'readline' attributes hobbyist 6 10,925 Jun-12-2020, 05:12 PM
Last Post: DreamingInsanity
  problem with readline() schlundreflex 6 4,420 Nov-06-2019, 02:22 PM
Last Post: schlundreflex
  readline() and readlines() rpaskudniak 9 30,100 Nov-21-2017, 07:39 PM
Last Post: metulburr
  Empty variable when using print before readline fstefanov 3 3,665 Oct-23-2017, 02:22 AM
Last Post: fstefanov
  How can I use GNU readline when the value of sys.stdin has been changed? thePhysicist8 6 7,098 Dec-30-2016, 10:09 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020