开发者

Set serial port pin high using python

开发者 https://www.devze.com 2022-12-23 06:49 出处:网络
Is it possible to set one pin of the serial port continuously high u开发者_如何学Gosing python (or C)?

Is it possible to set one pin of the serial port continuously high u开发者_如何学Gosing python (or C)? If yes, how?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号