52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
import usb.core
|
|
import usb.util
|
|
import time
|
|
import requests
|
|
import json
|
|
|
|
USB_IF = 0 # Interface
|
|
USB_TIMEOUT = 5 # Timeout in MS
|
|
USB_VENDOR = 0xffff # Vendor-ID:
|
|
USB_PRODUCT = 0x0035 # Product-ID
|
|
|
|
# Find the HID device by vender/product ID
|
|
dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
|
|
|
|
# Get and store the endpoint
|
|
endpoint = dev[0][(0,0)][0]
|
|
|
|
if dev.is_kernel_driver_active(USB_IF) is True:
|
|
dev.detach_kernel_driver(USB_IF)
|
|
|
|
# Claim the device
|
|
usb.util.claim_interface(dev, USB_IF)
|
|
|
|
receivedNumber = 0
|
|
while True:
|
|
control = None
|
|
|
|
try:
|
|
# Read a character from the device
|
|
control = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT)
|
|
# Here you have to analyze what's coming in.
|
|
# In my case you had to check the first byte (command)
|
|
if (control[2] != 40) & (control[2] != 0):
|
|
# Convert ascii to a number, there's probably better ways to do so.
|
|
receivedDigit = control[2] - 29
|
|
|
|
if receivedDigit == 10:
|
|
receivedDigit = 0
|
|
|
|
# Append the digit to the number
|
|
receivedNumber = 10 * receivedNumber + receivedDigit
|
|
|
|
# Check if the received character is CRLF
|
|
if (( control[0] == 0 )) & (( control[2] == 40 )) & (( not receivedNumber == 0 )):
|
|
print('cardNumber:', receivedNumber)
|
|
except KeyboardInterrupt:
|
|
exit()
|
|
except:
|
|
pass
|
|
|
|
time.sleep(0.001) # Let CTRL+C actually exit
|