这个是公司的项目,以前基于usb的虚拟串口来做上下位机通信,由于usb平时也就刷参数和调试之用,也不很常用,外面反应usb有时会连不上stm32控制器,心里一直认为不是大问题,后来反应的多了,就重视这个事情了,虽然usb平时不用,但是用的时候只要出现异常掉线,就再也连不上去了,特别影响用户对我们产品的信心。
在网上找了很多资料,到现在也只能隐约确定是stm32官方的虚拟串口库有问题,我发现不同的电源板都会影响到usb虚拟串口的枚举,于是下定决心使用其他方式来做上下位机的连接,排除了usb虚拟串口,眼前的选择只有两个
1)对于windows API来说,读和写hid设备的数据包都是大小确定了,也就是说如果stm32 端口设备的hid属性是64的话,那么读写都是64字节的。读写的最大帧是64字节,我在代码中是基于64字节做应用层协议的。
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *data, size_t length)
{
DWORD bytes_written;
BOOL res;
BOOL bResult;
OVERLAPPED ol;
HANDLE ReportEvent;
ReportEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
ResetEvent(ReportEvent);
memset(&ol, 0, sizeof(ol));
ol.hEvent = ReportEvent;
//res = WriteFile(dev->device_handle, data, 23, NULL, &ol);
res = WriteFile(dev->device_handle, data, length, NULL, &ol);
if (!res) {
int errcode = GetLastError();
if (errcode != ERROR_IO_PENDING) {
// WriteFile() failed. Return error.
register_error(dev, "WriteFile");
return -1;
}
}
bResult = WaitForSingleObject(ReportEvent, 50);
if(bResult == WAIT_TIMEOUT || bResult == WAIT_ABANDONED)
{
register_error(dev, "WriteFile");
CancelIo(&(dev->device_handle));
return -1;
}
// Wait here until the write is done. This makes
// hid_write() synchronous.
res = GetOverlappedResult(dev->device_handle, &ol, &bytes_written, TRUE);
if (!res) {
// The Write operation failed.
register_error(dev, "WriteFile");
return -1;
}
return bytes_written;
}