import pywinusb.hid as hid import sys # --------------------------------------------------------- # Print the data def readRX_handler(data): print("+" * 80) print('-- Info :: Receive Data::' + str(data)) print("-- Info :: Raw data: {0}".format(data)) print("+" * 80) # --------------------------------------------------------- # Get the data from the LCR MEter def getAT826(at826_vendor_id): # Find the AT826 devices of the vendor id all_devices = hid.HidDeviceFilter(vendor_id=at826_vendor_id).get_devices() if not all_devices: print("-- Error :: No device with the vendor id connected {0}").format(at826_vendor_id) else: # search for the AT826 # Open for at826device in all_devices: # try: try: at826device.open() print("-- Info :: Open Device :: {0.vendor_name} {0.product_name}".format(at826device)) #target_usage = hid.get_full_usage_id(0x0825, 0x0826) #print('-- Info :: Target Usage Value :: ' + repr(target_usage)) # generic vendor page, usage_id = 2 target_usage = hid.get_full_usage_id(0x0825, 0x0001 ) print('-- Info :: Target Usage Value :: ' + repr(target_usage)) print("-" * 80) # browse feature reports # Not implemented with AT826 print('-- try to get Feature Report') for freport in at826device.find_feature_reports(): print('-- get Feature Report :: ' + freport.get()) print('-- get Feature Report Raw:: ' + str(freport.get_raw_data()[:])) if target_usage in freport: # we found our usage freport.get() # print result print("The value:", list(freport[target_usage])) print("All the report: {0}".format(freport.get_raw_data())) print("-" * 80) # browse output reports for out_report in at826device.find_output_reports(): if target_usage in out_report: print("-- Info :: Target Usage detected") else: print("-- Info :: No Target Usage detected") # print(len(out_report)) # print(repr(out_report)) out_buffer = out_report.get_raw_data()[:] # print(len(out_buffer)) # print(out_buffer) # May be must be 1 ?? to trigger the send process of the api??? out_buffer[0] = 0x00 out_buffer[1] = 0x00 out_buffer[2] = 0x00 out_buffer[3] = 0x40 #get the Device information out_buffer[4] = ord('I') out_buffer[5] = ord('D') out_buffer[6] = ord('N') out_buffer[7] = ord('?') # nSignauture out_buffer[56] = 0x88 out_buffer[57] = 0x80 out_buffer[58] = 0x55 out_buffer[59] = 0x50 # nChecksum out_buffer[60] = 0x00 out_buffer[61] = 0x00 out_buffer[62] = 0x2B out_buffer[63] = 0xC1 # print(len(out_buffer)) # print(out_buffer) for i in range(64): print("-- Read buffer {0:02d} => {1}".format(i, hex(out_buffer[i]))) # Attach a custom handler when a data is received at826device.set_raw_data_handler(readRX_handler) print("-- Info :: send Buffer Data") out_report.set_raw_data(out_buffer) out_report.send() # at826device.send_output_report(out_buffer) out_buffer = out_report.get_raw_data()[:] print(out_buffer) if target_usage in out_report: print("-- Info ::target_usage detected") print("-" * 80) at826device.close() except: print('close') at826device.close() # --------------------------------------------------------- # list all devices on the maschine for the HID USB Interface def showAllHDIDef(): all_devices = hid.find_all_hid_devices() print("-- Show all attached devices --") for device in all_devices: print("-- Info :: fond :: " + str(device)) print("-" * 80) if __name__ == '__main__': # Debug, get all informations about all devices # if sys.version_info < (3,): # import codecs # # ou#tput = codecs.getwriter('mbcs')(sys.stdout) # else: # # python3, you have to deal with encodings, try redirecting to any file # output = sys.stdout # try: # hid.core.show_hids(output=output) # except UnicodeEncodeError: # print("\nError: Can't manage encodings on terminal, try to run the script on PyScripter or IDLE") # # Vendor ID of the device at826_vendor_id = 0x0825 # Read all devices showAllHDIDef() # Get the LCR getAT826(at826_vendor_id)