Python Forum
pyserial setting and getting RTS/DTR etc...- RTFM, but still don't understand it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyserial setting and getting RTS/DTR etc...- RTFM, but still don't understand it
#1
I've started playing around with the serial port, so I installed pySerial. I then built a very simple breakout board, where I can measure voltages, strap LED and so on across the various RS232 lines.
For example, from a quick web search for how to control the RTS line (I just picked RTS because it is an outgoing line controlled by the DTE a.k.a my laptop, I knocled up this script:

import os
import fnmatch
import time
import serial

pattern = "*Prolific*"
targetDir = ('/dev/serial/by-id/')
symlinkLocation = os.listdir(targetDir)

for device in symlinkLocation:
    if fnmatch.fnmatch(device, pattern):
        targetDevice = device
        prolificSymlink = os.readlink(targetDir+targetDevice)
        global targetPort
        targetPort = prolificSymlink.replace('../', '')
        print(targetPort)

devAddr = ('/dev/'+targetPort)

ser = serial.Serial(devAddr)

for x in range(60):
    ser.setRTS(True)
    print("True", end=' ')
    time.sleep(0.5)
    
    ser.setRTS(False)
    print("False", end=' ')
    time.sleep(0.5)
It works OK. The LED strapped across the RTS line and GND flashes.

When I looked to see how could read the state of the RTS line, I looked at the pySerial documentation page.

For setRTS, it says:
------------------------
setRTS(level=True)
Deprecated since version 3.0: see rts
------------------------

Since I am using pySerial 3.5, I followed the link to the rts bit.
That says:

------------------------
rts
Setter: Set the state of the RTS line
Getter: Return the state of the RTS line
Type: bool

Set RTS line to specified logic level. It is possible to assign this value before opening the serial port, then the value is applied upon open() (with restrictions, see open()).
------------------------

Which to me, is clear as mud! In the depreciated version, setRTS(), it was clear to use TRUE or FALSE, to turn it on or off. But with the preferred version, I can't see how to use Setter and Getter.

I have tried a couple of web searches looking for examples, but all I get in return is links to the main documentation page.

If some could explain it a bit clearer, such as with on example that would help a lot.
Reply


Forum Jump:

User Panel Messages

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