简体   繁体   中英

How to Read the serial port name from text box (editable)

I am trying to open the serial port in Linux with Python 2.7 PyQt4 with the below code and it works fine:

serialport.port = "/dev/ttyACM1"
serialport.baudrate = 115200       
serialport.open() 

I don't want to hard-code the serial port name as above. I want to take the serial port name as the input from user from an editable text box:

textbox.setText("/dev/ttyACM1")
serialport.port = textbox.text()
serialport.baudrate = 115200       
serialport.open() 

But I am unable to convert textbox.text() format to serialport.port . The following error occurs:

ValueError: "port" must be None or a string, not < class 'PyQt4.QtCore.QString' >

You are using PyQt and have something like this:

w = QWidget()
textbox =  QLineEdit(w)

right?!

The error message tells you that the result of textbox.text() is of type QString . But you need a string instead. You can simply convert the result using str(textbox.text())

serialport.port = str(textbox.text())

should solve the problem.

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