【发布时间】:2021-03-29 11:37:04
【问题描述】:
目前我尝试将 720 字节从 Windows 应用程序发送到自定义 STM32 设备(现在出于测试目的,我使用 Blue Pill - STM32F103xxx)。啊,我忘了指出我完全是编程新手:)。所以在设备端,我有 1000 字节的缓冲区用于接收和发送(感谢 STMCube)。带有终端程序(数据包
- 计算 SINE weave (360) 样本 - 16 字节大小
- 将它们作为 720 字节发送到 USB 设备(COM 端口的字节大小协议)
我的问题是进入设备的字节数不超过 64 个。
在某处我读到这个原因可能是内置的 Rx,Tx Windows 缓冲区(64 字节长,在互联网上某处提到),为此我在下面的代码中插入:
- SetupComm(hCom,1000,1000) 希望这能解决我的麻烦,但不是。下面是“我的”代码,有什么办法可以解决这个问题吗?
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
void PrintCommState(DCB dcb)
{
// Print some of the DCB structure values
_tprintf(TEXT("\nBaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n"),
dcb.BaudRate,
dcb.ByteSize,
dcb.Parity,
dcb.StopBits);
}
int _tmain(int argc, TCHAR* argv[])
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
const TCHAR* pcCommPort = TEXT("COM3"); // Most systems have a COM1 port
unsigned __int8 aOutputBuffer[720];// Data that will sent to device
unsigned __int16 aCalculatedWave[360];// Data that will sent to device
int iCnt; // temp counter to use everywhere
for (iCnt = 0; iCnt < 360; iCnt = iCnt + 1)
{
aCalculatedWave[iCnt] = (unsigned short)(0xFFFF * sin(iCnt * PI / 180));
if (iCnt > 180) aCalculatedWave[iCnt] = 0 - aCalculatedWave[iCnt];
}
// 16 bit aCalculatedWaveto to 8 bit aOutputBuffer
for (int i = 0, j = 0; i < 720; i += 2, ++j)
{
aOutputBuffer[i] = aCalculatedWave[j] >> 8; // Hi byte
aOutputBuffer[i + 1] = aCalculatedWave[j] & 0xFF; // Lo byte
}
// Open a handle to the specified com port.
hCom = CreateFile(pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL); // hTemplate must be NULL for comm devices
if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
if (SetupComm(hCom,1000,1000) !=0)
printf("Windows In/Out serial buffers changed to 1000 bytes\n");
else
printf("Buffers not changed with error %d.\n", GetLastError());
// Initialize the DCB structure.
SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
// Build on the current configuration by first retrieving all current
// settings.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
PrintCommState(dcb); // Output to console
// Fill in some DCB values and set the com state:
// 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_9600; // baud rate
dcb.ByteSize = 8; // data size, xmit and rcv
dcb.Parity = NOPARITY; // parity bit
dcb.StopBits = ONESTOPBIT; // stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
// Get the comm config again.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
PrintCommState(dcb); // Output to console
_tprintf(TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort);
if (WriteFile(hCom, aOutputBuffer, 720, NULL, 0) != 0)
_tprintf(TEXT("720 bytes successfully writed to Serial port %s \n"), pcCommPort);
else
_tprintf(TEXT("Fail on write 720 bytes to Serial port %s \n"), pcCommPort);
return (0);
}
【问题讨论】:
-
Windows 代码看起来不错。最有可能的是,问题出在设备方面。您也可以添加该代码吗?
-
顺便说一句:数据以 64 字节的数据包传输。这就是 USB 的工作原理。所以不要指望 STM32 端有一个大于 64 字节的数据包的回调。 720字节会自动分成12个包。
-
你好 Codo,设备代码在这里太大了,但如果你想看这里github.com/stm32dds/Lite。我猜测 usbser.sys 是由 64 字节块发送的,但在我阅读确认的文档中没有找到,所以现在将尝试将此设备更改为 WinUSB.sys 设备,并查看是否有可能更大的数据包。至少如果不可能,将接收这 720 个字节作为块。谢谢!
-
无法发送大于 64 字节的数据包。这就是 USB 的工作原理。司机不会有任何影响。
标签: c serial-port usb stm32 cdc