Python Forum
Issue in changing data format (2 bytes) into a 16 bit data.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue in changing data format (2 bytes) into a 16 bit data.
#11
(Jul-25-2022, 06:00 AM)deanhystad Wrote: What does struct.unpack() return? Have you read the documentation for the struct library? The answer is there.

Got it! Thank you. I changed the "I" to "H" which is for unsigned short (2 bytes) and I was able to receive the values combining every 2 bytes. But I got stuck into this tricky part where I only want to write every other value in the file. For example, write value[0], ignore value[1], write value[2], and so on...To work on this, I made the following change, but now the data is not being written into the file at all...any hints?

def spi_process(gpio,level,tick):
    data = bytes([0]*2048)
    spi.xfer2([0x02])
    for x in range(1):
        recv = spi.xfer2(data)
        values = struct.unpack(">" +"H"*1024, bytes(recv))
        f.write("\n")
        num=1
        while num in range(0,2047):
            if num%2==0:
                f.write("\n")
                f.write(str(x))
Reply
#12
I would try something like this:
def spi_process(*_):
    spi.xfer2([0x02])
    data_bytes = bytes(spi.xfer2([0]*2048))
    num_shorts = len(data_bytes) // 2
    data_short = struct.unpack(f">{num_shorts}H", data_bytes)
    # Write every other short to a file
    for index in range(0, num_shorts, 2):
        f.write(f"{data_short[index]}\n")
Or if you prefer unpacking to long and using shift.
def spi_process(*_):
    spi.xfer2([0x02])
    data_bytes = bytes(spi.xfer2([0]*2048))
    num_long = len(data_bytes) // 4
    for value in struct.unpack(f">{num_long}L", data_bytes):
        f.write(f"{value >> 16}\n")
I have not tested this code at all. I can't even install the spidev package.

Why aren't you using read_bytes() and write_bytes()?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 408 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Export data from PDF as tabular format zinho 5 704 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,879 Dec-12-2022, 08:22 PM
Last Post: jh67
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 3,050 Dec-06-2022, 11:09 AM
Last Post: mg24
  Write sql data or CSV Data into parquet file mg24 2 2,431 Sep-26-2022, 08:21 AM
Last Post: ibreeden
  Load multiple Jason data in one Data Frame vijays3 6 1,546 Aug-12-2022, 05:17 PM
Last Post: vijays3
  Need Help writing data into Excel format ajitnayak87 8 2,513 Feb-04-2022, 03:00 AM
Last Post: Jeff_t
  Adding shifted data set to data set xquad 3 1,502 Dec-22-2021, 10:20 AM
Last Post: Larz60+
  Why changing data in a copied list changes the original list? plumberpy 3 2,232 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Python issue - Data science - Help is needed yovel 2 2,008 Jul-29-2021, 04:27 PM
Last Post: yovel

Forum Jump:

User Panel Messages

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