使用 pySerial 重新连接到设备 [英] Reconnecting to device with pySerial

查看:34
本文介绍了使用 pySerial 重新连接到设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在使用 Python 中的 pySerial 模块时遇到问题.我的问题与连接和断开设备有关.我可以成功地连接到我的设备并与它通信,只要我愿意,就可以随时断开与它的连接.但是,一旦连接中断,我就无法重新连接到设备.

I am currently having a problem with the pySerial module in Python. My problem relates to connecting and disconnecting to a device. I can successfully connect to my device and communicate with it for as long as I want to, and disconnect from it whenever I desire. However, I am unable to reconnect to the device once the connection has been severed.

这是我的程序用来与串行端口接口的包装类:

Here is the wrapper class that my program uses to interface with the serial port:

import serial, tkMessageBox

class Controller:
""" Wrapper class for managing the serial connection with the MS-2000. """
    def __init__(self, settings):
        self.ser = None
        self.settings = settings

    def connect(self):
        """ Connect or disconnect to MS-2000. Return connection status."""
        try:
            if self.ser == None:
                self.ser = serial.Serial(self.settings['PORT'],
                                         self.settings['BAUDRATE'])
                print "Successfully connected to port %r." % self.ser.port
                return True
            else:
                if self.ser.isOpen():
                    self.ser.close()
                    print "Disconnected."
                    return False
                else:
                    self.ser.open()
                    print "Connected."
                    return True
        except serial.SerialException, e:
            return False

    def isConnected(self):
        '''Is the computer connected with the MS-2000?'''
        try:
            return self.ser.isOpen()
        except:
            return False

    def write(self, command):
        """ Sends command to MS-2000, appending a carraige return. """
        try:
            self.ser.write(command + '\r')
        except Exception, e:
            tkMessageBox.showerror('Serial connection error',
                                   'Error sending message "%s" to MS-2000:\n%s' %
                               (command, e))

    def read(self, chars):
        """ Reads specified number of characters from the serial port. """
        return self.ser.read(chars)

有人知道这个问题存在的原因以及我可以尝试做些什么来解决它吗?

Does anybody know the reason why this problem exists and what I could try to do to fix it?

推荐答案

完成后没有释放串口.在退出程序前使用 ser.close() 关闭端口,否则端口将无限期锁定.我建议为此在您的类中添加一个名为 disconnect() 的方法.

You aren't releasing the serial port when you are finished. Use ser.close() to close the port before exiting your program, otherwise the port will stay locked indefinitely. I would suggest adding a method called disconnect() in your class for this.

如果您使用的是 Windows,要在测试期间纠正这种情况,请启动任务管理器并终止任何可能锁定序列号的 python.exepythonw.exe 进程端口.

If you are on Windows, to remedy the situation during testing, start Task Manager and kill any python.exe or pythonw.exe processes that may be locking the serial port.

这篇关于使用 pySerial 重新连接到设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆