Python 3.x 问:用Python向HID设备写入字符串?

Python 3.x 问:用Python向HID设备写入字符串?,python-3.x,linux,hid,raspberry-pi4,evdev,Python 3.x,Linux,Hid,Raspberry Pi4,Evdev,我有一个RFID阅读器,它有两个天线(ant1,ant2)连接到一个RPi4(raspbian)上,RPi4起着HID USB键盘的作用。我已设法阅读了它的天线作品,并在以下帖子中发表: Python3中使用evdev模块连续读取天线的代码如下: #!/usr/bin/python3 python3 # -*- coding: utf-8 -*- # This script reads the output from HID device rfid scanner # Make sure

我有一个RFID阅读器,它有两个天线(ant1,ant2)连接到一个RPi4(raspbian)上,RPi4起着HID USB键盘的作用。我已设法阅读了它的天线作品,并在以下帖子中发表:

  • Python3中使用
    evdev
    模块连续读取天线的代码如下:

    #!/usr/bin/python3 python3
    # -*- coding: utf-8 -*-
    
    # This script reads the output from HID device rfid scanner
    # Make sure evdev is installed or install it with: pip3 install evdev.
    # The Kernel on the Pi creates an input device which resides in /dev/input/.
    # You can find out the names and other attributes of different devices using:
    # cat /proc/bus/input/devices
    
    import sys
    import inspect
    import serial
    import time
    import random
    import csv
    
    import evdev
    from evdev import InputDevice, categorize, ecodes as e, list_devices
    
    device = evdev.InputDevice('/dev/input/event1')     # check the event id first with cat 
    
    # reserve the device or maybe not
    device.grab()
    
    # return the current line number
    def lineno():
        return inspect.currentframe().f_back.f_lineno
    
    CODE_MAP_CHAR = {
        'KEY_MINUS': "-",
        'KEY_SPACE': " ",    
        'KEY_U': "U",
        'KEY_W': "W",
        'KEY_BACKSLASH': "\\",
        'KEY_GRAVE': "`",
        'KEY_NUMERIC_STAR': "*",
        'KEY_NUMERIC_3': "3",
        'KEY_NUMERIC_2': "2",
        'KEY_NUMERIC_5': "5",
        'KEY_NUMERIC_4': "4",
        'KEY_NUMERIC_7': "7",
        'KEY_NUMERIC_6': "6",
        'KEY_NUMERIC_9': "9",
        'KEY_NUMERIC_8': "8",
        'KEY_NUMERIC_1': "1",
        'KEY_NUMERIC_0': "0",
        'KEY_E': "E",
        'KEY_D': "D",
        'KEY_G': "G",
        'KEY_F': "F",
        'KEY_A': "A",
        'KEY_C': "C",
        'KEY_B': "B",
        'KEY_M': "M",
        'KEY_L': "L",
        'KEY_O': "O",
        'KEY_N': "N",
        'KEY_I': "I",
        'KEY_H': "H",
        'KEY_K': "K",
        'KEY_J': "J",
        'KEY_Q': "Q",
        'KEY_P': "P",
        'KEY_S': "S",
        'KEY_X': "X",
        'KEY_Z': "Z",
        'KEY_q': "q",
        'KEY_w': "w",
        'KEY_e': "e",
        'KEY_r': "r",
        'KEY_t': "t",
        'KEY_z': "z",
        'KEY_u': "u",
        'KEY_i': "i",
        'KEY_o': "o",
        'KEY_p': "p",
        'KEY_a': "a",
        'KEY_s': "s",
        'KEY_d': "d",
        'KEY_f': "f",
        'KEY_g': "g",
        'KEY_h': "h",
        'KEY_j': "k",
        'KEY_l': "l",
        'KEY_y': "y",
        'KEY_x': "x",
        'KEY_c': "c",
        'KEY_v': "v",
        'KEY_b': "b",
        'KEY_n': "n",
        'KEY_m': "m",
        'KEY_KP4': "4",
        'KEY_KP5': "5",
        'KEY_KP6': "6",
        'KEY_KP7': "7",
        'KEY_KP0': "0",
        'KEY_KP1': "1",
        'KEY_KP2': "2",
        'KEY_KP3': "3",
        'KEY_KP8': "8",
        'KEY_KP9': "9",
        'KEY_5': "5",
        'KEY_4': "4",
        'KEY_7': "7",
        'KEY_6': "6",
        'KEY_1': "1",
        'KEY_0': "0",
        'KEY_3': "3",
        'KEY_2': "2",
        'KEY_9': "9",
        'KEY_8': "8",
        'KEY_LEFTBRACE': "[",
        'KEY_RIGHTBRACE': "]",    
        'KEY_COMMA': ",",
        'KEY_EQUAL': "=",    
        'KEY_SEMICOLON': ";",
        'KEY_APOSTROPHE': "'",
        'KEY_T': "T",
        'KEY_V': "V",
        'KEY_R': "R",
        'KEY_Y': "Y",
        'KEY_TAB': "\t",
        'KEY_DOT': ".",
        'KEY_SLASH': "/",
    }
    
    scancodes = {
        # Scancode: ASCIICode
        0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8',
        10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R',
        20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL',
        30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u';',
        40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N',
        50: u'M', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT'
    }
    
    def parse_key_to_char(val):
        return CODE_MAP_CHAR[val] if val in CODE_MAP_CHAR else ""
    
    if __name__ == "__main__":
            
        event_id = 1    # set event id for lcd
        exclusive_access = 1    # grant exclusive access
    
        device = InputDevice('/dev/input/event{}'.format(event_id))
        if int(exclusive_access) == 1:
            device.grab()
            
        obidList = []   # set list for the reply string
        #print(lineno(), "obidList:", obidList)
        
        #get reply
        for event in device.read_loop():
            if event.type == evdev.ecodes.EV_KEY:
                e = categorize(event)
                if e.keystate == e.key_up:
                    sys.stdout.write(parse_key_to_char(e.keycode))
                    cifra = parse_key_to_char(e.keycode)
                    obidList.append(cifra)
                    print(lineno(), obidList)
                    sys.stdout.flush()
                if e.scancode == 28:
                    obidString = "".join(obidList)  # convert the list into a string
                    obidString = obidString.strip() # strip of eventual white spaces
                    print(lineno(), obidString)
                    break
    
    上述代码的输出为:

    (py3) pi@raspberrypi:~/Desktop/Python3 $ python obid_read.py 
    device /dev/input/event1, name "FEIG ELECTRONIC GmbH OBID RFID-Reader", phys "usb-0000:01:00.0-1.3/input1" 
    
    10004597E7A656780,02204 bag, antenna2: 004597E7A656780,02
    208 obidCount: 1
    0004597E7A656780,020004597E7A656780,01197 cartridge, antenna1: 004597E7A656780,020004597E7A656780,020004597E7A656780,01
    201 obidCount: 2
    004597E7A656780,(py3) pi@raspberrypi:~/Desktop/Python3 $ 004597E7A656780,02
    
    为了控制读卡器并能够读取单个天线,我必须向设备发送一个字符串。字符串为十六进制,需要转换为字节fist:

    # strings to send
    hex_string_ant1 = "02 00 0A FF B0 01 10 01 EF 14"
    hex_string_ant2 = "02 00 0A FF B0 01 10 02 74 26"
    byte_string_1 = bytearray.fromhex(hex_string_ant1)
    byte_string_2 = bytearray.fromhex(hex_string_ant2)
    
    我已经查看了
    evdev
    文档,其中包括但看不到如何关联。如何使用evedev向设备发送字节字符串