Python Forum
How can I solve this file handling issue?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I solve this file handling issue?
#9
I've re-written your code, removing some unneeded imports and redundant code, and putting in some timing code to show you where the time is actually being spent:

# Untested.
import datetime
import os
import struct

import pigpio
import spidev

# 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)

def output_file_path():
    return os.path.join(os.path.dirname(__file__),
               datetime.datetime.now().strftime("%dT%H.%M.%S") + ".csv")

input("Press Enter to start the process ")
print("SM1 Process started...")
spi.xfer2([0x01])
while True:
    if pi.wait_for_edge(25, pigpio.RISING_EDGE, 5.0):
        print("Detected")
        data = [0]*2048
        
        with open(output_file_path(), 'w') as f:
            for x in range(392):
                t1 = datetime.datetime.now()
                spi.xfer2(data)
                print("time taken reading data:", datetime.datetime.now() - t1)
                t1 = datetime.datetime.now()
                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')
                print("time taken writing data:", datetime.datetime.now() - t1)
        break
My prediction is that writing will not take anywhere near as much time as you think.

If that code works as you expect, then try changing these three lines:

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')
into this:

values = struct.unpack(">" +"I"*512, bytes(data))
f.write('\n'.join([str(n) for n in values]))
and see if it is faster. (Disclaimer: I have not run this code, it may contain typos or other errors.)
GiggsB likes this post
Reply


Messages In This Thread
RE: How can I solve this file handling issue? - by stevendaprano - Feb-12-2022, 08:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  File Handling not working properly TheLummen 8 3,678 Feb-17-2024, 07:47 PM
Last Post: TheLummen
  file handling Newbee question middlecope 2 1,599 Jan-18-2023, 03:09 PM
Last Post: middlecope
Star python exception handling handling .... with traceback mg24 3 3,414 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Delimiter issue with a CSV file jehoshua 1 2,388 Apr-19-2022, 01:28 AM
Last Post: jehoshua
  File handling issue GiggsB 4 2,515 Mar-31-2022, 09:35 PM
Last Post: GiggsB
  How to solve this file handling issue? GiggsB 3 2,484 Jan-10-2022, 09:36 AM
Last Post: Gribouillis
  File handling knollfinder 3 2,726 Jun-28-2020, 07:39 PM
Last Post: knollfinder
  Writing to File Issue Flash_Stang 3 3,339 Jun-05-2020, 05:14 AM
Last Post: Gribouillis
  file handling sivareddy 1 2,147 Feb-23-2020, 07:28 PM
Last Post: jefsummers
  Simple Read File Issue blackjesus24 4 3,675 Feb-09-2020, 12:07 AM
Last Post: blackjesus24

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020