Set serial port pin high using python

Is it possible to set one pin of the serial port continuously high using python (or C)? If yes, how?

Answers


Using the pyserial methods setRTS(level=True) and setDTR(level=True) you can control the RTS and DTR lines at will. For instance, the following code will toggle the RTS pin of the first serial port. (See the pyserial documentation for the details).

import time
import serial

ser = serial.Serial(0)
ser.setRTS(False)
time.sleep(0.5)
ser.setRTS(True)
time.sleep(0.5)
ser.setRTS(False)

Yes it is possible using pySerial.


If you open the port, the Tx line should go to the voltage high state, and if you never send data it should stay in that state (see here). I've never actually tried this though. You might find you need to enable one of the control lines instead, like CTR.

As for software, TheMachineCharmer's suggestion of pySerial is the most platform independent way of doing it. If you're on Windows, the Win32 API calls are summarized here. Look for CreateFile and (if you need to set control lines) GetCommState and SetCommState. Note that the filename you pass to CreateFile should look like "\\\\.\\COM1". The .NET way of doing is is summarized here. You'll need to construct a SerialPort object, then access its properties to set control lines.


Need Your Help

Thumbnail image does not appear on website

php wordpress

I have a problem in my wordpress theme.

Java EE 7 CDI - Injection doesn't work, sending NullPointerException

vaadin cdi java-ee-7 wildfly

I have a problem with my injection, it's the first time I try it. I'm working with Wildfly and Java EE 7. I've got a NullPointerException when trying to access Authenticator instance in LoginContro...