usbtest.py 3.2 KB

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