Advertisement
Guest User

usbtest.py

a guest
Apr 28th, 2012
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  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. from serial 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 30 second timeout - just to be sure
  39. ser = Serial(sys.argv[1], 115200, 8, timeout=30)
  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.     block = "00000001430afcd1af8d5628c53c9b67bc191f8af5854b77bf8af4080000021d000000000918ac11f800eb36152384ca9af8b03661f3b7d0a11271a00b27d807d33df6fd4f55ba0b1a0b350c00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000"
  48.     midstate = "5d9f088fa64d2036bb92508f59917fe427977487bc211e100067d6d856f36348"
  49.  
  50.     rdata2  = block.decode('hex')[95:63:-1]
  51.     rmid    = midstate.decode('hex')[::-1]
  52.     payload = rmid + rdata2
  53.  
  54.     print("Push payload to icarus: " + binascii.hexlify(payload))
  55.     ser.write(payload)
  56.  
  57.     b=ser.read(4)
  58.     print("Result:(should be: 063c5e01): " + binascii.hexlify(b))
  59.  
  60.     # Just another test
  61.     payload2 = "ce92099c5a80bb81c52990d5c0924c625fd25a535640607d5a4bdf8174e2c8d500000000000000000000000080000000000000000b290c1a42313b4f21b5bcb8"
  62.     print("Push payload to icarus: " + payload2)
  63.     ser.write(payload2.decode('hex'))
  64.  
  65.     b=ser.read(4)
  66.     print("Result:(should be: 8e0b31c5): " + binascii.hexlify(b))
  67. else:
  68.     data = ""
  69.     for arg in sys.argv[2::]:
  70.         if arg[0:2:] == '0x':
  71.             data += arg[2::].decode('hex')
  72.         else:
  73.             data += arg
  74.  
  75.     print("Sending: 0x" + binascii.hexlify(data))
  76.     ser.write(data)
  77.  
  78.     # If you're expecting more than one linefeed terminated reply,
  79.     # you'll only see the first one
  80.     # AND with no linefeed, this will wait the full 30 seconds before returning
  81.     print("Waiting up to 30 seconds ...")
  82.     b=ser.readline()
  83.     print("Result: hex 0x" + binascii.hexlify(b))
  84.  
  85.     # This could mess up the display - do it last
  86.     print("Result: asc '" + b + "'")
  87.  
  88. ser.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement