Hi,
I have this python script where I am getting data from microcontroller (MCU) and calculating the CRC value of the received data. As the data is received, I am storing it into a new file with the name as
Can someone help me with how can I overwrite the data on the same file? Thanks.
I have this python script where I am getting data from microcontroller (MCU) and calculating the CRC value of the received data. As the data is received, I am storing it into a new file with the name as
DATE.T.HOUR.MINUTE.SECOND
. After all data is received (~1 MB data), I am calculating the CRC value and comparing this with what MCU has sent. If the value matches, then good....if it does not matches, I plan to overwrite the data on the same file. Can someone help me with how can I overwrite the data on the same file? Thanks.
import datetime import os import struct import time import pigpio import spidev import zlib bus = 0 device = 0 spi = spidev.SpiDev() spi.open(bus, device) spi.max_speed_hz = 4000000 spi.mode = 0 pi.set_mode(25, pigpio.INPUT) rpi_crc=0 def output_file_path(): return os.path.join(os.path.dirname(__file__), datetime.datetime.now().strftime("%dT%H.%M.%S") + ".csv") def spi_process(gpio,level,tick): #print("Detected") data = bytes([0]*2048) crc_data = bytes([0]*4) spi.xfer2([0x02]) rpi_crc=0 with open(output_file_path(), 'w') as f: for x in range(392): recv = spi.xfer2(data) values = struct.unpack("<" +"I"*512, bytes(recv)) rpi_crc = zlib.crc32(bytes(recv),rpi_crc) f.write("\n") f.write("\n".join([str(x) for x in values])) mcu_crc_bytes = spi.xfer2(crc_data) mcu_crc = struct.unpack("<"+"I"*1,bytes(mcu_crc_bytes)) mcu_crc_int = int(''.join(map(str,mcu_crc))) if (rpi_crc != mcu_crc_int): spi.xfer([0x03]) print("CRC did not match!!") #Some function to open the same file and overwrite! for x in range(392): recv = spi.xfer2(data) values = struct.unpack("<" +"I"*512, bytes(recv)) f.write("\n") f.write("\n".join([str(x) for x in values])) else: print("CRC matched!!") spi.xfer([0x04]) input("Press Enter to start the process ") spi.xfer2([0x01]) cb1=pi.callback(INTERRUPT_GPIO, pigpio.RISING_EDGE, spi_process) while True: time.sleep(1)