USB调试工具(python2.7+Tkinter+pyusb/pywinusb)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

项目地址: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 python2#-*- coding: utf-8 -*-34'''5Tkinter控件初始化配置(默认为深色)6'''7__author__="jakey.chen"8__version__="v1.0"91011importTkinter as tk1213g_default_theme ="dark"14#g_default_theme = "default"1516classPyButton(tk.Button):17'''18Button19'''20def__init__(self, master, theme=g_default_theme, **kv):21self.theme =theme22self.kv =kv23self.temp =dict()24self.choose_theme()25tk.Button.__init__(self, master, self.temp)2627defchoose_theme(self):28ifself.theme =="dark":29dark_theme_dict ={30"activebackground":"#00B2EE",31"activeforeground":"#E0EEEE",32"bg":"#008B8B",33"fg":"#FFFFFF"34}35forkey,valueindark_theme_dict.items():36self.temp[key] =value3738forkey,valueinself.kv.items():39self.temp[key] =value4041classPyLabel(tk.Label):42'''43Label44'''45def__init__(self, master, theme=g_default_theme, **kv):46self.theme =theme47self.kv =kv48self.temp =dict()49self.choose_theme()50tk.Label.__init__(self, master, self.temp)5152defchoose_theme(self):53ifself.theme =="dark":54dark_theme_dict ={55"bg":"#292929",56"fg":"#E0EEEE"57}58forkey,valueindark_theme_dict.items():59self.temp[key] =value6061forkey,valueinself.kv.items():62self.temp[key] =value6364classPyLabelFrame(tk.LabelFrame):65'''66Frame67'''68def__init__(self, master, theme=g_default_theme, **kv):69self.theme =theme70self.kv =kv71self.temp =dict()72self.choose_theme()73tk.LabelFrame.__init__(self, master, self.temp)7475defchoose_theme(self):76ifself.theme =="dark":77dark_theme_dict ={78"bg":"#292929",79"fg":"#1E90FF"80}81forkey,valueindark_theme_dict.items():82self.temp[key] =value8384forkey,valueinself.kv.items():85self.temp[key] =value8687classPyListbox(tk.Listbox):88'''89Listbox90'''91def__init__(self, master, theme=g_default_theme, **kv):92self.theme =theme93self.kv =kv94self.temp =dict()95self.choose_theme()96tk.Listbox.__init__(self, master, self.temp)9798defchoose_theme(self):99ifself.theme =="dark":100dark_theme_dict ={101"bg":"#292929",102"fg":"#1E90FF",103"selectbackground":"#00B2EE"104}105forkey,valueindark_theme_dict.items():106self.temp[key] =value107108forkey,valueinself.kv.items():109self.temp[key] =value110111classPyText(tk.Text):112'''113Text114'''115def__init__(self, master, theme=g_default_theme, **kv):116self.theme =theme117self.kv =kv118self.temp =dict()119self.choose_theme()120tk.Text.__init__(self, master, self.temp)121122defchoose_theme(self):123ifself.theme =="dark":124dark_theme_dict ={125"bg":"#292929",126"fg":"#1E90FF"127}128forkey,valueindark_theme_dict.items():129self.temp[key] =value130131forkey,valueinself.kv.items():132self.temp[key] =value133134classPyCheckbutton(tk.Checkbutton):135'''136Checkbutton137'''138def__init__(self, master, theme=g_default_theme, **kv):139self.theme =theme140self.kv =kv141self.temp =dict()142self.choose_theme()143tk.Checkbutton.__init__(self, master, self.temp)144145defchoose_theme(self):146ifself.theme =="dark":147dark_theme_dict ={148"bg":"#292929",149"fg":"#FFFFFF",150"activebackground":"#292929",151"activeforeground":"#FFFFFF",152"selectcolor":"#292929"153}154forkey,valueindark_theme_dict.items():155self.temp[key] =value156157forkey,valueinself.kv.items():158self.temp[key] =value159160classPyRadiobutton(tk.Radiobutton):161'''162Radiobutton163'''164def__init__(self, master, theme=g_default_theme, **kv):165self.theme =theme166self.kv =kv167self.temp =dict()168self.choose_theme()169tk.Radiobutton.__init__(self, master, self.temp)170171defchoose_theme(self):172ifself.theme =="dark":173dark_theme_dict ={174"bg":"#292929",175"fg":"#FFFFFF",176"activebackground":"#292929",177"selectcolor":"#292929"178}179forkey,valueindark_theme_dict.items():180self.temp[key] =value181182forkey,valueinself.kv.items():183self.temp[key] =value184185186classPyEntry(tk.Entry):187'''188Entry189'''190def__init__(self, master, theme=g_default_theme, **kv):191self.theme =theme192self.kv =kv193self.temp =dict()194self.choose_theme()195tk.Entry.__init__(self, master, self.temp)196197defchoose_theme(self):198ifself.theme =="dark":199dark_theme_dict ={200"bg":"#292929",201"fg":"#E0EEEE",202"insertbackground":"#E0EEEE"203}204forkey,valueindark_theme_dict.items():205self.temp[key] =value206207forkey,valueinself.kv.items():208self.temp[key] =value209210if__name__=='__main__':211root =tk.Tk()212root.configure(bg="#292929")213PyButton(root, text="1234", font=("Monaco", 12)).pack()214PyLabel(root, text="123", font=("Monaco", 15)).pack()215PyCheckbutton(root, text="123", font=("Monaco", 15)).pack()216PyEntry(root, font=("Monaco", 15)).pack()217PyText(root, font=("Monaco", 15), height=2, width=20).pack()218listbox_0 = PyListbox(root, height=2, font=("Monaco", 15))219listbox_0.pack()220foriinrange(2):221listbox_0.insert("end", i)222radio_intvar =tk.IntVar()223PyRadiobutton(root, text="001", variable=radio_intvar, value=0, font=("Monaco", 15)).pack()224PyRadiobutton(root, text="002", variable=radio_intvar, value=1, font=("Monaco", 15)).pack()225radio_intvar.set(1)226227root.mainloop()

主界面构建不表,使用LabelFrame通过grid布局组成

具体自行查看项目代码

======================

COM文件夹

Windows下:

使用pywinusb

1#! /usr/bin/env python2#-*- coding: utf-8 -*-34'''5win下使用的HID设备通讯帮助类6'''7__author__="jakey.chen"8__version__="v1.0"91011importpywinusb.hid as hid121314classhidHelper(object):15def__init__(self, vid=0x1391, pid=0x2111):16self.alive =False17self.device =None18self.report =None19self.vid =vid20self.pid =pid2122defstart(self):23'''24开始,打开HID设备25'''26_filter = hid.HidDeviceFilter(vendor_id = self.vid,PRoduct_id =self.pid)27hid_device =_filter.get_devices()28iflen(hid_device) >0:29self.device =hid_device[0]30self.device.open()31self.report =self.device.find_output_reports()32self.alive =True3334defstop(self):35'''36停止,关闭HID设备37'''38self.alive =False39ifself.device:40self.device.close()4142defsetcallback(self):43'''44设置接收数据回调函数45'''46ifself.device:47self.device.set_raw_data_handler(self.read)4849defread(self, data):50'''51接收数据回调函数52'''53print([hex(item).upper()foritemindata[1:]])5455defwrite(self, send_list):56'''57向HID设备发送数据58'''59ifself.device:60ifself.report:61self.report[0].set_raw_data(send_list)62bytes_num =self.report[0].send()63returnbytes_num646566if__name__=='__main__':67myhid =hidHelper()68myhid.start()69ifmyhid.alive:70myhid.setcallback()71send_list = [0x00foriinrange(65)]72myhid.write(send_list)73importtime74time.sleep(0.5)75myhid.stop()

linux(我使用的是Ubuntu14.04)下:

1#! /usr/bin/env python2#-*- coding: utf-8 -*-34'''5linux下使用的usb设备通讯帮助类6'''7__author__="jakey.chen"8__version__="v1.0"91011importthreading12importusb.util13importusb.core141516classusbHelper(object):17def__init__(self, vid=0x1391, pid=0x2111):18self.alive =False19self.handle =None20self.size = 6421self.vid =vid22self.pid =pid2324defstart(self):25'''26开始,打开usb设备27'''28self.dev = usb.core.find(idVendor=self.vid, idProduct=self.pid)29ifself.dev !=None:30self.ep_in =self.dev[0][(0,0)][0].bEndpointAddress31self.ep_out = self.dev[0][(0,0)][1].bEndpointAddress32self.size = self.dev[0][(0,0)][1].wMaxPacketSize33self.open()34self.alive =True3536defstop(self):37'''38停止,关闭usb设备,释放接口39'''40self.alive =False41ifself.handle:42self.handle.releaseInterface()4344defopen(self):45'''46打开usb设备47'''48busses =usb.busses()49forbusinbusses:50devices =bus.devices51fordeviceindevices:52ifdevice.idVendor == self.vidanddevice.idProduct ==self.pid:53self.handle =device.open()54#Attempt to remove other drivers using this device.55ifself.dev.is_kernel_driver_active(0):56try:57self.handle.detachKernelDriver(0)58exceptException as e:59self.alive =False60try:61self.handle.claimInterface(0)62exceptException as e:63self.alive =False6465defread(self, size=64, timeout=0):66'''67读取usb设备发过来的数据68'''69ifsize >=self.size:70self.size =size7172ifself.handle:73data =self.handle.interruptRead(self.ep_in, self.size, timeout)7475try:76data_list =data.tolist()77returndata_list78except:79returnlist()8081defwrite(self, send_list, timeout=1000):82'''83发送数据给usb设备84'''85ifself.handle:86bytes_num =self.handle.interruptWrite(self.ep_out, send_list, timeout)87returnbytes_num8889if__name__=='__main__':90importtime91dev =usbHelper()9293dev.start()9495send_list = [0xAAforiinrange(64)]96dev.write(send_list)97#time.sleep(0.25)98whileTrue:99try:100mylist =dev.read()101printmylist102ifmylist[1] == 0x02:103

break104except:105dev.stop()106

break107dev.stop()

======================

主函数:界面逻辑。不表,自行查看。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航