-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_print.py
218 lines (151 loc) · 5.49 KB
/
usb_print.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import usb.core
import usb.util
import usb.backend
import sys
import time
VENDOR_ID = 0x10c4 # nhiet do
PRODUCT_ID = 0xea60
dev = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
# was it found?
if dev is None:
raise ValueError('Device not found')
# cfg = usb.util.find_descriptor(dev)
# cfg.set()
# dev.set_configuration(cfg)
# dev.reset()
endpoint = dev[0][(0,0)][0]
print "Endpoint: %s address: %s" % (endpoint, endpoint.bEndpointAddress)
#make sure the hiddev kernel driver is not active
try:
if dev.is_kernel_driver_active(0):
print "detaching device..."
dev.detach_kernel_driver(0)
except usb.core.USBError as e:
sys.exit("Could not detatch kernel driver: %s" % str(e))
dev.set_configuration()
# dev.ctrl_transfer(0xC1, 0xB, 0x1FD, 0, 0)
# data = dev.ctrl_transfer(0xC1,0x10,0x310B,0, 0x14)
# print data
# for bRequest in range(255):
# try:
# data = dev.ctrl_transfer(0xC0,0xFF,0x370B,0, 0x1)
# # ret = dev.ctrl_transfer(0xC0, bRequest, 0, 0, 1)
# print "bRequest ",bRequest
# print ret
# except:
# # failed to get data for this request
# pass
# while True:
# data = dev.ctrl_transfer(0xC1,0x10,0x0,0x0,14)
# print data
# time.sleep(1)
data = dev.ctrl_transfer(0xC0,0xFF,0x370B,0x0, 0x1)
data = dev.ctrl_transfer(0x41,0x0,0x1,0x0)
data = dev.ctrl_transfer(0xC1,0x8,0x0,0x0,0x1)
data = dev.read(0x81, endpoint.wMaxPacketSize, 0, 0x0)
dev.ctrl_transfer(0xC1,0x8,0x0,0x0,0x4) # In 00
dev.ctrl_transfer(0x41,0x0B,0x01FD,0x0) # Out
dev.ctrl_transfer(0x41,0x12,0xF,0x0) # Out
dev.ctrl_transfer(0x41,0x0B,0x1FD,0x0) # Out
dev.ctrl_transfer(0x41,0x0B,0x1FD,0x0) # Out
# Get temperature and wind speed
dev.ctrl_transfer(0x41,0x1E,0x1FD,0x0, [0x80, 0x25, 0x0, 0x0]) # Out 80 25 00 00
dev.ctrl_transfer(0x41,0x07,0x0202,0x0) # Out
dev.ctrl_transfer(0x41,0x07,0x101,0x0) # Out
dev.ctrl_transfer(0x41,0x03,0x800,0x0) # Out
dev.ctrl_transfer(0x41,0x19,0x0,0x0, [0x1A, 00, 00, 00, 0x11, 0x13]) # Out
dev.ctrl_transfer(0x41,0x13,0x0,0x0, [0x1, 00, 00, 00, 0x4, 00, 00, 00, 0x01, 00, 00, 00, 0x01, 00, 00]) # Out
dev.ctrl_transfer(0xC1,0x8,0x0,0x0,0x2) # In 03
dev.ctrl_transfer(0xC1,0x8,0x0,0x0,0x2) # In 03
dev.ctrl_transfer(0xC1,0x8,0x0,0x0,0x2) # In 03
dev.ctrl_transfer(0xC1,0x8,0x0,0x0,0x2) # In 03
dev.ctrl_transfer(0xC1,0x10,0x0,0x0,0x14) # In 08, 00, ...
dev.ctrl_transfer(0xC1,0x8,0x0,0x0,0x2) # In 03
dev.ctrl_transfer(0xC1,0x8,0x0,0x0,0x2) # In 03
dev.ctrl_transfer(0xC1,0x8,0x0,0x0,0x2) # In 03
dev.ctrl_transfer(0xC1,0x8,0x0,0x0,0x2) # In 03
dev.read(0x81, endpoint.wMaxPacketSize, 0, 1000) # 1 bytes 00
dev.read(0x81, endpoint.wMaxPacketSize, 0, 1000) # 512 bytes = 0x200
block = []
print 'MaxPacketSize: %s' % endpoint.wMaxPacketSize
show = False;
while True:
dev.ctrl_transfer(0x41,0x0B,0x01FD,0x0) # Out
dev.ctrl_transfer(0xC1,0x10,0x0,0x0,0x14) # In 04, 00, 00, 00, 00, 00, 00, 01, 00 ....
data = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, 0, 1000) # 512 bytes = 0x200
print data;
if len(data) == 1 and data[0] == 0:
show = True
time.sleep(0.4)
continue
if len(data) == 12 and show == True:
hexdata = []
for i, v in enumerate(data):
hexdata.append("%0.2X" % v);
print hexdata
show = False
# n = 0
# for i,v in enumerate(data):
# # check "i" is first character or not continue to loop
# if not block and v != 0:
# continue
# elif i+1 == len(data):
# continue
# elif data[i+1] == 0:
# continue
# block.append(v)
# if (len(block) == 13):
# print block
# block[2] # humidity
# block[7] # temperature
# block[8] # mode (unit) windspeed
# block[9] # change way to show windspeed
# block[10] # windspeed number
# block[11] # decimal wind speed ?
# block = []
time.sleep(0.3)
# data = dev.ctrl_transfer(0xC1,0x8,0x0,0x0,1)
# print data
# data = dev.ctrl_transfer(0xC1,0x8,0x0,0x0,4)
# print data
# data = dev.ctrl_transfer(0x41,0x0B,0x1FD,0x0)
# print data
# data = dev.ctrl_transfer(0x41,0x12,0xF,0x0)
# print data
# data = dev.ctrl_transfer(0x41,0x0B,0x1FD,0x0)
# print data
# data = dev.ctrl_transfer(0x41,0x0B,0x1FD,0x0, [128, 37, 0, 0])
# print data
# data = dev.ctrl_transfer(0x41,0x0B,0x1FD,0x0, [])
# print data
# while True:
# # data = dev.ctrl_transfer(0x41,0x19,0x1FD,0x0, [26, 0, 0, 0, 17, 19])
# # print data
# # data = dev.ctrl_transfer(0x41,0x13,0x1FD,0x0, [1, 0, 0, 0, 4, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0])
# # print data
# # data = dev.ctrl_transfer(0xC1,0x10,0x0,0x0, 0x14)
# # print data
# # data = dev.ctrl_transfer(0xC1,0x8,0x0,0x0, 2)
# # print data
# dev.set_configuration()
# data = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, 0, 100)
# print data
# data = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, 0, 100000)
# print data
# blank = False
# raw = False
# while True:
# print "End Point: %s" % endpoint.bEndpointAddress
# data = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, 0, 1000)
# print 1
# if raw == True:
# print(data)
# elif data[0] != 240:
# line = ""
# for value in data:
# line += str(value) + " " * (4 - len(str(value)))
# print(line)
# blank = False
# elif blank == False:
# blank = True
# print("")