/// <summary> /// 发送控制命令 /// </summary> /// <param name="cmd">USB_CMD命令</param> /// <param name="length">欲发送数据长度</param> /// <param name="data">命令附加参数,即如:{x, y, width, height}</param> public void sendCommand(USB_CMD cmd, ushort length, byte[] data) { USB_CMD_BUF buf = new USB_CMD_BUF(); buf.head = CMD_BUF_HEAD; buf.end = CMD_BUF_END; buf.cmd = (byte)cmd; buf.length = length; buf.data = data; int a; ErrorCode ec = writer.Transfer(getBytes(buf), 0, 9, MAX_TIMEOUT, out a); //if (ec != ErrorCode.None) throw new Exception(UsbDevice.LastErrorString); }
/// <summary> /// 将 USB_CMD_BUF 转换成 byte数组 /// </summary> /// <param name="buf">USB_CMD_BUF对象</param> /// <returns>byte数组</returns> private byte[] getBytes(USB_CMD_BUF buf) { int size = Marshal.SizeOf(buf); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(buf, ptr, true); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); return(arr); }