如何从Python中的文本框(可编辑)中读取串口名称? [英] How to read the serial port name from text box (editable)in Python?
本文介绍了如何从Python中的文本框(可编辑)中读取串口名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用以下代码在Linux Ptyhon 2.7 pyqt4中打开串口,并且工作正常
serialport.port =/ dev / ttyACM1
serialport.baudrate = 115200
serialport.open()
我不想像上面那样硬编码串口名。我想从可编辑文本框中获取用户输入的串行端口名称。
textbox.setText(/ dev / ttyACM1)
serialport.port = textbox.text()
serialport.baudrate = 115200
serialport.open()
我无法将textbox.text()格式转换为serialport.port。发生以下错误。 ValueError:port必须是None或字符串,而不是
请帮帮我,
先谢谢。
我尝试过:
< pre> textbox.setText(/ dev / ttyACM1)
serialport.port = textbox.text()
serialport.baudrate = 115200
serialport.open()
我无法将textbox.text()格式转换为serialport.port。发生以下错误。 ValueError:port必须是None或字符串,而不是
请帮帮我,
先谢谢
解决方案
pySerialport
属性是一个Python字符串,但 PyQtQLineEdit
text()
functions返回QString
对象。所以你必须使用转换方法或函数。
我不确定哪个转换必须或可以在这里使用,因为我不熟悉Python 。但你可以尝试QString
方法toAscii()
,toUtf8()
,toLocal8Bit()
:
serialport.port = textbox.text()。toAscii( )
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
解决方案
The pySerialport
property is a Python string but the PyQtQLineEdit
text()
functions returns aQString
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 theQString
methodstoAscii()
,toUtf8()
, andtoLocal8Bit()
:
serialport.port = textbox.text().toAscii()
这篇关于如何从Python中的文本框(可编辑)中读取串口名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文