gpt4 book ai didi

python - Windows 到 Raspberry Pi 零串行问题

转载 作者:行者123 更新时间:2023-11-30 05:13:17 26 4
gpt4 key购买 nike

我正在使用窗口的 WriteFile 函数通过串行端口发送数据。这是由运行位于 pyserial readline 循环(下面的代码)中的 python 脚本的 raspberry pi 零接收的。在 Windows 端,我将文件的内容放入缓冲区,然后调用 WriteFile 函数。它适用于大约前 95-99% 的数据,因为树莓派可以很好地接收该数据。我正在传输一个大小约为 200 KB 的文件内容,因此它获得了大量有用的数据,但不是最后的数据。每当我发送少量数据时,比如一条线,那么大部分线都会被损坏。

Windows 代码(相关位):

config.StopBits = ONESTOPBIT;
config.BaudRate = 115200;
config.Parity = NOPARITY;
config.fBinary = TRUE;
config.fParity = TRUE;
config.ByteSize = 8;

//10 second time out
COMMTIMEOUTS timeOuts;
timeOuts.ReadIntervalTimeout = 10000;
timeOuts.ReadTotalTimeoutConstant = 10000;
timeOuts.ReadTotalTimeoutMultiplier = 10000;
timeOuts.WriteTotalTimeoutConstant = 10000;
timeOuts.WriteTotalTimeoutMultiplier = 10000;

char* packet_buffer;

if (!WriteFile(dataFile, packet_buffer, size, &bytesRead, NULL))
{
_tprintf("There is a problem: %d\n", GetLastError());
}

Raspberry Pi Python 脚本(相关位):

ser = serial.Serial(
port = '/dev/serial0',
baudrate = 115200,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 10
)

data = open("data.txt", "w")

while 1:
x = ser.readline()
data.write(x)

如果这个问题含糊不清,我很抱歉,但我这几天一直在摆弄这个问题。我的“直觉”是 raspberry pi 零硬件有问题(比如它不够快或其他什么),但是当它接收到几百 KB 时这就没有意义了。

最佳答案

尝试启用流量控制。

在 python 方面,在你的 ctor 中设置 rtscts=1:

ser = serial.Serial(
port = '/dev/serial0',
baudrate = 115200,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 10,
rtscts = True
)

在 win c++ 端,在 DCB Struct 中启用硬件流控制:

DCB dcb;
SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
fSuccess = GetCommState(dataFile, &dcb)
dcb.fOutX = false;
dcb.fInX = false;
dcb.fOutxCtsFlow = true;
dcb.fOutxDsrFlow = true;
dcb.fDsrSensitivity = true;
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
fSuccess = SetCommState(hCom, &dcb);

关于python - Windows 到 Raspberry Pi 零串行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44003765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com