[英]Raspberry Pi, Python: How to send large data over usb serial?
I am trying to send a large amount of data from my Raspberry Pi 4 to my computer.我正在尝试将大量数据从我的 Raspberry Pi 4 发送到我的计算机。 I configured the Raspberry Pi as USB OTG Serial Gadget and the data is sent through the usb-c port to my computer.
我将 Raspberry Pi 配置为 USB OTG 串行小工具,数据通过 usb-c 端口发送到我的计算机。
Please take a look at the following code running on the Raspberry Pi.请看一下在 Raspberry Pi 上运行的以下代码。 One MB of data is sent.
发送了 1 MB 的数据。
import serial
ser = serial.Serial( port='/dev/ttyGS0', baudrate=115200)
packet = bytearray()
for i in range(0, 1000000):
packet.append(0x2f)
ser.write(packet)
This is the code I am running first on my computer.这是我首先在我的计算机上运行的代码。
import serial
import time
ser = serial.Serial(port='COM30', baudrate=115200)
sum = 0
while 1:
bytesToRead = ser.inWaiting()
if bytesToRead > 0:
serial_line = ser.read(bytesToRead)
sum += bytesToRead
print(sum)
time.sleep(0.01)
I would expect that the received data has always the same length as the sent data.我希望接收到的数据始终与发送的数据具有相同的长度。 But in this example the computer receives a data length of around 990.000 Bytes in most cases.
但在此示例中,计算机在大多数情况下接收的数据长度约为 990.000 字节。 Even if I run the code without the sleep function on my computer, there are sometimes missing bytes.
即使我在计算机上运行没有睡眠 function 的代码,有时也会丢失字节。
How can I make sure that the data is sent and received without data loss?如何确保发送和接收数据而不会丢失数据?
First, if you have enabled logins on the serial port, you need to disable them first otherwise the port will be inaccessible.首先,如果您在串口上启用了登录,您需要先禁用它们,否则该端口将无法访问。
Second, stop getty@ttyGS0.service
and disable it.其次,停止
getty@ttyGS0.service
并禁用它。
And then everything will work fine.然后一切都会正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.