我想使用USB(UsbLibrary.dll)将数据C#发送到PIC。 我使用以下代码发送数据;
private void button1_Click(object sender, EventArgs e)
{
byte[] my_buffer = new byte[64];
my_buffer[1] = (byte)0x01;
usbHidPort1.SpecifiedDevice.SendData(my_buffer);
}
但是我得到错误,因为usbHidPort1.SpecifiedDevice值为null(这意味着连接的PIC设备未被C#识别),尽管供应商和产品ID在C#和CCS C(PIC)中编码相同。如何使用UsbLibrary将数据C#发送到PIC?
答案 0 :(得分:0)
这是我用来向PIC发送数据的函数
private UsbDevice usb = new UsbDevice();
if (usb != null && usb.myDevice != null)
{
byte[] data = new byte[usb.myDevice .nOutputReportLength];
data[0] = 0; // The first byte is the "Report ID" and does not get sent over the USB bus. Always set = 0.
data[1] = b;
if (d != null)
Array.Copy(d, 0, data, 2, d.Length);
this.usb.myDevice .SendData(data);
}
else
MessageBox.Show("Device not found");