Feb-12-2022, 06:52 AM
I am using SPI communication protocol, where a microcontroller sends data to the Raspberry Pi. Raspberry Pi collects this data and stores it in a file. I am using python script to collect this data and store it into a file.
I am sending ~800,000 bytes (8-bit integers) from microcontroller. On Raspberry Pi, after 2048 bytes are collected, 4 bytes are joined together to form a 32-bit integer and stored in a file and then the loop starts again to collect next set of 2048 bytes.
The problem is that all of the data is received within 3 seconds (I ran the script without any file handling operations), but with file handling it takes a total of ~20 seconds. Is there an efficient way where I can reduce the time from 20 seconds to less than 5-10 seconds?
I am not using struct.unpack() function because first of all, it expects an array of length 4, so I would needto create another array and store 4 values and then use that function, making it take more time. Secondly, Link shows that bit shifting operation takes less time than struct.unpack(). Thanks.
I am sending ~800,000 bytes (8-bit integers) from microcontroller. On Raspberry Pi, after 2048 bytes are collected, 4 bytes are joined together to form a 32-bit integer and stored in a file and then the loop starts again to collect next set of 2048 bytes.
The problem is that all of the data is received within 3 seconds (I ran the script without any file handling operations), but with file handling it takes a total of ~20 seconds. Is there an efficient way where I can reduce the time from 20 seconds to less than 5-10 seconds?
I am not using struct.unpack() function because first of all, it expects an array of length 4, so I would needto create another array and store 4 values and then use that function, making it take more time. Secondly, Link shows that bit shifting operation takes less time than struct.unpack(). Thanks.
import time
import spidev
import array as arr
import struct
import datetime as datetime
import os
import pigpio
# We only have SPI bus 0 available to us on the Pi
bus = 0
#Device is the chip select pin. Set to 0 or 1, depending on the connections
device = 0
# Enable SPI
spi = spidev.SpiDev()
# Open a connection to a specific bus and device (chip select pin)
spi.open(bus, device)
# Set SPI speed and mode
spi.max_speed_hz = 4000000
spi.mode = 0
pi=pigpio.pi()
pi.set_mode(25, pigpio.INPUT)
k=0
value=[]
def output_file_path():
return os.path.join(os.path.dirname(__file__),
datetime.datetime.now().strftime("%dT%H.%M.%S") + ".csv")
print("Enter '1' to start the process")
a=input()
if a:
print("SM1 Process started...")
spi.xfer2([0x01])
while True:
if pi.wait_for_edge(25, pigpio.RISING_EDGE, 5.0):
print("Detected")
with open(output_file_path(), 'w') as f:
t1=datetime.datetime.now()
data=[0]*2048
for x in range(392):
spi.xfer2(data)
#value=struct.unpack("<I", bytearray(data))[0]
for y in range(0,2048,4):
value=data[y]<<24 | data[y+1]<<16 | data[y+2]<<8 | data[y+3]
f.write(str(value)+'\n')
f.close()
t2=datetime.datetime.now()
print(t2-t1)
break
else:
print("Wrong input.")
