/ usbtest.py
usbtest.py
1 #!/usr/bin/env python2.7 2 # 3 # Original version supplied to me (Kano/kanoi) by xiangfu 4 # 5 # Modified to allow supplying the data to send 6 # 7 # Linux usAge: ./ubstest.py /dev/ttyUSB0 0xhexcodes|string|icarus 8 # OR python ubstest.py /dev/ttyUSB0 0xhexcodes|string|icarus 9 # 10 # Windows usAge: ./ubstest.py COM1 0xhexcodes|string|icarus 11 # 12 # sends the data sepcified to the USB device and waits 13 # for a reply then displays it 14 # 15 # the data can be: 16 # 0xhexcodes: e.g. 0x68656c6c6f20776f726c640a 17 # would send "hello world\n" 18 # 19 # string: e.g. sendsometext 20 # 21 # icarus: sends 2 known block payloads for an icarus device 22 # and shows the expected and actual answers if it's 23 # a working V3 icarus 24 25 import sys 26 import serial 27 import binascii 28 29 if len(sys.argv) < 2: 30 sys.stderr.write("usAge: " + sys.argv[0] + " device strings...\n") 31 sys.stderr.write(" where device is either like /dev/ttyUSB0 or COM1\n") 32 sys.stderr.write(" and strings are either '0xXXXX' or 'text'\n") 33 sys.stderr.write(" if the first string is 'icarus' the rest are ignored\n") 34 sys.stderr.write(" and 2 valid icarus test payloads are sent with results displayed\n") 35 sys.stderr.write("\nAfter any command is sent it waits up to 30 seconds for a reply\n"); 36 sys.exit("Aborting") 37 38 # Open with a 10 second timeout - just to be sure 39 ser = serial.Serial(sys.argv[1], 115200, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE, 10, False, False, 5, False, None) 40 41 if sys.argv[2] == "icarus": 42 43 # This show how Icarus use the block and midstate data 44 # This will produce nonce 063c5e01 45 block = "0000000120c8222d0497a7ab44a1a2c7bf39de941c9970b1dc7cdc400000079700000000e88aabe1f353238c668d8a4df9318e614c10c474f8cdf8bc5f6397b946c33d7c4e7242c31a098ea500000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000" 46 midstate = "33c5bf5751ec7f7e056443b5aee3800331432c83f404d9de38b94ecbf907b92d" 47 48 rdata2 = block.decode('hex')[95:63:-1] 49 rmid = midstate.decode('hex')[::-1] 50 payload = rmid + rdata2 51 52 print("Push payload to icarus: " + binascii.hexlify(payload)) 53 ser.write(payload) 54 55 b=ser.read(4) 56 print("Result:(should be: 063c5e01): " + binascii.hexlify(b)) 57 58 # Just another test 59 payload2 = "ce92099c5a80bb81c52990d5c0924c625fd25a535640607d5a4bdf8174e2c8d500000000000000000000000080000000000000000b290c1a42313b4f21b5bcb8" 60 print("Push payload to icarus: " + payload2) 61 ser.write(payload2.decode('hex')) 62 63 b=ser.read(4) 64 print("Result:(should be: 8e0b31c5): " + binascii.hexlify(b)) 65 else: 66 data = "" 67 for arg in sys.argv[2::]: 68 if arg[0:2:] == '0x': 69 data += arg[2::].decode('hex') 70 else: 71 data += arg 72 73 print("Sending: 0x" + binascii.hexlify(data)) 74 ser.write(data) 75 76 # If you're expecting more than one linefeed terminated reply, 77 # you'll only see the first one 78 # AND with no linefeed, this will wait the 10 seconds before returning 79 print("Waiting up to 10 seconds ...") 80 b=ser.readline() 81 print("Result: hex 0x" + binascii.hexlify(b)) 82 83 # This could mess up the display - do it last 84 print("Result: asc '" + b + "'") 85 86 ser.close()