controller_communication.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:HES 作者: HackerSchool 项目源码 文件源码
def find_serial_port():
        """
        tries to connect to all available serial ports, returns of first successful connection.
        exit if none works

        :return: serial interface object
        """
        if settings.debug_port_detection:
            for port in list_ports.comports():
                print('trying {}'.format(port[0]))

            return serial.Serial(settings.forced_port, settings.baud_rate, timeout=1)

        else:
            for port in (list_ports.comports()):
                print('trying {}'.format(port[0]))
                try:
                    serial_interface = serial.Serial(port[0], settings.baud_rate, timeout=2)
                    sleep(2)  # wait for the device to be ready
                    # send hello command
                    serial_interface.write(settings.handshake_challenge)

                    # check if this device knows what to reply
                    reply = serial_interface.read(len(settings.handshake_response))
                    print(reply)
                    if reply == settings.handshake_response:
                        return serial_interface

                except serial.SerialException:
                    # print("opening serial failed")
                    pass

        raise ConnectionError("Couldn't connect to any serial ports, exiting...")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号