在minicom打开端口之前,不能使用PySerial [英] using PySerial don't work until port is opened with minicom

查看:157
本文介绍了在minicom打开端口之前,不能使用PySerial的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RPi3B +上为Domoticz开发了一个插件.该插件在Python中. 我想使用USB串行端口向Arduino开发板发送命令.

I developed a plugin for Domoticz on a RPi3B+. This plugin is in Python. I want to send commands to a Arduino board using a USB serial port.

插件将打开串行端口,发送命令并关闭串行端口.除非重新启动,否则它运作良好.

The plugin opens the serial port, sends a command and closes the serial port. It works well except after a reboot.

重启后,端口打开,该命令似乎已发送到Arduino,但它不理解,就像波特率错误一样. Arduino的Rx LED闪烁.

After a reboot, the port is open, and the command seems to be sent to the Arduino, but it doesn't understand it, just as if the baud rate were wrong. Arduino's Rx LED is blinking.

如果我使用minicom并行打开串行并退出minicom而不重置参数,那么插件将开始正常工作.

If I open the serial in parallel using minicom and exit minicom without resetting the parameters, then the plugin starts working properly.

这是我的代码:

serialCmd = "gpio sh" + str( shutterId ) + "_" + order +" on for " + str( PULSE_DURATION_MS ) + "\r"
            Domoticz.Debug( "Serial command : " + serialCmd )

            # open serial port
            try:
                Domoticz.Debug( "Opening serial port : " + Parameters["SerialPort"] )
                serialPort = serial.Serial( port = Parameters["SerialPort"], 
                                            baudrate = 115200,
                                            bytesize = serial.EIGHTBITS, 
                                            parity = serial.PARITY_NONE,
                                            stopbits = serial.STOPBITS_ONE, 
                                            timeout = 1,
                                            xonxoff = False,
                                            rtscts = False,
                                            dsrdtr = False )
            except:
                serialPort = None

            if serialPort:
                serialPort.write( serialCmd.encode( 'utf-8' ) )
                serialPort.close()
                serialPort = None

串行端口为/dev/ttyUSB0 . 如果我尝试在 serial.Serial(...)中使用 exclusive = True ,它将无法打开端口,就像端口已经打开一样.

The serial port is /dev/ttyUSB0. If I try to use exclusive = True in serial.Serial(...), it fails to open the port, as if the port were already open.

另一个奇怪的事情是:当Arduino板重启时,它会向串口发送一些信息. 我无法使用带有PySerial的Python插件来读取它,但可以使用minicom读取它.

Another strange thing: when the Arduino board reboots, it sends some information to the serial port. I can't read it with the plugin in Python with PySerial, but I can read it with minicom.

如果我通过重置参数关闭 ,则每次打开minicom时,minicom都会读取此信息(无需重置Arduino板),就像从未读取输入缓冲区一样,并且Python插件仍然无法正常工作.

If I close minicom with resetting the parameters, each time I open minicom this information is read by minicom (without resetting the Arduino board) as if the input buffer has never been read, and the Python plugin still doesn't work.

我该如何解决问题?

推荐答案

此问题是由于这篇文章使我搜索了互联网上正确的事情.这就是为什么在不重置端口的情况下打开minicom并关闭即可使其正常工作的原因.

This issue was because of Arduino nano schematic where DTR signal is connected to the reset signal which makes the Arduino to reset each time the serial port is open. This post made me search the right thing on the internet. That's why opening minicom and closing without resetting the port makes it work...

当某个进程不再使用串行端口时,DTR线将被重置,因此下一次进程打开串行端口时,将驱动DTR,从而使Arduino重新启动.

When the serial is not used anymore by a process the DTR line is reset so next time a process open the serial port the DTR is driven which makes the Arduino reboot.

一种解决方法是修改板或两个禁用DTR处理.这可以通过终端lib的HUPCL位.

A way to fix it is to modify the board or two disable the DTR handling. This can be done by dsiable the HUPCL bit of the terminal lib.

有些人设法使用pySerial修复了该问题,但是它对我不起作用,因此我必须按照以下说明进行操作. ..found 此处

Some people manage to fix it with pySerial but it does not work for me so I had to do as below...found here and here.

import serial
import termios

port = '/dev/ttysUSB0'
f = open(port)
attrs = termios.tcgetattr(f)
attrs[2] = attrs[2] & ~termios.HUPCL
termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
f.close()
se = serial.Serial()
se.baudrate = 115200
se.port = port
se.open()

这篇关于在minicom打开端口之前,不能使用PySerial的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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