简体   繁体   English

树莓派,Python:如何通过 usb 串口发送大数据?

[英]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.

相关问题 如何使用Python将Hex数据发送到Raspberry Pi串行端口 - How to send Hex Data to Raspberry Pi Serial Port using Python Python Raspberry PI-在LCD上显示USB串行数据 - Python Raspberry PI - Display USB Serial Data on LCD USB上的Raspberry Pi和arduino串行连接显示USB信息 - Raspberry pi and arduino serial connection over usb displays USB information 使用python通过Raspberry Pi中的串行端口将数据发送到PLC - Sending data to PLC over serial port in Raspberry Pi using python Raspberry通过串行USB将数据发送到Arduino Python - Raspberry send data via serial USB to Arduino Python Raspberry pi不会使用minicom或python将串行数据发送到arduino - Raspberry pi will not send serial data to an arduino using either minicom or python 通过串行接口与Arduino接口的Python代码在Raspberry Pi上不起作用 - Python code to interface with Arduino over serial not working on Raspberry Pi 可能吗? Python通过USB端口(Raspberry Pi)发送十六进制代码 - Is it possible? Python send Hex code via usb port (raspberry pi) 将带有 Python 的树莓派字符串发送到 Arduino 以启动 LED 灯条。 怎么把select正确的USB端口? - Send string from raspberry pi with Python to Arduino for starting led strip. How to select the right USB port? 如何将数据从python raspberry pi发送到mssql服务器? - how to send data to to mssql server from python raspberry pi?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM