Now working thanks to @JonathanDotCel

This commit is contained in:
ABelliqueux 2021-04-26 12:32:21 +02:00
parent 367a5f7186
commit c7570b6804

169
loadlvl.py Normal file → Executable file
View File

@ -1,6 +1,23 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# This working version corrected with the help of sickle :
# https://discord.com/channels/642647820683444236/663664210525290507/836029253593858060
# Corrected script by Sickle : http://psx.arthus.net/code/rawdog.py
# Sickle - 26/04/2021 :
# " Ooh, you were like frustratingly close dude! Few tiny issues:
# - first of your 3 rolling buffers was bugged (other 2 were spot on)
# - waiting too long between commands at points, unirom timed out
# - var i was missing the i += chunkSize so we were stuck in a loop there (e.g. tried to send a second chonk)
# - exit was gummed up with the main logic being in a while True: "
# As suggested:
# - Removed while True: loop
# - moved rolling buffer loops to WaitForResponse()
# - reduced sleeps
# - inc var i with chunkSize
import sys import sys
import serial import serial
import time import time
@ -18,7 +35,7 @@ address = '800b1470' # We're receiving this info as a string from the psx
# test data # test data
data = int(111111111111111111111111111111111).to_bytes(14, byteorder='big', signed=False) data = int(111111111111111111111111111111111).to_bytes(14, byteorder='little', signed=False)
size = len( data ) size = len( data )
@ -30,6 +47,43 @@ Saddr = 0
Slen = 0 Slen = 0
def WaitForResponse( expectedAnswer ):
responseBuffer = ""
while True:
if ser.in_waiting:
print( "Input buffer : " + str(ser.in_waiting))
chkValue = ser.read(1)
# Make sure byte value is < 128 so that it can be decoded to ascii :: always getting '\xc7' 'q' '\x1c' '\xc7' '\xab' '\xed' '1' '\x05'
if chkValue[0] < 128:
responseBuffer += chkValue.decode('ascii')
else:
responseBuffer += '.'
if len( responseBuffer ) > 4:
# remove first char in buffer
responseBuffer = responseBuffer[1:]
print( "Response buffer : " + responseBuffer )
if responseBuffer == expectedAnswer:
break
print( "Got response : " + responseBuffer + " - " + expectedAnswer )
def main(args): def main(args):
global checkSum, responseBuffer, Ssbin, Saddr, Slen, chunkSize global checkSum, responseBuffer, Ssbin, Saddr, Slen, chunkSize
@ -51,7 +105,7 @@ def main(args):
print("checkSum : " + str(checkSum) ) print("checkSum : " + str(checkSum) )
while True: # ~ while True:
# ~ global responseBuffer, Ssbin, Saddr, Slen, chunkSize # ~ global responseBuffer, Ssbin, Saddr, Slen, chunkSize
@ -73,32 +127,37 @@ def main(args):
ser.write( bytes( 'SBIN' , 'ascii' ) ) ser.write( bytes( 'SBIN' , 'ascii' ) )
time.sleep(.5) time.sleep(.1)
ser.write( bytes( 'UPV2' , 'ascii' ) ) ser.write( bytes( 'UPV2' , 'ascii' ) )
Ssbin = 1 Ssbin = 1
time.sleep(.5) time.sleep(.1)
while True: # ~ while True:
# ~ while ser.in_waiting: #while ser.in_waiting:
print(".") # ~ print(".")
responseBuffer += ser.read(12).decode('ascii' ) # ~ responseBuffer += ser.read(12).decode('ascii' )
break # ~ break
print( "Buffer : " + responseBuffer ) # ~ print( "Buffer : " + responseBuffer )
if responseBuffer[-4:] == "OKAY": # ~ if responseBuffer[-4:] == "OKAY":
responseBuffer = "" responseBuffer = ""
print("Waiting for OKAY...")
WaitForResponse("OKAY")
# convert addr str > int > bytes # convert addr str > int > bytes
bytesAddr = int( address, 16 ).to_bytes( 4, byteorder='big', signed=False ) bytesAddr = int( address, 16 ).to_bytes( 4, byteorder='little', signed=False )
# same as ? # same as ?
@ -118,23 +177,23 @@ def main(args):
ser.write( bytesAddr ) ser.write( bytesAddr )
time.sleep(.5) time.sleep(.1)
# Convert and write size bytes to serial # Convert and write size bytes to serial
bytesSize = size.to_bytes( 4, byteorder='big', signed = False ) bytesSize = size.to_bytes( 4, byteorder='little', signed = False )
ser.write( bytesSize ) ser.write( bytesSize )
time.sleep(.5) time.sleep(.1)
# Convert and write chekSum bytes to serial # Convert and write chekSum bytes to serial
bytesChk = checkSum.to_bytes( 4, byteorder='big', signed = False ) bytesChk = checkSum.to_bytes( 4, byteorder='little', signed = False )
ser.write( bytesChk ) ser.write( bytesChk )
time.sleep(.5) time.sleep(.1)
# Convert and write data bytes to serial # Convert and write data bytes to serial
@ -156,8 +215,8 @@ def main(args):
# we know data length is < 2048, we'd need some code to cut the data in 2K chunks in real use case # we know data length is < 2048, we'd need some code to cut the data in 2K chunks in real use case
print( "Input buffer b : " + str(ser.out_waiting)) # ~ print( "Input buffer b : " + str(ser.out_waiting))
print( "Output buffer b : " + str(ser.out_waiting)) # ~ print( "Output buffer b : " + str(ser.out_waiting))
# ~ for byte in range( len( data ) ): # ~ for byte in range( len( data ) ):
@ -165,7 +224,7 @@ def main(args):
# ~ time.sleep(.005) # ~ time.sleep(.005)
time.sleep(.5) time.sleep(.1)
# Put chunk checksum calculation here # Put chunk checksum calculation here
@ -177,14 +236,14 @@ def main(args):
wait += 1 wait += 1
time.sleep(.5) time.sleep(.1)
print("Wait : "+ str(wait)) print("Wait : "+ str(wait))
# reset input buffer # reset input buffer
print( "Input buffer : " + str(ser.in_waiting)) # ~ print( "Input buffer : " + str(ser.in_waiting))
print( "Output buffer : " + str(ser.out_waiting)) # ~ print( "Output buffer : " + str(ser.out_waiting))
# ~ ser.reset_input_buffer() # ~ ser.reset_input_buffer()
@ -192,70 +251,78 @@ def main(args):
print( "Chunk" + str( i + 1 ) + " waiting for unirom to request checksum (CHEK)..." ) print( "Chunk" + str( i + 1 ) + " waiting for unirom to request checksum (CHEK)..." )
# Wait for "CHEK" WaitForResponse( "CHEK" )
while True: # Wait for "CHEK" - MOVED to WaitForResponse()
if ser.in_waiting: # ~ while True:
print( "Input buffer : " + str(ser.in_waiting)) # ~ if ser.in_waiting:
# ~ print(".") # ~ print( "Input buffer : " + str(ser.in_waiting))
chkValue = ser.read() #print(".")
# Make sure byte value is < 128 so that it can be decoded to ascii :: always getting '\xc7' 'q' '\x1c' '\xc7' '\xab' '\xed' '1' '\x05' # ~ chkValue = ser.read()
print( "chkVal : " + str(chkValue) + " - " + str( int.from_bytes(chkValue, 'big') ) ) # ~ # Make sure byte value is < 128 so that it can be decoded to ascii :: always getting '\xc7' 'q' '\x1c' '\xc7' '\xab' '\xed' '1' '\x05'
if int.from_bytes(chkValue, 'big') < 128: # ~ print( "chkVal : " + str(chkValue) + " - " + str( int.from_bytes(chkValue, 'big') ) )
responseBuffer += chkValue.decode('ascii') # ~ if int.from_bytes(chkValue, 'big') < 128:
if len( responseBuffer ) > 4: # ~ responseBuffer += chkValue.decode('ascii')
# remove first char in buffer # ~ if len( responseBuffer ) > 4:
responseBuffer = responseBuffer[1:] # ~ # remove first char in buffer
# ~ responseBuffer = responseBuffer[1:]
# ~ print( "Response buffer : " + responseBuffer ) # ~ print( "Response buffer : " + responseBuffer )
if responseBuffer == "CHEK": # ~ if responseBuffer == "CHEK":
print( "Got response : " + responseBuffer ) # ~ print( "Got response : " + responseBuffer )
break # ~ break
print( "Sending checksum to unirom..." ); print( "Sending checksum to unirom..." );
ser.write( bytesChk ) ser.write( bytesChk )
time.sleep( 1 ) time.sleep( .1 )
# Wait for "MORE" # Wait for "MORE" - replace with WaitForResponse()
while True: # ~ while True:
if ser.in_waiting: # ~ if ser.in_waiting:
print(".") # ~ print(".")
responseBuffer += ser.read().decode('ascii') # ~ responseBuffer += ser.read().decode('ascii')
if len( responseBuffer ) > 4: # ~ if len( responseBuffer ) > 4:
# remove first char in buffer # ~ # remove first char in buffer
responseBuffer = responseBuffer[1:] # ~ responseBuffer = responseBuffer[1:]
if responseBuffer == "MORE": # ~ if responseBuffer == "MORE":
print( "Got response : " + responseBuffer ) # ~ print( "Got response : " + responseBuffer )
break # ~ break
WaitForResponse("MORE")
print( str(i+1) + "chunk sent with correct checksum.") print( str(i+1) + "chunk sent with correct checksum.")
i += chunkSize
print("DONE!")
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':