Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Am trying to open serial port in Linux Ptyhon 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 serial port name as above. I want to take serial port name as the input from user from editable text box.

textbox.setText("/dev/ttyACM1")
serialport.port = textbox.text()
serialport.baudrate = 115200       
serialport.open() 
I am unable to convert textbox.text() format to serialport.port. The following error occurs. ValueError: "port" must be None or a string, not

Please Help me, 
Thanks in Advance.

What I have tried:

<pre>textbox.setText("/dev/ttyACM1")
serialport.port = textbox.text()
serialport.baudrate = 115200       
serialport.open() 
I am unable to convert textbox.text() format to serialport.port. The following error occurs. ValueError: "port" must be None or a string, not

Please Help me, 
Thanks in Advance
Posted
Updated 7-Mar-18 1:40am
Comments
Richard MacCutchan 7-Mar-18 5:58am    
There is something missing from your error message. You can check exactly what is returned from the textbox.text() method with the debugger.

1 solution

The pySerial port property is a Python string but the PyQt QLineEdit text() functions returns a QString object. So you have to use a conversion method or function.

I don't know for sure which conversion must or can be used here because I don't know Python well. But you can try the QString methods toAscii(), toUtf8(), and toLocal8Bit():
Python
serialport.port = textbox.text().toAscii()
 
Share this answer
 
Comments
Richard MacCutchan 7-Mar-18 8:25am    
Strings in Python default to UTF-8. I did Google for this earlier but could not find a reference to the text() method; most likely because I did not know that the textbox was a QLineEdit object.
Jochen Arndt 7-Mar-18 8:34am    
Thank you for this information.

Then all of the above methods should work because such device string contain only ASCII characters.

It might be also another input widget than QLineEdit but all PyQt text widgets use the QString type which uses UTF-16 internally.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900