UPDATED THE QUESTION
Hello,
I am writing this python code for Raspberry Pi (rpi) where RPi is connected to a microcontroller (mcu). The mcu is sending data to rpi through SPI communication protocol. The mcu acts as the slave device and rpi as the master device.
Every time mcu wants to send any data, it sends out a high signal (or an interrupt) to rpi and waits for the rpi's response. Rpi then sends 0x2 after which rpi starts data collection. On rpi, the first time when data comes, it opens a csv file in 'w' mode and stores the data into csv file. And next time whenever data comes, I want rpi to store data in the same file and start from the position after the last data was written.
Data format: I am sending data in the form of bytes from the mcu. On Rpi, I want to combine every two 8-bit data (two bytes) to form into a 16-bit data and ignore the next two data bytes and store the combined 16-bit data in the file in every new line. For ex., if there is data as "data[0], data[1],data[2], data[3],data[4], data[5],data[6], data[7],data[8], data[9]...", then I want to use "data[0], data[1]" to form the first 16-bit data, then ignore "data[2], data[3]" and so on.
I have written some code, but I am unable to update it according to my requirement. I have 2 options:
a. Either every 4 data bytes, combine it to form in to 32-bit data and right shift the data by 16 bits and then store into file. OR
b. Get only every 2 data bytes, combine them and store it into file and ignore the next 2 data bytes and so on.
Can someone suggest which one would be better and can help me with the same? Thanks.
Hello,
I am writing this python code for Raspberry Pi (rpi) where RPi is connected to a microcontroller (mcu). The mcu is sending data to rpi through SPI communication protocol. The mcu acts as the slave device and rpi as the master device.
Every time mcu wants to send any data, it sends out a high signal (or an interrupt) to rpi and waits for the rpi's response. Rpi then sends 0x2 after which rpi starts data collection. On rpi, the first time when data comes, it opens a csv file in 'w' mode and stores the data into csv file. And next time whenever data comes, I want rpi to store data in the same file and start from the position after the last data was written.
Data format: I am sending data in the form of bytes from the mcu. On Rpi, I want to combine every two 8-bit data (two bytes) to form into a 16-bit data and ignore the next two data bytes and store the combined 16-bit data in the file in every new line. For ex., if there is data as "data[0], data[1],data[2], data[3],data[4], data[5],data[6], data[7],data[8], data[9]...", then I want to use "data[0], data[1]" to form the first 16-bit data, then ignore "data[2], data[3]" and so on.
I have written some code, but I am unable to update it according to my requirement. I have 2 options:
a. Either every 4 data bytes, combine it to form in to 32-bit data and right shift the data by 16 bits and then store into file. OR
b. Get only every 2 data bytes, combine them and store it into file and ignore the next 2 data bytes and so on.
Can someone suggest which one would be better and can help me with the same? Thanks.
import datetime import os import struct import time import pigpio import spidev import zlib INTERRUPT_GPIO=25 bus = 0 device = 0 spi = spidev.SpiDev() spi.open(bus, device) spi.max_speed_hz = 4000000 spi.mode = 0 pi = pigpio.pi() if not pi.connected: exit() pi.set_mode(INTERRUPT_GPIO, pigpio.INPUT) 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): data = bytes([0]*1024) spi.xfer2([0x02]) for x in range(1): f.truncate() recv = spi.xfer2(data) values = struct.unpack("<" +"I"*256, bytes(recv)) f.write("\n") f.write("\n".join([str(x) for x in values])) input("Press Enter to start the process ") spi.xfer2([0x01]) f = open(output_file_path(), 'w') cb1=pi.callback(INTERRUPT_GPIO, pigpio.RISING_EDGE, spi_process) while True: time.sleep(1)