scanlab, зараза, отказывается отправлять спеки... Ок, пойдем трудным путем...
Взял я usb сниффер и наснифил следующее:
Видно, что комп отправляет:
Код: Выделить всё
00000000 14 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000010 00 00 00 00 00 C7 09 16 00 .........
На что получает ответ:
Код: Выделить всё
00000000 94 00 06 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000010 00 00 00 00 00 87 09 00 00 .........
где 87 09 - желаемая величина.
Теперь вопрос, как все это послать плате?
Нашел такую штуку:
https://github.com/walac/pyusb/blob/mas ... torial.rst
Но с ней не удается прочитать из девайса - вроде как ничего не отправляет...
Код: Выделить всё
Traceback (most recent call last):
File "1.py", line 138, in <module>
main()
File "1.py", line 109, in main
dev.write(1, raw)
File "/usr/local/lib/python2.6/dist-packages/usb/core.py", line 921, in write
self.__get_timeout(timeout)
File "/usr/local/lib/python2.6/dist-packages/usb/backend/libusb0.py", line 528, in bulk_write
data, timeout)
File "/usr/local/lib/python2.6/dist-packages/usb/backend/libusb0.py", line 611, in __write
timeout
File "/usr/local/lib/python2.6/dist-packages/usb/backend/libusb0.py", line 425, in _check
raise USBError(errmsg, ret)
usb.core.USBError: [Errno None] Connection timed out
Вот мой код:
Код: Выделить всё
#!/usr/bin/python
import os
import sys
import time
import usb.core
import usb.util
packet_len = 64
# Packing a request. Please see HexWax documentation for the list of all commands
# Packets are 64 bytes long, most of the commands are 4 bytes long. So up to 18
# can be batched into a packet. For example command with bytes [0x94, 0x0, 0x0, 0x0]
# is getting firmware id
def pack_request(*arguments):
packet = [0x0] * packet_len
i = 0
for arg in arguments:
packet[i] = arg
i += 1
#packet[0:4] = [0x94, 0x0, 0x0, 0x0] #get firmware id
return ''.join([chr(c) for c in packet])
# Logs error to the error output
def log_error(functionName, ret):
sys.stderr.write(functionName + (" failed with return code %d\n" % ret))
# Logs result onto standard output. Result is 64 bytes as decimal numbers
# Response is 64 bytes long
def show_result(bytes):
print ("Result:")
print (''.join(['%d ' % abyte for abyte in bytes]))
# Turns LED on the bord on or off depending on input parameter on. 0 is turning
# the led on 1 is turning it off. The command is 0x9F set port bit (set output
# pin value), port is 0x03 (port C), 0x06 is bit index (so this is 7th bit),
# and the last bit is 0 for clear, 1 for set
def set_led(on, dev):
if on:
param = 0x00
else:
param = 0x01
raw = pack_request(0x9F, 0x03, 0x06, param) #set port bit - 0 to turn it on 1 to turn it off
dev.write(1, raw, 0, 100)
bytes = dev.read(0x81, packet_len, 0, 100)
show_result(bytes)
def main():
#initialising debuging - don't have a clue if this will work
os.environ['PYUSB_LOG_FILENAME'] = 'debug'
dev = usb.core.find(idVendor=0x0451, idProduct=0x9001)
cfg = dev.get_active_configuration()
alt = usb.util.find_descriptor(cfg, find_all=True, bInterfaceNumber=1)
print alt
intf = cfg[(0,0)]
ep = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
print ep
# was it found?
if dev is None:
raise ValueError('Device not found')
try:
# dev.detach_kernel_driver(0)
print "ok"
except: # this usually mean that kernel driver has already been dettached
print "not ok"
pass
# set the active configuration, the device has 2 configurations you can see them
# by using: lsusb -vvv -d 0x0b40:
# this device has configurations 1 and 2 I'm using the first one, not sure at the
# moment if 2nd would work the same
dev.set_configuration(1)
#removed following lines if you're running this from command line
#my netbeans didn't want to show standard output so I had to redirect it to
#a file
# fsock = open('out.log', 'w')
# sys.stdout = fsock
# fsock2 = open('out.err', 'w')
# sys.stderr = fsock2
#prepare our own command - this is get firmware id, only the first byte is
#significant
raw = pack_request(0x14,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC7,0x09,0x16,0x00)
#send the packet
dev.write(1, raw)
raw = pack_request(0x94, 0x00, 0x06, 0x00, 0x00)
dev.write(1, raw)
#then read the result
try:
bytes = dev.read(1, 1, 1000)
show_result(bytes)
except:
pass
#prepare another request
#set register TRISC bit 6 - port C bit 6 to be output
#mind though 0x9B command can be used to write any register, so check the
#microcontroller's datasheet
# raw = pack_request(0x94, 0x00, 0x06, 0x00, 0X00)
# dev.write(1, raw, 0, 100)
# bytes = dev.read(packet_len, 100)
# show_result(bytes)
if __name__ == '__main__':
main()