使用USB向PIC发送数据C#

本文关键字:数据 PIC USB 使用 | 更新日期: 2023-09-27 18:20:34

我想使用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?

使用USB向PIC发送数据C#

这是一个我用来向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");