[英]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
.作为检查,我可以使用
minicom
或python3 -m serial.tools.miniterm
登录设备,并从pwd
和echo 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 .将您的
port
、 baudrate
、 parity
和stopbits
与 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:一般注意事项:
read()
or until and end of line character with read_until
, ensure that the timeout is not set too low.read()
读取固定数量的字节或使用read_until
读取直到和行尾字符时,请确保超时设置不会太低。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.