0

thanks in advance for any help on this.

I am writing some code to send data from a Mac to an Arduino board so that I can program a flash memory device. I have a Python program which negotiates a link to the arduino board and then should send 256 byte chunks of data read from a file to the arduino. Code running on the Arduino programs the memory device in 256 byte pages using an SPI link. Here's the Python code:

import serial, time, sys

try:
    dataFile = open(sys.argv[1], "rb")
except IOError:
    sys.exit("file cannot be opened")  
arduino = serial.Serial('/dev/cu.usbmodem2101', 19200, timeout=1)
time.sleep(1) # give the connection a second to settle
arduino.write(("WAKEUP").encode('ascii'))
if( arduino.readline() != ("ACK").encode('ascii') ):
    sys.exit("no initial ACK from programmer")  
print("received initial ACK")
for block in range(1, 131073): # 256Mb is 131072 x 256B blocks
    blockData = dataFile.read(256)
    checksum = 256 - (sum(blockData) % 256) # checksum when added to summed data should result in 0
    arduino.write(blockData) # send the data
    arduino.write(checksum) # send the checksum
    if( arduino.readline() != ("ACK").encode('ascii') ): # wait for the block to be processed
        sys.exit("Failed to complete data transfer")
    print("Block = " + str(block) + " sent succesfully" )
arduino.close()
dataFile.close()

Instead of sending the data the program just sends 0x00 over and over. If I modify the code to read a single byte of data from the file at a time and send the data one byte at a time it works fine. Please can anyone advise me what I'm doing wrong with the code shown above?

Roman
  • 11
  • 4

1 Answers1

0

I worked on this some more to fix some checksum-related bugs and the following code works:

    import serial, time, sys

try:
    dataFile = open(sys.argv[1], "rb")
except IOError:
    sys.exit("file cannot be opened")  
arduino = serial.Serial('/dev/cu.usbmodem2101', 115200, timeout=1)
time.sleep(1) # give the connection a second to settle
arduino.write(("WAKEUP").encode('ascii'))
if( arduino.readline() != ("ACK").encode('ascii') ):
    sys.exit("no initial ACK from programmer")  
print("received initial ACK")
for block in range(0, 16384): # 256Mb is 16384 x 2048B blocks
    blockData = dataFile.read(2048)
    checksum = (256 - (sum(blockData) % 256)) % 256 # checksum when added to summed data should result in 0
    arduino.write((blockData)) # send data
    if( checksum == 0) :
        arduino.write(bytearray([0]))
    else :
        arduino.write(checksum.to_bytes((checksum.bit_length() + 7) // 8, 'big')) # send the checksum
    if( arduino.readline() != ("ACK").encode('ascii') ): # wait for the block to be processed
        sys.exit("Failed to complete data transfer")
    print("Block = " + str(block) + " sent succesfully" )
arduino.close()
dataFile.close()

Don't really understand why it suddenly decided to work.

Roman
  • 11
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 30 '22 at 08:33