OSDN Git Service

Ver1.1 scilog Ver1.0.4に対応 記録CH数指定
[scilog/decode_sci.git] / decode_sci.c
1 /*
2         decode_sci.cgit s
3         sciLogger high sample data decoder
4
5         入力ファイル Little Endian
6         このプログラムが動作するマシンもLittle Endianである必要あり。
7
8 *** Ver1.1
9 * scilog Ver1.0.4に対応
10 * 記録するCH数を変更できるようになったので対応
11 * ヘッダにはCH数を保存していないので、CH数をコマンドラインで指定する必要がある
12 * 指定しなかった場合はDefault 6CH
13 *
14 * Usage: decode_sci -c CH_NUM inputfile
15 *  CH_NUM ファイルに記録されているCH数を指定する 無指定時のDefaultは6
16
17   Copyright Naoya Takamura@NT systemd design, 2011
18  
19   This program is free software; you can redistribute it and/or modify
20   it under the terms of the GNU General Public License as published by
21   the Free Software Foundation; either version 2 of the License, or
22   (at your option) any later version.
23  
24   This program is distributed in the hope that it will be useful,
25   but WITHOUT ANY WARRANTY; without even the implied warranty of
26   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27   GNU General Public License for more details.
28  
29   You should have received a copy of the GNU General Public License
30   along with this program; if not, write to the Free Software
31   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 */
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <getopt.h>
38
39 #define VER     "1.1"
40 #define AD_CH   6
41 #define AD_SAMPLE       50
42 #define AD_BYTES        3
43 #define EXE_NAME "decode_sci"
44
45 struct _recdata_head_t {
46     uint16_t    year;   // UTC
47     uint8_t             month;
48     uint8_t             day;
49     uint8_t             hour;
50     uint8_t             min;
51     uint8_t             sec;
52     uint32_t    tacc;   // ns
53     uint8_t             valid;
54     uint16_t    freq;   // 記録周波数 Hz
55 } __attribute__((__packed__));
56
57
58 typedef struct _recdata_head_t  recdata_head;
59
60 #define REC_DATALEN_HEAD        sizeof(recdata_head)
61
62 /*
63         Little Endian
64         3byte -> signed long(4byte)
65 */
66 static int32_t b3_to_long32(unsigned char *ptr)
67 {
68         char    buf[4];
69         
70         buf[0] = *ptr++;
71         buf[1] = *ptr++;
72         buf[2] = *ptr;
73         if (*ptr & 0x80) {
74                 buf[3] = 0xFF;
75         } else {
76                 buf[3] = 0;
77         }
78         return *((int32_t*)buf);
79 }
80
81 /*
82         data decode
83 */
84 static void read_high(FILE *fp, int chn)
85 {
86         recdata_head    d;
87         int     ch, j;
88         uint8_t data[AD_CH * AD_SAMPLE * AD_BYTES];
89
90         while(1) {
91                 // Header
92                 if (fread(&d, 1, REC_DATALEN_HEAD, fp) < 1) break;
93 //fprintf(stderr, "%02d:%02d:%02d.%03d %d %d", d.hour, d.min, d.sec, (1000*j/AD_SAMPLE), d.tacc, d.valid);
94                 if (d.freq > AD_SAMPLE || d.freq < 1) {
95                         fprintf(stderr, "read_high() Error freq=%d\n", d.freq);
96                         break;
97                 }
98                 int data_size = chn * d.freq * AD_BYTES;
99                 // AD data
100                 if (fread(data, 1, data_size, fp) < 1) break;
101                 for(j = 0; j < d.freq; j++) {
102                         fprintf(stdout, "%02d:%02d:%02d.%03d", d.hour, d.min, d.sec, (1000*j/d.freq));
103                         for(ch = 0; ch < chn; ch++) {
104                                 fprintf(stdout, ",%+07d", b3_to_long32((char*)(data + ch*d.freq*AD_BYTES + j*AD_BYTES)));
105                         }
106                         fprintf(stdout, "\n");
107                 }
108         }
109 }
110
111 int main(int argc, char *argv[])
112 {
113         FILE    *fpin;
114         FILE    *fpout;
115         char    fname_out[128];
116         char    sz[32];
117         unsigned char   buf[256];
118         int result;
119         int chn = AD_CH;
120         
121         if (argc == 1) {
122                 printf("sciLogger high sample data converter Ver"VER"\n");
123                 printf("Please set filename.\n");
124                 printf("Usage: \n");
125                 printf(EXE_NAME " -c CH_NUM inputfile\n");
126                 exit(0);
127         }
128         while((result = getopt(argc, argv, "c:")) != -1) {
129                 switch(result) {
130                         case 'c':
131                                 chn = atoi(optarg);
132                                 if (chn <= 0 || chn > AD_CH) {
133                                         fprintf(stderr, "Invalid CH num %d.\n", chn);
134                                         goto errout2;
135                                 }
136                                 break;
137                 }
138         }
139         //
140         fpin = fopen(argv[optind], "rb");
141
142         if (fpin == NULL) {
143                 fprintf(stderr, "Input file open error.\n");
144                 goto errout2;
145         }
146         // 出力ファイル名
147         strcpy(fname_out, argv[optind]);
148         strcat(fname_out, ".txt");
149
150         fpout = freopen(fname_out, "w", stdout);
151         if (fpout == NULL) {
152                 fprintf(stderr, "Output file open ERROR. %s\n", fname_out);
153                 goto errout1;
154         }
155         read_high(fpin, chn);
156
157         fclose(fpout);
158         fclose(fpin);
159         return 0;
160 errout1:
161         fclose(fpin);
162 errout2:
163         return -1;
164 }