我正在编写一个应用程序,允许我通过选定的串口连接到我的设备(使用RS485)。除了一件事,一切都很好。单击按钮时,我的应用程序调用功能并打开连接,但是一旦此功能执行了所有操作,应该立即关闭端口连接。因此,当我想稍后使用此连接时,我收到错误," serial未定义"。
def open_connection(self):
cur_item = self.comboBox.currentText()
if (cur_item) is not None:
fullname = self.full_port_name(str(cur_item))
try:
ser = serial.Serial(
port=fullname,
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=None)
except SerialException, e:
QMessageBox.critical(self, 'Failure',
'Failed to open %s:\n%s' % (cur_item, e))
我希望我的连接打开,因为当我关闭程序时单击按钮以便我可以在此期间使用lineEdit(在按下Enter后触发其他功能)以向设备发送一些命令。你能帮助我吗?
编辑: 好的,我用QThreads解决了这个问题。但我仍然不知道为什么如果我的程序的某个部分使用串口,另一个不能用它而不打开自己的端口(这是不可能的,因为只有一个'' ;可以一次连接到串口。)