项目地址:USB-HID-TEST
整体预览图(win8下的效果):
======================
项目结构:
COM
--hidHelper.py
--usbHelper.py
UI
--Adaptive.py
--HID_TESTUI.py
--PyTkinter.py
main.py
======================
UI文件夹:
使用PyTkinter来初始化一些颜色配置(个人喜好)
实现代码:
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 ''' 5 Tkinter控件初始化配置(默认为深色) 6 ''' 7 __author__ = "jakey.chen" 8 __version__ = "v1.0" 9 10 11 import Tkinter as tk 12 13 g_default_theme = "dark" 14 # g_default_theme = "default" 15 16 class PyButton(tk.Button): 17 ''' 18 Button 19 ''' 20 def __init__(self, master, theme=g_default_theme, **kv): 21 self.theme = theme 22 self.kv = kv 23 self.temp = dict() 24 self.choose_theme() 25 tk.Button.__init__(self, master, self.temp) 26 27 def choose_theme(self): 28 if self.theme == "dark": 29 dark_theme_dict = { 30 "activebackground": "#00B2EE", 31 "activeforeground": "#E0EEEE", 32 "bg": "#008B8B", 33 "fg": "#FFFFFF" 34 } 35 for key,value in dark_theme_dict.items(): 36 self.temp[key] = value 37 38 for key,value in self.kv.items(): 39 self.temp[key] = value 40 41 class PyLabel(tk.Label): 42 ''' 43 Label 44 ''' 45 def __init__(self, master, theme=g_default_theme, **kv): 46 self.theme = theme 47 self.kv = kv 48 self.temp = dict() 49 self.choose_theme() 50 tk.Label.__init__(self, master, self.temp) 51 52 def choose_theme(self): 53 if self.theme == "dark": 54 dark_theme_dict = { 55 "bg": "#292929", 56 "fg": "#E0EEEE" 57 } 58 for key,value in dark_theme_dict.items(): 59 self.temp[key] = value 60 61 for key,value in self.kv.items(): 62 self.temp[key] = value 63 64 class PyLabelFrame(tk.LabelFrame): 65 ''' 66 Frame 67 ''' 68 def __init__(self, master, theme=g_default_theme, **kv): 69 self.theme = theme 70 self.kv = kv 71 self.temp = dict() 72 self.choose_theme() 73 tk.LabelFrame.__init__(self, master, self.temp) 74 75 def choose_theme(self): 76 if self.theme == "dark": 77 dark_theme_dict = { 78 "bg": "#292929", 79 "fg": "#1E90FF" 80 } 81 for key,value in dark_theme_dict.items(): 82 self.temp[key] = value 83 84 for key,value in self.kv.items(): 85 self.temp[key] = value 86 87 class PyListbox(tk.Listbox): 88 ''' 89 Listbox 90 ''' 91 def __init__(self, master, theme=g_default_theme, **kv): 92 self.theme = theme 93 self.kv = kv 94 self.temp = dict() 95 self.choose_theme() 96 tk.Listbox.__init__(self, master, self.temp) 97 98 def choose_theme(self): 99 if self.theme == "dark": 100 dark_theme_dict = { 101 "bg": "#292929", 102 "fg": "#1E90FF", 103 "selectbackground": "#00B2EE" 104 } 105 for key,value in dark_theme_dict.items(): 106 self.temp[key] = value 107 108 for key,value in self.kv.items(): 109 self.temp[key] = value 110 111 class PyText(tk.Text): 112 ''' 113 Text 114 ''' 115 def __init__(self, master, theme=g_default_theme, **kv): 116 self.theme = theme 117 self.kv = kv 118 self.temp = dict() 119 self.choose_theme() 120 tk.Text.__init__(self, master, self.temp) 121 122 def choose_theme(self): 123 if self.theme == "dark": 124 dark_theme_dict = { 125 "bg": "#292929", 126 "fg": "#1E90FF" 127 } 128 for key,value in dark_theme_dict.items(): 129 self.temp[key] = value 130 131 for key,value in self.kv.items(): 132 self.temp[key] = value 133 134 class PyCheckbutton(tk.Checkbutton): 135 ''' 136 Checkbutton 137 ''' 138 def __init__(self, master, theme=g_default_theme, **kv): 139 self.theme = theme 140 self.kv = kv 141 self.temp = dict() 142 self.choose_theme() 143 tk.Checkbutton.__init__(self, master, self.temp) 144 145 def choose_theme(self): 146 if self.theme == "dark": 147 dark_theme_dict = { 148 "bg": "#292929", 149 "fg": "#FFFFFF", 150 "activebackground": "#292929", 151 "activeforeground": "#FFFFFF", 152 "selectcolor": "#292929" 153 } 154 for key,value in dark_theme_dict.items(): 155 self.temp[key] = value 156 157 for key,value in self.kv.items(): 158 self.temp[key] = value 159 160 class PyRadiobutton(tk.Radiobutton): 161 ''' 162 Radiobutton 163 ''' 164 def __init__(self, master, theme=g_default_theme, **kv): 165 self.theme = theme 166 self.kv = kv 167 self.temp = dict() 168 self.choose_theme() 169 tk.Radiobutton.__init__(self, master, self.temp) 170 171 def choose_theme(self): 172 if self.theme == "dark": 173 dark_theme_dict = { 174 "bg": "#292929", 175 "fg": "#FFFFFF", 176 "activebackground": "#292929", 177 "selectcolor": "#292929" 178 } 179 for key,value in dark_theme_dict.items(): 180 self.temp[key] = value 181 182 for key,value in self.kv.items(): 183 self.temp[key] = value 184 185 186 class PyEntry(tk.Entry): 187 ''' 188 Entry 189 ''' 190 def __init__(self, master, theme=g_default_theme, **kv): 191 self.theme = theme 192 self.kv = kv 193 self.temp = dict() 194 self.choose_theme() 195 tk.Entry.__init__(self, master, self.temp) 196 197 def choose_theme(self): 198 if self.theme == "dark": 199 dark_theme_dict = { 200 "bg": "#292929", 201 "fg": "#E0EEEE", 202 "insertbackground": "#E0EEEE" 203 } 204 for key,value in dark_theme_dict.items(): 205 self.temp[key] = value 206 207 for key,value in self.kv.items(): 208 self.temp[key] = value 209 210 if __name__ == '__main__': 211 root = tk.Tk() 212 root.configure(bg="#292929") 213 PyButton(root, text="1234", font=("Monaco", 12)).pack() 214 PyLabel(root, text="123", font=("Monaco", 15)).pack() 215 PyCheckbutton(root, text="123", font=("Monaco", 15)).pack() 216 PyEntry(root, font=("Monaco", 15)).pack() 217 PyText(root, font=("Monaco", 15), height=2, width=20).pack() 218 listbox_0 = PyListbox(root, height=2, font=("Monaco", 15)) 219 listbox_0.pack() 220 for i in range(2): 221 listbox_0.insert("end", i) 222 radio_intvar = tk.IntVar() 223 PyRadiobutton(root, text="001", variable=radio_intvar, value=0, font=("Monaco", 15)).pack() 224 PyRadiobutton(root, text="002", variable=radio_intvar, value=1, font=("Monaco", 15)).pack() 225 radio_intvar.set(1) 226 227 root.mainloop()
主界面构建不表,使用LabelFrame通过grid布局组成
具体自行查看项目代码
======================
COM文件夹
Windows下:
使用pywinusb
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 ''' 5 win下使用的HID设备通讯帮助类 6 ''' 7 __author__ = "jakey.chen" 8 __version__ = "v1.0" 9 10 11 import pywinusb.hid as hid 12 13 14 class hidHelper(object): 15 def __init__(self, vid=0x1391, pid=0x2111): 16 self.alive = False 17 self.device = None 18 self.report = None 19 self.vid = vid 20 self.pid = pid 21 22 def start(self): 23 ''' 24 开始,打开HID设备 25 ''' 26 _filter = hid.HidDeviceFilter(vendor_id = self.vid, product_id = self.pid) 27 hid_device = _filter.get_devices() 28 if len(hid_device) > 0: 29 self.device = hid_device[0] 30 self.device.open() 31 self.report = self.device.find_output_reports() 32 self.alive = True 33 34 def stop(self): 35 ''' 36 停止,关闭HID设备 37 ''' 38 self.alive = False 39 if self.device: 40 self.device.close() 41 42 def setcallback(self): 43 ''' 44 设置接收数据回调函数 45 ''' 46 if self.device: 47 self.device.set_raw_data_handler(self.read) 48 49 def read(self, data): 50 ''' 51 接收数据回调函数 52 ''' 53 print([hex(item).upper() for item in data[1:]]) 54 55 def write(self, send_list): 56 ''' 57 向HID设备发送数据 58 ''' 59 if self.device: 60 if self.report: 61 self.report[0].set_raw_data(send_list) 62 bytes_num = self.report[0].send() 63 return bytes_num 64 65 66 if __name__ == '__main__': 67 myhid = hidHelper() 68 myhid.start() 69 if myhid.alive: 70 myhid.setcallback() 71 send_list = [0x00 for i in range(65)] 72 myhid.write(send_list) 73 import time 74 time.sleep(0.5) 75 myhid.stop()
Linux(我使用的是Ubuntu14.04)下:
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 ''' 5 linux下使用的usb设备通讯帮助类 6 ''' 7 __author__ = "jakey.chen" 8 __version__ = "v1.0" 9 10 11 import threading 12 import usb.util 13 import usb.core 14 15 16 class usbHelper(object): 17 def __init__(self, vid=0x1391, pid=0x2111): 18 self.alive = False 19 self.handle = None 20 self.size = 64 21 self.vid = vid 22 self.pid = pid 23 24 def start(self): 25 ''' 26 开始,打开usb设备 27 ''' 28 self.dev = usb.core.find(idVendor=self.vid, idProduct=self.pid) 29 if self.dev != None: 30 self.ep_in = self.dev[0][(0,0)][0].bEndpointAddress 31 self.ep_out = self.dev[0][(0,0)][1].bEndpointAddress 32 self.size = self.dev[0][(0,0)][1].wMaxPacketSize 33 self.open() 34 self.alive = True 35 36 def stop(self): 37 ''' 38 停止,关闭usb设备,释放接口 39 ''' 40 self.alive = False 41 if self.handle: 42 self.handle.releaseInterface() 43 44 def open(self): 45 ''' 46 打开usb设备 47 ''' 48 busses = usb.busses() 49 for bus in busses: 50 devices = bus.devices 51 for device in devices: 52 if device.idVendor == self.vid and device.idProduct == self.pid: 53 self.handle = device.open() 54 # Attempt to remove other drivers using this device. 55 if self.dev.is_kernel_driver_active(0): 56 try: 57 self.handle.detachKernelDriver(0) 58 except Exception as e: 59 self.alive = False 60 try: 61 self.handle.claimInterface(0) 62 except Exception as e: 63 self.alive = False 64 65 def read(self, size=64, timeout=0): 66 ''' 67 读取usb设备发过来的数据 68 ''' 69 if size >= self.size: 70 self.size = size 71 72 if self.handle: 73 data = self.handle.interruptRead(self.ep_in, self.size, timeout) 74 75 try: 76 data_list = data.tolist() 77 return data_list 78 except: 79 return list() 80 81 def write(self, send_list, timeout=1000): 82 ''' 83 发送数据给usb设备 84 ''' 85 if self.handle: 86 bytes_num = self.handle.interruptWrite(self.ep_out, send_list, timeout) 87 return bytes_num 88 89 if __name__ == '__main__': 90 import time 91 dev = usbHelper() 92 93 dev.start() 94 95 send_list = [0xAA for i in range(64)] 96 dev.write(send_list) 97 # time.sleep(0.25) 98 while True: 99 try: 100 mylist = dev.read() 101 print mylist 102 if mylist[1] == 0x02: 103 break 104 except: 105 dev.stop() 106 break 107 dev.stop()
======================
主函数:界面逻辑。不表,自行查看。