-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathowonfileread.c
375 lines (327 loc) · 12.7 KB
/
owonfileread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* owonfileread.c
* Read a binary (vectorgram) dump of the trace memory of an Owon scope from
* a local file. The vectorgram is parsed into a tabulated text format that
* can be plotted using GNUplot or a similar package.
*
* Copyright Aug 2009, Michael Murphy <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <asm/errno.h>
#include <arpa/inet.h> // for htonl() macro
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "owondump.h"
int debug = 0; // set to 1 for channel data hex dumps
char *filename = "output.bin"; // default output filename
int text = 1; // tabulated text output as well as raw data output
int channelcount = 0; // the number of channels in the data dump
struct channelHeader headers[10]; // provide for up to ten scope channels
int decodeVertSensCode(long int i) {
// This is the one byte vertical sensitivity code
char c = (char) i;
int vertSensitivity=-1;
switch (c) {
case 0x01 : vertSensitivity = 5; // 5mV
break;
case 0x02 : vertSensitivity = 10;
break;
case 0x03 : vertSensitivity = 20;
break;
case 0x04 : vertSensitivity = 50;
break;
case 0x05 : vertSensitivity = 100;
break;
case 0x06 : vertSensitivity = 200;
break;
case 0x07 : vertSensitivity = 500;
break;
case 0x08 : vertSensitivity = 1000;
break;
case 0x09 : vertSensitivity = 2000;
break;
case 0x0A : vertSensitivity = 5000; // 5V
break;
}
return vertSensitivity;
}
long long int decodeTimebase(long int i) {
long long int timebase=-1;
// c is the one byte timebase code
char c = (char) i;
// return an LL int
switch (c) {
case 0x00 : timebase = 5LL; // in nanoseconds
break;
case 0x01 : timebase = 10LL;
break;
case 0x02 : timebase = 25LL;
break;
case 0x03 : timebase = 50LL;
break;
case 0x04 : timebase = 100LL;
break;
case 0x05 : timebase = 250LL;
break;
case 0x06 : timebase = 500LL;
break;
case 0x07 : timebase = 1000LL; // 1 microsecond
break;
case 0x08 : timebase = 2500LL;
break;
case 0x09 : timebase = 5000LL;
break;
case 0x0a : timebase = 10000LL;
break;
case 0x0b : timebase = 25000LL;
break;
case 0x0c : timebase = 50000LL;
break;
case 0x0d : timebase = 100000LL;
break;
case 0x0e : timebase = 250000LL;
break;
case 0x0f : timebase = 500000LL;
break;
case 0x10 : timebase = 1000000LL; // 1 millisecond
break;
case 0x11 : timebase = 2500000LL;
break;
case 0x12 : timebase = 5000000LL;
break;
case 0x13 : timebase = 10000000LL; // 10 ms
break;
case 0x14 : timebase = 25000000LL;
break;
case 0x15 : timebase = 50000000LL;
break;
case 0x16 : timebase = 100000000LL; // 100 ms
break;
case 0x17 : timebase = 250000000LL;
break;
case 0x18 : timebase = 500000000LL;
break;
case 0x19 : timebase = 1000000000LL; // 1 second
break;
case 0x1a : timebase = 2500000000LL;
break;
case 0x1b : timebase = 5000000000LL;
break;
case 0x1c : timebase = 10000000000LL;
break;
case 0x1d : timebase = 25000000000LL;
break;
case 0x1e : timebase = 50000000000LL;
break;
case 0x1f : timebase = 100000000000LL; // 100 seconds
break;
default : printf("..Unknown timebase code of %02x\n", c);
}
// printf("decodetimebase passed %02x and returned %Ld\n", c, timebase);
return timebase;
}
// decode the contents of the vectorgram data header - providing us with the timebase and voltage values for the channel data
// hdrBuf has already been stripped of the 10 byte vectorgram file header that begins "SPB......"
// returns the size of the channel data block in bytes
struct channelHeader decodeVectorgramBufferHeader(char *hdrBuf) {
struct channelHeader header;
// if not a bitmap then must be a vectorgram
memcpy(&header.channelname,hdrBuf,3);
header.channelname[3]='\0';
memcpy(&header.blocklength, hdrBuf+3,4);
memcpy(&header.samplecount1, hdrBuf+7,4);
memcpy(&header.samplecount2, hdrBuf+11,4);
memcpy(&header.startoffset, hdrBuf+15,4);
memcpy(&header.timebasecode, hdrBuf+19,4);
memcpy(&header.v_position, hdrBuf+23,4);
memcpy(&header.vertsenscode, hdrBuf+27,4);
memcpy(&header.probexcode, hdrBuf+31,4);
memcpy(&header.t_sample, hdrBuf+35,4);
memcpy(&header.frequency, hdrBuf+39,4);
memcpy(&header.period, hdrBuf+43,4);
memcpy(&header.unknown9, hdrBuf+43,4);
header.vertSensitivity = decodeVertSensCode(header.vertsenscode); // 5mV through 5000mV (5V)
header.timeBase = decodeTimebase(header.timebasecode); // in nanoseconds (10E-9)
printf("-------------------------------\n");
printf(" Channel: %s\n", header.channelname);
printf(" sample count: %d\n", (int) header.samplecount1);
printf(" vertical sensitivity: %dmV\n", header.vertSensitivity);
printf(" timebase: %gms\n", (double) header.timeBase / 1000000);
printf(" t_sample: %gus\n", (double) header.t_sample);
printf("-------------------------------\n");
if(debug) {
printf(" data block length: %08x (%d) bytes\n", (int) header.blocklength, (int) header.blocklength);
printf(" samplecount1: %08x (%d)\n", (int) header.samplecount1, (int) header.samplecount1);
printf(" samplecount2: %08x (%d)\n", (int) header.samplecount2, (int) header.samplecount2);
printf(" sampleoffset: %08x (%d)\n", (int) header.startoffset, (int) header.startoffset);
printf(" timebase code: %08x (%d)\n", (int) header.timebasecode, (int) header.timebasecode);
printf(" v_position: %08x (%d)\n", (int) header.v_position, (int) header.v_position);
printf(" vert sens code: %08x (%d)\n", (int) header.vertsenscode, (int) header.vertsenscode);
printf(" probe mult code: %08x (%d)\n", (int) header.probexcode, (int) header.probexcode);
printf(" frequency: %08x (%d Hz)\n", (int) header.frequency, (int) header.frequency);
printf(" period: %08x (%d us)\n", (int) header.frequency, (int) header.frequency);
printf(" unknown9: %08x (%d)\n", (int) header.unknown9, (int) header.unknown9);
printf("\n");
printf("-------------------------------\n");
printf("\n");
}
return header;
}
void writeTextData(char *buf, int count) {
FILE *fpout;
char *ptr[channelcount];
int i,j;
char txtfilename[strlen(filename)+3];
strcpy(txtfilename,filename);
strcat(txtfilename,".txt");
if ((fpout = fopen(txtfilename,"w")) == NULL) {
printf("..Failed to open file \'%s\'!\n", txtfilename);
return;
}
printf("..Successfully opened text file \'%s\'!\n", txtfilename);
fprintf(fpout,"# Units:(mV) -- Timebase: (%gms)\n", (double) headers[0].timeBase / 1000000);
ptr[0] = buf; // initialise first pointer to start of char *buf
ptr[0] += VECTORGRAM_FILE_HEADER_LENGTH; // +10 bytes to jump over the file header
ptr[0] += VECTORGRAM_BLOCK_HEADER_LENGTH; // +51 bytes to jump over the channel header
// fprintf(stderr, "ptr[%d] = 0x%p (offset %d) \n", 0, ptr[0], (int) (ptr[0]-ptr[0]));
// print the channel names as column headers
for(i=1; i < channelcount; i++) {
ptr[i] = ptr[i-1] + (int) headers[i-1].blocklength + 3;
// fprintf(stderr, "ptr[%d] = 0x%p (offset %d) \n", i, ptr[i], (int) (ptr[i]-ptr[i-1]));
}
fprintf(fpout, "#");
for(i=0; i < channelcount; i++)
fprintf(fpout, "\t\t %s", headers[i].channelname);
fprintf(fpout,"\n");
// for the sake of pointer sanity, we must check the sample count of every channel..
// before trying to print a sample for channel 'n' for every timeslot up to the sample count of channel 1
//
// Even so, this is less than ideal where the timebases for each channel are different... it means
// the plots produced from the data tables are basically meaningless - but that's the way Owon have done it..
//
// see the README in the tarball for perhaps how the text data file should be written.
for(j=0;j < (int) headers[0].samplecount1;j++) {
fprintf(fpout, "%d", j+1);
for(i = 0 ;i < channelcount;i++) {
if(j > (int) headers[i].samplecount1) // no sample available for this timeslot on channel i
fprintf(fpout,"\t\t -");
else
fprintf(fpout, "\t\t%5.1f", (short int) *ptr[i] * headers[i].vertSensitivity * 0.04);
ptr[i]+=2;
}
fprintf(fpout, "\n");
}
printf("..Successfully written trace data to \'%s\'!\n", txtfilename);
if(!fclose(fpout))
printf("..Successfully closed text file \'%s\'!\n", txtfilename);
}
void readOwonBinFile(FILE *fp) {
int i, j, ret;
int owonFileSize=0;
char *owonDataBuffer; // malloc-ed at runtime
char *headerptr; // used to reference the start of the header
int fd;
struct stat buf;
fd = fileno(fp);
fstat(fd, &buf);
owonFileSize = buf.st_size;
printf("..Attempting to malloc read buffer space of %08xh (%d) bytes\n", owonFileSize, owonFileSize);
owonDataBuffer = malloc(owonFileSize);
if(!owonDataBuffer) {
printf("..Failed to malloc(%08xh)!\n", owonFileSize);
goto bail;
}
else
printf("..Successful malloc!\n");
printf("..Attempting to read %08xh (%d) bytes from file...\n", owonFileSize, owonFileSize);
ret = fread(owonDataBuffer, sizeof(char), owonFileSize, fp);
if(ret < 0) {
printf("..Failed to read: %xh (%d) bytes: %d - '%s'\n", owonFileSize, owonFileSize, ret, strerror(-ret));
goto bail;
}
else
printf("..Successful read of %08xh (%d) bytes! : \n", ret, ret);
if (debug) {
// hexdump the first 0x40 bytes of the Owon file Buffer
printf("..Hexdump of first 0x40 bytes of the Owon binary file:\n");
for(i=0; i<=0x03; i++) {
printf("\t%08x: ",i);
for(j=0;j<0x10;j++)
printf("%02x ", (unsigned char) owonDataBuffer[(i*0x10)+j]);
printf("\n");
}
printf("\n");
}
//determine from the header whether this is bitmap data or vectorgram
// is it a 'BM' (bitmap) ?
if(*owonDataBuffer=='B' && *(owonDataBuffer+1)=='M')
printf("640x480 bitmap of %04xh (%d) bytes\n", (int) *(owonDataBuffer+2), *(owonDataBuffer+2));
// is it a vectorgram ('SPBV') ? If so, we decode the contents..
else if(*owonDataBuffer=='S' && *(owonDataBuffer+1)=='P' && *(owonDataBuffer+2)=='B') {
switch (*(owonDataBuffer+3)) {
case 'V' : printf("..Found data from Owon PDS5022S\n");
break;
case 'W' : printf("..Found data from Owon PDS6062S\n");
break;
default : printf("..Found data from Owon unknown model\n");
}
printf("..Found vectorgram data\n");
// initialise the header pointer to the first header in the data
headerptr = owonDataBuffer + VECTORGRAM_FILE_HEADER_LENGTH; // jump over the 10 byte "SPB...." file header
// printf("owonDataBuffer = %Ld\n", (long long int)owonDataBuffer);
// printf("headerptr = (oDB + FILE_HDR_LEN (%d) = %Ld\n", VECTORGRAM_FILE_HEADER_LENGTH, (long long int)headerptr);
// printf("owonFileSize = %Ld\n", (long long int)owonFileSize);
while((owonDataBuffer+owonFileSize) > headerptr) {
if (debug) {
// hexdump the first 0x50 bytes of channel header
printf("..Hexdump of channel header :\n");
for(i=0; i<=0x05; i++) {
printf("\t%08x: ",i);
for(j=0;j<0x10;j++)
printf("%02x ", (unsigned char) *(headerptr+(i*0x10)+j));
printf("\n");
}
} // end if (debug)
headers[channelcount] = decodeVectorgramBufferHeader(headerptr);
headerptr += (int) headers[channelcount].blocklength;
headerptr += 3; // and jump over the channel name itself
channelcount++;
}
// for(i=0;i<channelcount;i++)
// printf("%s has %d samples\n", headers[i].channelname, (int) headers[i].samplecount1); // (16 bits used per sample)
}
// is it neither a BM (bitmap) nor a SPB (vectorgram) ?
else {
printf("..Failed to determine data type.\n");
printf("%c %c %c %c\n", *owonDataBuffer, *(owonDataBuffer+1), *(owonDataBuffer+2), *(owonDataBuffer+3));
}
// dump the buffer to disk as tabulated text data.
writeTextData(owonDataBuffer, owonFileSize);
free(owonDataBuffer); // a buffer of vectorgrams is just a few KB in size
// but for bitmaps this buffer could be very large (~1MB)
bail:
return;
}
int main(int argc, char *argv[]) {
FILE *fp;
// printf("..Size of short int=%d, int=%d, long int = %d, long long int = %d \n", (int) sizeof(short int), (int) sizeof(int), (int) sizeof(long int), (int) sizeof(long long int));
if (argc>1) {
filename = malloc(strlen(argv[1]));
memcpy(filename, argv[1], strlen(argv[1]));
}
else {
printf("..Usage: owonfileread owonbinary filename\n");
return 0;
}
if ((fp = fopen(filename, "r")) == NULL) {
printf("..Couldn\'t open %s\n", filename);
return 0;
}
readOwonBinFile(fp);
fclose(fp);
return 0;
}