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
  Changing client.get() method type based on size of data... dl0dth 1 1,275 Jan-02-2025, 08:30 PM
Last Post: dl0dth
Question [SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar' Winfried 1 3,094 Jan-02-2025, 02:09 AM
Last Post: lyly19
  Python: How to import data from txt, instead of running the data from the code? Melcu54 1 1,307 Dec-13-2024, 06:50 AM
Last Post: Gribouillis
  Help with to check an Input list data with a data read from an external source sacharyya 3 2,617 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Export data from PDF as tabular format zinho 5 3,665 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 5,265 Dec-12-2022, 08:22 PM
Last Post: jh67
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 7,242 Dec-06-2022, 11:09 AM
Last Post: mg24
  Write sql data or CSV Data into parquet file mg24 2 5,279 Sep-26-2022, 08:21 AM
Last Post: ibreeden
  Load multiple Jason data in one Data Frame vijays3 6 3,752 Aug-12-2022, 05:17 PM
Last Post: vijays3
  Need Help writing data into Excel format ajitnayak87 8 5,177 Feb-04-2022, 03:00 AM
Last Post: Jeff_t

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.