本帖最后由 海文 于 2018-11-15 12:40 编辑
有没有大佬用过LibUsbDotNet这个开源库搞上位机的吗?我用STM32F4枚举成HID设备作为下位机,使用USB2.0全速,端点设置正确,但是在单片机不断发送数据到上位机的时候,一开始上位机可以接收到单片机的数据,运行几分钟后就接收不到数据了,此时单片机的USB状态是suspend,希望各位大佬可以给点意见,谢谢!下面这幅图是这个开源库测试USB设备信息的程序运行结果:
1.png (27.59 KB, 下载次数: 0)
上位机中关于USB的就只有下面这些了,
private void Form1_Load(object sender, EventArgs e)
{
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
// If the device is open and ready
if (MyUsbDevice == null)
{
// The device is not connected or cannot be accessed by libusbdotnet
messagebox.Text = "Device not connected!" + "
";
return;
}
else messagebox.Text = messagebox.Text + "
" + "Device connected!" + "
";
//开启接收数据响应
IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
if (!ReferenceEquals(wholeUsbDevice, null))
{
// Select config #1
wholeUsbDevice.SetConfiguration(1);
// Claim interface #0.
wholeUsbDevice.ClaimInterface(0);
}
// open read endpoint 1.
reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
reader.DataReceived += (OnRxEndPointData);
reader.DataReceivedEnabled = true;
}
static void USB_SendCmd(byte[] buffer, int count)
{
ErrorCode ec = ErrorCode.None;
try
{
// open write endpoint 1.
ec = writer.Write(buffer, 2000, out count);
if (ec != ErrorCode.None) throw new Exception(UsbDevice.LastErrorString);
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
}
}
long cnt = 0;
private void OnRxEndPointData(object sender, EndpointDataEventArgs e)
{
Encoding.Default.GetString(e.Buffer, 0, e.Count);
cnt++;
//bytesRead = e.Count;
if (e.Buffer[0] != 0)
{
for (int i = 0; i < e.Count; i++)
{
//messagebox.Text = messagebox.Text + " " + readBuffer;
RecBuffer【i】 = e.Buffer【i】;
e.Buffer【i】= 0;
}
RecFlag = true;
}
}
尝试过使用USB Hound抓包但是没有显示,不知道原因。
一周热门 更多>