// Receive several data input reports over the interrupt endpoint public static bool ReceiveData(IntPtr hid, ref byte[] buffer, int bufferSize, ref int bytesRead, int timeout) { bool success = false; Stopwatch stopwatch = new Stopwatch(); try { bytesRead = 0; stopwatch.Start(); while (bytesRead == 0) { success = HID.ReceiveData(hid, ref buffer, bufferSize, ref bytesRead); if (!success || stopwatch.ElapsedMilliseconds > timeout) { success = false; break; } Thread.Sleep(Constants.TIMER_READ); } } catch (Exception ex) { Console.WriteLine("ReceiveData error: " + ex.Message); } finally { stopwatch.Stop(); } return(success); }
private static void GetSilabVersion(IntPtr m_hid) { byte[] buffer = new byte[128]; int bytesRead = 0; byte[] command = RFIDCommands.PowerOn(false); if (!USBSocket.TransmitData(m_hid, command, command.Length)) { Console.WriteLine("Device failed to transmit data."); return; } if (HID.IsOpened(m_hid)) { if (USBSocket.ReceiveData(m_hid, ref buffer, buffer.Length, ref bytesRead, 1000)) { } } else { Console.WriteLine("Device is not connected."); } command = SiliconLabCommands.GetVersion(); if (!USBSocket.TransmitData(m_hid, command, command.Length)) { Console.WriteLine("Device failed to transmit data."); return; } // Make sure that we are connected to a device if (HID.IsOpened(m_hid)) { if (USBSocket.ReceiveData(m_hid, ref buffer, buffer.Length, ref bytesRead, 1000)) { if ((buffer[0] == Constants.PREFIX) && (bytesRead >= 13) && (buffer[8] == 0xB0) && (buffer[9] == 0x00)) { string slabVersion = buffer[10].ToString() + "." + buffer[11].ToString() + "." + buffer[12].ToString(); Console.WriteLine("Silicon lab firmware version: " + slabVersion); } else { Console.WriteLine("Cannot get silicon lab firmware version."); } } } else { Console.WriteLine("Device is not connected."); } }
private static void GetBTVersion(IntPtr m_hid) { byte[] command = BluetoothCommands.GetVersion(); if (!USBSocket.TransmitData(m_hid, command, command.Length)) { Console.WriteLine("Device failed to transmit data."); return; } byte[] buffer = new byte[128]; int bytesRead = 0; // Make sure that we are connected to a device if (HID.IsOpened(m_hid)) { if (USBSocket.ReceiveData(m_hid, ref buffer, buffer.Length, ref bytesRead, 2000)) { if ((buffer[0] == Constants.PREFIX) && (bytesRead >= 13) && (buffer[8] == 0xC0) && (buffer[9] == 0x00)) { uint crc = ((uint)buffer[6] << 8) | (uint)buffer[7]; if (crc != 0 && !CRC.CheckCRC(buffer, 0, 13, crc)) { Console.WriteLine("Wrong CRC received."); return; } string btVersion = buffer[10].ToString() + "." + buffer[11].ToString() + "." + buffer[12].ToString(); Console.WriteLine("Bbluetooth firmware version: " + btVersion); } else { Console.WriteLine("Cannot get bluetooth firmware version."); } } } else { Console.WriteLine("Device is not connected."); } }
public static int GetHidString(int deviceIndex, ref string deviceString) { int status = 0; byte[] String = new byte[MAX_PATH_LENGTH]; IntPtr pnt = Marshal.AllocHGlobal(MAX_PATH_LENGTH); status = HID.HidDevice_GetHidString(deviceIndex, VID, PID, HID_PATH_STRING, pnt, MAX_PATH_LENGTH); if (status == HID_DEVICE_SUCCESS) { Marshal.Copy(pnt, String, 0, MAX_PATH_LENGTH); deviceString = System.Text.Encoding.ASCII.GetString(String); deviceString = deviceString.Split('\0')[0]; } Marshal.FreeHGlobal(pnt); return(status); }
// Fragment and transmit data by sending output reports over // the interrupt endpoint public static bool TransmitData(IntPtr hid, byte[] buffer, int bufferSize) { bool success = false; // Make sure that we are connected to a device if (HID.IsOpened(hid)) { try { success = HID.TransmitData(hid, buffer, bufferSize); } catch (Exception ex) { throw new Exception(ex.Message); } } else { throw new Exception("Device is not connected."); } return(success); }
public static void Main(string[] args) { string deviceString = ""; int deviceNumber = 0; IntPtr m_hid = IntPtr.Zero; RFIDReader reader; try { //reset libUSB device using the tool usb-reset (on Linux only) //https://github.com/ralight/usb-reset PlatformID pid = Environment.OSVersion.Platform; if (pid == PlatformID.Unix) { string ret = ExecuteBashCommand("sudo usb-reset 10c4:8468"); if (ret != String.Empty) { Console.WriteLine("Unable to reset CS108 USB device: " + ret); return; } } bool found = false; // Iterate through each HID device with matching VID/PID for (int i = 0; i < HID.GetNumHidDevices(); i++) { // Add path strings to the combobox if (HID.GetHidString(i, ref deviceString) == HID.HID_DEVICE_SUCCESS) { Console.WriteLine("CS108 Found. Device ID = " + deviceString); deviceNumber = i; found = true; } if (found) { break; //get the first device being detected and break } } if (!found) { Console.WriteLine("No CS108 device detected over USB. Exit program."); return; } int status = HID.Open(ref m_hid, deviceNumber); // Attempt to open the device if (status == HID.HID_DEVICE_SUCCESS) { GetSilabVersion(m_hid); GetBTVersion(m_hid); reader = new RFIDReader(m_hid); } else { Console.WriteLine("Error connecting to CS108 through HID. Exit program."); return; } ConsoleKeyInfo KeyPress; Console.WriteLine("Press any key to start inventory and press ESC to stop..."); Console.ReadKey(); byte[] command = RFIDCommands.PowerOn(true); if (!USBSocket.TransmitData(m_hid, command, command.Length)) { Console.WriteLine("Device failed to power on"); return; } //enable battery reporting notifications command = NotifyCommands.SetBatteryReport(true); if (!USBSocket.TransmitData(m_hid, command, command.Length)) { Console.WriteLine("Device failed to enable battery reporting."); return; } if (!reader.setPowerAndChannel()) { Console.WriteLine("Error settign power and frequency channel. Exit program."); return; } if (!reader.setInventoryConfig()) { Console.WriteLine("Error setting inventory configurations. Exit program."); return; } Thread.Sleep(500); reader.StartInventory(); while (true) { KeyPress = Console.ReadKey(); if (KeyPress.Key == ConsoleKey.Escape) { break; } } //stop inventory reader.StopInventory(); while (reader.isReading) { ; } //power off RFID module command = RFIDCommands.PowerOn(false); if (!USBSocket.TransmitData(m_hid, command, command.Length)) { Console.WriteLine("Device failed to power off"); } //disable battery reporting notifications command = NotifyCommands.SetBatteryReport(false); if (!USBSocket.TransmitData(m_hid, command, command.Length)) { Console.WriteLine("Device failed to disable battery reporting."); return; } //Stop RFID data processing reader.close(); Thread.Sleep(1000); //close USB connection HID.Close(m_hid); Console.WriteLine("All operations completed. Press any key to exit program..."); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } }