Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pySerial why do i need \r
#1
Hi there!
I'm pretty new to python. I'm a chemist by training. Right now i'm sitting in the lab and try to understand how a certain serial connection is working. Everything makes sense to me in the code below, except for the \r part inside the go() function. The device is a port-selector, where you can select different ports (1-24). In the code port 7 is selected.
Without the \r the device is not working. Could anyone explain to me what the role of \r is and why \n couldn't be used?
Many thanks!

import serial
pos = 7

def go():
    position = 'GO%d\r' % (pos)
    ps.write(position.encode())

pscom = "/dev/cu.usbserial-DO028VY0"
ps = serial.Serial(port=pscom, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
go()
Reply
#2
\r is a carriage feed (hex 0d)
\n is cr lf (2 bytes) (hex 0d 0a)
your serial port is looking for a single 0d hex.
Reply


Forum Jump:

User Panel Messages

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