Python serial - Attempting to use a port that is not open

非 Y 不嫁゛ 提交于 2019-12-07 06:25:50

问题


I'm still new in python so please bear with me, so I'm trying to write a script with python2-pyserial but I keep getting error Attempting to use a port that is not open Here's the script:

#!/usr/bin/python

import serial, time
#initialization and open the port
#possible timeout values:
#    1. None: wait forever, block call
#    2. 0: non-blocking mode, return immediately
#    3. x, x is bigger than 0, float allowed, timeout block call
ser = serial.Serial()
ser.port = "/dev/ttyUSB2"
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
#ser.timeout = None          #block read
ser.timeout = 1            #non-block read
#ser.timeout = 2              #timeout block read
ser.xonxoff = False     #disable software flow control
ser.rtscts = False     #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False       #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 2     #timeout for write
try:
    ser.open()
    print ("Port has been opened")
except Exception, e:
    print ("error open serial port: ") + str(e)
    exit()

if ser.isOpen():
    try:
        ser.flushInput() #flush input buffer, discarding all its contents
        ser.flushOutput()
        ser.write("ATI")
        print("write data: ATI")
        time.sleep(1)  #give the serial port sometime to receive the data
        numOfLines = 0
        while True:
            response = ser.readline()
            print("read data: " + response)
            numOfLines = numOfLines + 1
            if (numOfLines >= 5):
                break
                #pass
            ser.close()
    except Exception, e1:
        print ("error communicating...: ") + str(e1)
else:
    print ("cannot open serial port ")

I've tried to run the script with sudo python2 ser but I still have the same error. How do I fix it ?


回答1:


The first part of your code is wrong, you're making the wrong attributions for ser. Try the following way:

ser = serial.Serial(
port = "/dev/ttyUSB2",
baudrate = 115200,
bytesize = serial.EIGHTBITS, 
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE, 
timeout = 1,
xonxoff = False,
rtscts = False,
dsrdtr = False,
writeTimeout = 2
)

On my environment, the port was already open after that, but if it isn't you can try to open it:

ser.open()
ser.isOpen()

And you have to be sure that this is not a virtual port on your pc if it is, you will have to change this:

ser.rtscts = False  #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False  #disable hardware (DSR/DTR) flow control

For this:

ser.rtscts = True
ser.dsrdtr = True

Check out this issue for more info



来源:https://stackoverflow.com/questions/37288278/python-serial-attempting-to-use-a-port-that-is-not-open

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!