|
Revision 454:3333270c2a78, 1.3 kB
(checked in by Maxime Petazzoni <maxime.petazzoni@…>, 14 months ago)
|
|
Improved USB dump reading utility.
|
|
|
| Line | |
|---|
| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | # A small utility to read data from the brick through the USB |
|---|
| 4 | # connection. This script expects the following communication |
|---|
| 5 | # protocol: |
|---|
| 6 | # |
|---|
| 7 | # A data size (U32, 4 bytes), immediately followed by the data itself, |
|---|
| 8 | # <data size> bytes. |
|---|
| 9 | |
|---|
| 10 | import nxt |
|---|
| 11 | import struct |
|---|
| 12 | import sys |
|---|
| 13 | from nxt.lowlevel import get_device |
|---|
| 14 | |
|---|
| 15 | NXOS_INTERFACE = 0 |
|---|
| 16 | |
|---|
| 17 | def main(): |
|---|
| 18 | print "Looking for NXT...", |
|---|
| 19 | brick = get_device(0x0694, 0xFF00, timeout=60) |
|---|
| 20 | if not brick: |
|---|
| 21 | print "not found!" |
|---|
| 22 | return False |
|---|
| 23 | |
|---|
| 24 | brick.open(NXOS_INTERFACE) |
|---|
| 25 | print "ok." |
|---|
| 26 | |
|---|
| 27 | # Read data size |
|---|
| 28 | print "Waiting for data size...", |
|---|
| 29 | read_size = brick.read(4, 5000) |
|---|
| 30 | if not read_size: |
|---|
| 31 | print "timeout!" |
|---|
| 32 | return False |
|---|
| 33 | |
|---|
| 34 | size = struct.unpack("<L", read_size)[0] |
|---|
| 35 | print "ok. Expecting %d bytes." % size |
|---|
| 36 | |
|---|
| 37 | # Receive data |
|---|
| 38 | print "Receiving data...", |
|---|
| 39 | data = brick.read(size, 10000) |
|---|
| 40 | if not data: |
|---|
| 41 | print "timeout!" |
|---|
| 42 | return False |
|---|
| 43 | print "ok." |
|---|
| 44 | |
|---|
| 45 | data = [ ord(i) for i in data ] |
|---|
| 46 | |
|---|
| 47 | if len(sys.argv) > 1: |
|---|
| 48 | if sys.argv[1] == 'i2c': |
|---|
| 49 | from i2c_pin_data import beautify |
|---|
| 50 | beautify(data, size) |
|---|
| 51 | elif sys.argv[1] == 'ascii': |
|---|
| 52 | from ascii_dump import beautify |
|---|
| 53 | beautify(data, size) |
|---|
| 54 | else: |
|---|
| 55 | print [ str(i) for i in data ] |
|---|
| 56 | |
|---|
| 57 | return True |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | if __name__ == "__main__": |
|---|
| 61 | main() |
|---|