Hi, I have tried a number of examples to try and get the code below to step through correctly.
The header needs to be stepped through once and the sample code at the end to run through until there are no 4 bytes left.
At the moment the while does the whole lot over and over.
The header is 20 bytes long and consists of values made up of different byte sizes.
byte, int16 and int32.
Hope you can help.
Thanks,
The header needs to be stepped through once and the sample code at the end to run through until there are no 4 bytes left.
At the moment the while does the whole lot over and over.
The header is 20 bytes long and consists of values made up of different byte sizes.
byte, int16 and int32.
Hope you can help.
Thanks,
import time
import struct
# Header = 20 words
# 32bit integer - Size
# 1 byte - Variation
# 1 byte - Object
# Object Variation
# 30 1 32-bit integer samples
# 30 2 16-bit integer samples
# 30 3 32-bit integer samples
# 30 4 16-bit integer samples
# 30 5 32-bit floating point samples
# 16bit unsigned integer - Point index, analog point index being sampled
# 32bit integer - Start Time EPOCH
# 32bit integer - Sample Rate, the interval (in seconds) between samples of the analog point
# 32bit integer - Number of Samples, the number of sample records in this file
# Size = 20 + (Number of Samples) * 4 - 16bit integer
# Size = 20 + (Number of Samples) * 8 - 32bit integer
f = open("C:\Python\XXXXXXXXXXX", "rb")
byte = f.read(4)
while byte:
#Step through once only
#Header Section
#Size 32 bit int, should be 484
byte = f.read(4)
Size = struct.unpack('>i', byte)[0] # As integere
print('The size is ')
print(Size)
#Variation 1 byte, should be 1 to 5
byte = f.read(1)
Variation = int.from_bytes(byte, byteorder='big', signed=False)
print('The variation is ')
print(Variation)
#Object 1 byte, should be 30
byte = f.read(1)
Object = int.from_bytes(byte, byteorder='big', signed=False)
print('The object is ')
print(Object)
#Point Index 16 bit int
byte = f.read(2)
PointIndex = int.from_bytes(byte, byteorder='big', signed=False)
print('The point index is ')
print(PointIndex)
#StartTime 32 bit int
byte = f.read(4)
temp = struct.unpack('>i', byte)[0] # As integer
bin(struct.unpack('>i', byte)[0]) # As binary
StartTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(struct.unpack('>i', byte)[0])) # as time
print('The start time is ')
print(StartTime)
#Sample Rate 32 bit int
byte = f.read(4)
SampleRate = int.from_bytes(byte, byteorder='big', signed=False)
print('The sample rate is ')
print(SampleRate)
#Number of Samples
byte = f.read(4)
NoOfSamples = int.from_bytes(byte, byteorder='big', signed=False)
print('The number of samples is ')
print(NoOfSamples)
#End of header section
#Do until end of file, until only 4 more bytes left
#Sample(s)
byte = f.read(4)
Sample = struct.unpack('>i', byte)[0] # As integer
print(Sample)
f.close()
