Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import serial, sys, time, psutil, os, bz2, gc
  2.  
  3. # disable gc
  4. gc.disable()
  5.  
  6. # give us high priority
  7. p = psutil.Process(os.getpid())
  8. p.nice(psutil.REALTIME_PRIORITY_CLASS)
  9.  
  10. # connect to device
  11. ser = serial.Serial('COM12', 2000000, timeout=0.1)
  12.  
  13. # send "ping" command to make sure device is there
  14. ser.write(b'\xFF')
  15. data = ser.read()
  16. if data == b'\xFF':
  17. print("+++ Connected to device, device is ready to receive commands...")
  18. else:
  19. print("!!! Device is not ready, exiting...")
  20. sys.exit()
  21.  
  22. f = None
  23. filename = sys.argv[1]
  24. if filename[-3:].lower() == "bz2":
  25. f = bz2.BZ2File(filename, "r")
  26. else:
  27. f = open(filename, "rb")
  28.  
  29. # reset device
  30. print("--- Sending reset command to device")
  31. ser.write(b'\x00')
  32. time.sleep(0.1)
  33.  
  34. # set window size
  35. #ser.write(bytes([0xA0, 0x40, 0x00])) # 16384 = ~0.68ms
  36.  
  37. #ser.write(bytes([0xA0, 0x05, 0x00])) # 0x94 = 1ms 0x470 = 5ms
  38.  
  39. # set window off at
  40. #ser.write(bytes([0xA1, 0x08, 0x49])) # 2122
  41.  
  42. # set clock filter timers (DPCM fix)
  43. ser.write(bytes([0xA4, 32])) # Port 1 timer (128 = 5us)
  44. ser.write(bytes([0xB4, 32])) # Port 2 timer (128 = 5us)
  45.  
  46. # autolatcher (automatically triggers a latch every n'th clock of the selected controller)
  47. #ser.write(bytes([0xC0, 1, 1])) # set autolatch on controller port 2
  48. #ser.write(bytes([0xC1, 16])) # 16-bit autolatching
  49.  
  50. # set overread values (set to 1 for all data lines)
  51. ser.write(bytes([0xD0, 0x01, 0x01, 0x01]))
  52. ser.write(bytes([0xD1, 0x01, 0x01, 0x01]))
  53.  
  54. # --- dkc2 trains
  55. ser.write(bytes([0xEC, 0x1D, 0x00, 0x04, 0x00, 0x13, 0x02, 0x79, 0x00, 0x7A, 0x00, 0x4D, 0x00, 0x4D, 0x00, 0x21, 0x00, 0x33, 0x02, 0x5D, 0x00, 0xBA, 0x00, 0xD1, 0x00, 0x45, 0x01, 0x07, 0x00, 0xCC, 0x00, 0xE7, 0x00, 0x4F, 0x01, 0x38, 0x00, 0xBF, 0x00, 0xC8, 0x00, 0x85, 0x01, 0x45, 0x00, 0xE5, 0x00, 0xE7, 0x00, 0x56, 0x00, 0xF9, 0x00, 0xCB, 0x00, 0xE3, 0x00, 0x53, 0x3F, 0x8E]))
  56. data = ser.read()
  57. if data == b'\x02':
  58. print("!!! Latch train setup failed, exiting...")
  59. sys.exit()
  60. # --- end of dkc2 trains
  61.  
  62. # start run
  63. print("--- Sending start command to device")
  64. ser.write(b'\x01\x02\x02\x02\x00\x00') # command 1 (play), 16-bits, 2 port, 2 datalines, no window 1, no window 2
  65.  
  66. latches = 0
  67. extra = 0
  68. skip = 0
  69.  
  70. for n in range(0, skip):
  71. f.read(16)
  72.  
  73. cmd = None
  74. data = None
  75. inputs = None
  76.  
  77. trainCount = 1
  78.  
  79. print("--- Starting read loop")
  80. while True:
  81. cmd = ser.read(1)
  82. if cmd == b'\x0F':
  83. if extra > 0:
  84. inputs = f.read(112 - (extra * 16))
  85. data = []
  86. for i in range(0, len(inputs), 16):
  87. data = data + [inputs[i], inputs[i+1], inputs[i+2], inputs[i+3]]
  88. data = data + [inputs[i+8], inputs[i+9], inputs[i+10], inputs[i+11]]
  89.  
  90. data = ([0] * (extra * 8)) + data
  91. ser.write(bytes([0x0F] + data))
  92. extra = 0
  93. else:
  94. inputs = f.read(112)
  95. data = []
  96. for i in range(0, len(inputs), 16):
  97. data = data + [inputs[i], inputs[i+1], inputs[i+2], inputs[i+3]]
  98. data = data + [inputs[i+8], inputs[i+9], inputs[i+10], inputs[i+11]]
  99.  
  100. ser.write(bytes([0x0F] + data))
  101.  
  102. latches = latches + 7
  103. # if latches % 60 == 0:
  104. # print('*** Latches: [%d] - Data: [%x]' % (latches, data[0]))
  105. elif cmd == b'\x77':
  106. print('END OF LATCH TRAIN #%d' % trainCount)
  107. trainCount += 1
  108. elif cmd == b'\x70':
  109. print('--- Short a frame. Adding a frame to compensate.')
  110. elif cmd == b'\x71':
  111. print('--- Extra frame detected. Skipping a frame to compensate.')
  112. elif cmd == b'\x72':
  113. print('!!! Off by many frames. Attempting recovery. Good luck!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement