简体   繁体   English

pyserial连接到串口上的android没有响应

[英]No response from pyserial connection to android on serial port

Trying to communicate with an android device on a serial port.尝试通过串行端口与安卓设备通信。 I can establish a connection through pyserial, but I don't understand how the flow of information works and why I can't get any output (or when I should expect output).我可以通过 pyserial 建立连接,但我不明白信息流是如何工作的,以及为什么我无法获得任何输出(或者我什么时候应该期待输出)。 As a check, I can log into the device using minicom or python3 -m serial.tools.miniterm and get the expected output from things like pwd and echo hello .作为检查,我可以使用minicompython3 -m serial.tools.miniterm登录设备,并从pwdecho hello等获取预期输出。 My basic code:我的基本代码:

import serial
ser = serial.Serial("/dev/ttyS0")
ser.write(b'echo hello')
ser.inWaiting()
ser.write(b'pwd')
ser.inWaiting()
# print(ser.readLine().decode())

ser.inWaiting() returns 0 both times it's called, so there's no point in trying to read anthing. ser.inWaiting()两次调用都返回0 ,因此尝试读取 anthing 是没有意义的。

I would greatly appreciate any discussion of fundamentals that I seem to be missing.我将非常感谢任何关于我似乎遗漏的基础知识的讨论。 Thank you.谢谢你。

Compare your port , baudrate , parity and stopbits with the minicom setting and pass those keywords to serial.Serial .将您的portbaudrateparitystopbits与 minicom 设置进行比较,并将这些关键字传递给serial.Serial You did not set a baudrate and serial.Serial 's default of 9600 B is probably wrong.您没有设置波特率和串行。串行的默认值 9600 B 可能是错误的。 115200 B would be my first guess. 115200 B 将是我的第一个猜测。

On a general note:一般注意事项:

  1. The short introduction shows the usage of pyserial .简短的介绍显示了pyserial的用法。
  2. When reading a fixed number of bytes with read() or until and end of line character with read_until , ensure that the timeout is not set too low.使用read()读取固定数量的字节或使用read_until读取直到和行尾字符时,请确保超时设置不会太低。
  3. Ensure that only one instance open and closes the serial port.确保只有一个实例打开和关闭串行端口。 A port can be opened multiple times, but that leads to unexpected behavior.一个端口可以打开多次,但这会导致意外行为。
  4. When using inWaiting() you to wait a while for the answer first(300 ms would be good starting point)—serial communication is so slow!使用inWaiting()时,您首先要等待一段时间(300 毫秒将是一个很好的起点)——串行通信太慢了!

The return of ser.inWaiting will be the number of bytes on input buffer. ser.inWaiting的返回值将是输入缓冲区的字节数。 The code below writes your first message and waits if something is entered on input buffer then will read what come to you:下面的代码写入您的第一条消息并等待输入缓冲区中输入的内容,然后将读取您收到的内容:

import serial
ser = serial.Serial("dev/ttyS0")
while 1:
    ser.write(b'echo hello')
    if ser.inWaiting()>0:
        ser.read(32)# the argument is the number of bytes to read

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM