usbtest.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. import sys
  25. import serial
  26. import binascii
  27. if len(sys.argv) < 2:
  28. sys.stderr.write("usAge: " + sys.argv[0] + " device strings...\n")
  29. sys.stderr.write(" where device is either like /dev/ttyUSB0 or COM1\n")
  30. sys.stderr.write(" and strings are either '0xXXXX' or 'text'\n")
  31. sys.stderr.write(" if the first string is 'icarus' the rest are ignored\n")
  32. sys.stderr.write(" and 2 valid icarus test payloads are sent with results displayed\n")
  33. sys.stderr.write("\nAfter any command is sent it waits up to 30 seconds for a reply\n");
  34. sys.exit("Aborting")
  35. # Open with a 10 second timeout - just to be sure
  36. ser = serial.Serial(sys.argv[1], 115200, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE, 10, False, False, 5, False, None)
  37. if sys.argv[2] == "icarus":
  38. # This show how Icarus use the block and midstate data
  39. # This will produce nonce 063c5e01
  40. block = "0000000120c8222d0497a7ab44a1a2c7bf39de941c9970b1dc7cdc400000079700000000e88aabe1f353238c668d8a4df9318e614c10c474f8cdf8bc5f6397b946c33d7c4e7242c31a098ea500000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000"
  41. midstate = "33c5bf5751ec7f7e056443b5aee3800331432c83f404d9de38b94ecbf907b92d"
  42. rdata2 = block.decode('hex')[95:63:-1]
  43. rmid = midstate.decode('hex')[::-1]
  44. payload = rmid + rdata2
  45. print("Push payload to icarus: " + binascii.hexlify(payload))
  46. ser.write(payload)
  47. b=ser.read(4)
  48. print("Result:(should be: 063c5e01): " + binascii.hexlify(b))
  49. # Just another test
  50. payload2 = "ce92099c5a80bb81c52990d5c0924c625fd25a535640607d5a4bdf8174e2c8d500000000000000000000000080000000000000000b290c1a42313b4f21b5bcb8"
  51. print("Push payload to icarus: " + payload2)
  52. ser.write(payload2.decode('hex'))
  53. b=ser.read(4)
  54. print("Result:(should be: 8e0b31c5): " + binascii.hexlify(b))
  55. else:
  56. data = ""
  57. for arg in sys.argv[2::]:
  58. if arg[0:2:] == '0x':
  59. data += arg[2::].decode('hex')
  60. else:
  61. data += arg
  62. print("Sending: 0x" + binascii.hexlify(data))
  63. ser.write(data)
  64. # If you're expecting more than one linefeed terminated reply,
  65. # you'll only see the first one
  66. # AND with no linefeed, this will wait the 10 seconds before returning
  67. print("Waiting up to 10 seconds ...")
  68. b=ser.readline()
  69. print("Result: hex 0x" + binascii.hexlify(b))
  70. # This could mess up the display - do it last
  71. print("Result: asc '" + b + "'")
  72. ser.close()