简体   繁体   中英

Python serial port status

How do you know the status of a serial port in Python prior to opening the serial port?

I know I can use the following code below, to determine its status, but this required the port to be already open.

if self.ser.isOpen():

The reason I ask, is sometimes when I execute my code I get a serial exception stating that the port is already open, in this case, I want to know before hand and try to close it before opening it.

As you have mentioned you know the status of the port by using isOpen() . This will return a bool value of either True or False .

When you declare a serial port initially for example: ser.serial('/dev/ttyUSB0') the port is opened by default. You could either close the serial port using ser.close() and check the status of the serial port using ser.isOpen() .

Hope this helps you more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM