usbtest.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python2.7
  2. # Copyright 2012 Xiangfu
  3. # Copyright 2012-2013 Andrew Smith
  4. # Copyright 2013 Luke Dashjr
  5. #
  6. # This program is free software; you can redistribute it and/or modify it under
  7. # the terms of the GNU General Public License as published by the Free Software
  8. # Foundation; either version 3 of the License, or (at your option) any later
  9. # version. See COPYING for more details.
  10. # Linux usage: ./usbtest.py /dev/ttyUSB0 0xhexcodes|string|icarus
  11. # OR python usbtest.py /dev/ttyUSB0 0xhexcodes|string|icarus
  12. #
  13. # Windows usage: ./usbtest.py COM1 0xhexcodes|string|icarus
  14. #
  15. # sends the data sepcified to the USB device and waits
  16. # for a reply then displays it
  17. #
  18. # the data can be:
  19. # 0xhexcodes: e.g. 0x68656c6c6f20776f726c640a
  20. # would send "hello world\n"
  21. #
  22. # string: e.g. sendsometext
  23. #
  24. # icarus: sends 2 known block payloads for an icarus device
  25. # and shows the expected and actual answers if it's
  26. # a working V3 icarus
  27. import sys
  28. import serial
  29. import binascii
  30. if len(sys.argv) < 2:
  31. sys.stderr.write("Usage: " + sys.argv[0] + " device strings...\n")
  32. sys.stderr.write(" where device is either like /dev/ttyUSB0 or COM1\n")
  33. sys.stderr.write(" and strings are either '0xXXXX' or 'text'\n")
  34. sys.stderr.write(" if the first string is 'icarus' the rest are ignored\n")
  35. sys.stderr.write(" and 2 valid icarus test payloads are sent with results displayed\n")
  36. sys.stderr.write("\nAfter any command is sent it waits up to 30 seconds for a reply\n");
  37. sys.exit("Aborting")
  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)
  40. if sys.argv[2] == "icarus":
  41. # This show how Icarus use the block and midstate data
  42. # This will produce nonce 063c5e01
  43. block = "0000000120c8222d0497a7ab44a1a2c7bf39de941c9970b1dc7cdc400000079700000000e88aabe1f353238c668d8a4df9318e614c10c474f8cdf8bc5f6397b946c33d7c4e7242c31a098ea500000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000"
  44. midstate = "33c5bf5751ec7f7e056443b5aee3800331432c83f404d9de38b94ecbf907b92d"
  45. rdata2 = block.decode('hex')[95:63:-1]
  46. rmid = midstate.decode('hex')[::-1]
  47. payload = rmid + rdata2
  48. print("Push payload to icarus: " + binascii.hexlify(payload))
  49. ser.write(payload)
  50. b=ser.read(4)
  51. print("Result:(should be: 063c5e01): " + binascii.hexlify(b))
  52. # Just another test
  53. payload2 = "ce92099c5a80bb81c52990d5c0924c625fd25a535640607d5a4bdf8174e2c8d500000000000000000000000080000000000000000b290c1a42313b4f21b5bcb8"
  54. print("Push payload to icarus: " + payload2)
  55. ser.write(payload2.decode('hex'))
  56. b=ser.read(4)
  57. print("Result:(should be: 8e0b31c5): " + binascii.hexlify(b))
  58. else:
  59. data = ""
  60. for arg in sys.argv[2::]:
  61. if arg[0:2:] == '0x':
  62. data += arg[2::].decode('hex')
  63. else:
  64. data += arg
  65. print("Sending: 0x" + binascii.hexlify(data))
  66. ser.write(data)
  67. # If you're expecting more than one linefeed terminated reply,
  68. # you'll only see the first one
  69. # AND with no linefeed, this will wait the 10 seconds before returning
  70. print("Waiting up to 10 seconds ...")
  71. b=ser.readline()
  72. print("Result: hex 0x" + binascii.hexlify(b))
  73. print("Result: asc %s" % (repr(b),))
  74. ser.close()