-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
249 lines (211 loc) · 8.74 KB
/
Source.cpp
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
#include <stdlib.h>
#include <stdio.h>
//#include <conio.h>
//#include<bitset>
//#include<string>
#pragma warning(disable : 4996) // Ýòî íóæíî òîêëüî äëÿ VS ñè++
#define Max_Kol_Fra 1024 // Ìàêñèìàëüíîå êîëè÷åñòâî ôðåéìîâ
// typedef int bool ; // Ýòî íóæíî ñòîëüêî äëÿ ñòàðîãî ÑÈ
// const int true =1; // ó íåãî íåòó ýòîãî
// const int false=0; //
unsigned int POS = 0; // Óêàçàòåëü íà ÷èòàåìûé áàéò
unsigned int NOM = 0; // Íîìåð ïîñëåäíåãî ïðî÷èòàíîãî ôðåéìà
unsigned int SIZ = 0; // Ìàêñèìàëüíîå êîëè÷åñòâî äàííûõ â ïîòîêå
char*source; // Ïîòîê äàííûõ
typedef struct sFrame {
unsigned char AC[4]; unsigned int Frame_Number; // Frame Header 8
unsigned char AB[8]; // DDO Header 8
unsigned long long DDO[7]; // DDO Data 7*8 56
unsigned char DD[4]; unsigned char ART[4]; // Art Header +Art 8
unsigned char AD[8]; // L0 Data Header 8
} sFrame; // Ñòðóêòóðà îïèñûâàþùàÿ ôðåéì
sFrame*Frames[Max_Kol_Fra];// Ìàññèâ ñ ôðåéìàìè
int FSizes[Max_Kol_Fra];// Ðàìåð äàííûõ ôðåìéìà
//auto x = 1,23423423;
char *FileToStr(FILE *file) { // Ïåðåâîäèò àïðèîðå ñóùåñòâóþùèé îòêðûòûé ôàéë â ñòðîêó
char *str = NULL;// ÍÅêàÿ ñòðîêà â ïàìÿòè ïàìÿòü ïîä íå¸ íå âûäåëåíà
int len = 0;// ÐÀçìåð ôàéëà â áàéòàõ
int real_len = 0;// ðåàëüíàÿ äëèíà ñòðîêè íàâåðíî òèïà òîãî
int lines = 0;// Äîëæíî áûëî áûòü êîëè÷åñèòâî ñòðîê
fseek(file, 0, SEEK_END);// ïåðåìåùàåìñÿ â êîíåö ôàéëà
len = ftell(file);// ÏÎëó÷àåì ðàçìåð ôàéëà â áàéòàõ
SIZ = len;// Çàïîìèíàåì ðàçìåð äàííûõ
fseek(file, 0, SEEK_SET);// Ïåðåìåùàåìñÿ â íà÷àëî ôàéëà
str = (char*)malloc(len + 128);// Âûäåëÿåì ïàìÿòü êóäà çàãðóçèì ôàéë ó÷àñòîê êàê ñòðîêà
int Read_Byte;
int kol = 0;
do {
Read_Byte = fread(&str[kol], 128, 1, file);// Çàãðóæàåì ôàéë
kol = kol + 128;
}
while (Read_Byte == 1);
fclose(file);
return str;// Âîçâðàùàåì óêàçàòåëü íà ñòðîêó ñîäåðæàùèþ òåêñò ïðîãðàììû ôàéë
}
void StrToFile(FILE *file, char* iStr) { // çàïèñûâàåò â ôàéë
int kol = 0;
int Write_Byte;
fseek(file, 0, SEEK_SET);// Ïåðåìåùàåìñÿ â íà÷àëî ôàéëà
do {
Write_Byte = fwrite(&iStr[kol], 128, 1, file);// Çàãðóæàåì ôàéë
kol = kol + 128;
}
while ((Write_Byte == 1) && (kol < SIZ));
fclose(file);
}
bool Find_Start_Frame() {
bool ex = false;
while (!ex&&SIZ > POS) {
if ((*(unsigned long long*)&source[POS] == 0xbcbcbcbcbcbcbcbc) && (*(unsigned char*) &source[POS + 8] != 0xbc))
{
ex = true;
POS = POS + 8;
}
else POS++;
}
if (POS >= SIZ) return false;
else
return true;
}
bool Find_Next_Frame() {
unsigned int Kol_BC = 0;
while (*(unsigned int*)&source[POS] != 0xACACACAC && SIZ > POS) {
Kol_BC = Kol_BC + 1;
POS = POS + 8;
}
FSizes[NOM - 1] = Kol_BC;
if (POS >= SIZ) return false;
else
return true;
}
bool Read_Frame() {
Frames[NOM] = (sFrame*)&source[POS];
// ïðîâåðêà ôðåéìà
int f = 0;
bool Er = false;
for (f = 0; f < 4; f++)
if (Frames[NOM]->AC[f] != 0xAC)
Er = true;
for (f = 0; f < 8; f++)
if (Frames[NOM]->AB[f] != 0xAB)
Er = true;
for (f = 0; f < 8; f++)
if (Frames[NOM]->AD[f] != 0xAD)
Er = true;
for (f = 0; f < 4; f++)
if (Frames[NOM]->DD[f] != 0xDD)
Er = true;
if (Er) printf("Frame %d format Error ", NOM);
POS = POS + sizeof(sFrame);// Ïåðåìåùàåìñÿ â êîíåö ôðåéìà
NOM++;// Óâåëè÷èâàåì íîìåð ôðåéìà
return !Er;
}
int main()
{
// Ñäåñü ×èòàåì Ôàéë ======================================================
FILE *f;// Ôàéë ñ èñõîäíûì êîäîì ïðîãðàììû
if ((f = fopen("L0_085_sk_00__2715.txt", "rb")) != NULL) // Îòêðûòèå è Ïðîâåðêà íà ñóùåñòâîâàíèå ôàéëà
source = FileToStr(f); else
printf("File not found");
//=========================================================================
// ×òåíèå ôðåéìîâ =========================================================
POS = 0;// Óñòàíàâëèâàåì óêàçàòåëü íà íà÷àëî äàííûõ
NOM = 0;// Êîëè÷åñòâî ôðåéìîâ
if (Find_Start_Frame()) // Èùèì ñòàðòîâûé ôðåéì
while (Read_Frame() && Find_Next_Frame()); // íàõîäèì âñå ôðåéìû
// ========================================================================
printf(" Count Frame %d \r\n", NOM);// Âûâîäèì êîëè÷åñòâî ôðåéìîâ
// ========================================================================
FILE *fp;
if ((fp = fopen("tes.txt", "w")) == NULL) {
printf("Cannot open file.\n");
exit(1);
//retun 1
}
// fprintf(fp, "this is a test %d %f", 10, 20.01);
//fclose(fp);
int Nom_Fram = 0; // Âûâîä ôðåìéîâ
for (Nom_Fram = 0; Nom_Fram < NOM; Nom_Fram++) {
//------------------------------------------------------------------------
printf("=============================================================\r\n");
printf("=============================================================\r\n");
printf(" EVENT %d ", Frames[Nom_Fram]->Frame_Number); // Âûâîäèì DDO
printf(" DDO DATA \r\n");
//*******************************************************************
fprintf(fp,"=============================================================\r\n");
fprintf(fp,"=============================================================\r\n");
fprintf(fp," EVENT %d ", Frames[Nom_Fram]->Frame_Number); // Âûâîäèì DDO
fprintf(fp," DDO DATA \r\n");
int n = 0; int b = 0;
for (n = 0; n < 7; n++)
{
for (b = 54; b >= 0; b = b - 6)
{
fprintf(fp, "%3d ", (Frames[Nom_Fram]->DDO[n] >> b) & 0x0000003F);
printf("%3d ", (Frames[Nom_Fram]->DDO[n] >> b) & 0x0000003F);
}
printf("\r\n");
fprintf(fp,"\r\n");
}
//*** ***********************************************************************
//------------------------------------------------------------------------
unsigned int buf_part;
//int32_t buf_part;
//std::bitset<32> buf_part;
// âûâîäèì çàãîëîâîê
printf("ART: %d \r\n", Frames[Nom_Fram]->ART[0]);
printf("Header: \r\n");
fprintf(fp, "ART: %d \r\n", Frames[Nom_Fram]->ART[0]);
fprintf(fp, "Header: \r\n");
buf_part = *(unsigned int*) ((char*)Frames[Nom_Fram] + sizeof(sFrame));
printf("%2d ", ((buf_part >> 31) & 0x00000001)); // V (1)
printf("%2d ", ((buf_part >> 30) & 0x00000001)); // P (1)
printf("%2d ", ((buf_part >> 28) & 0x00000003)); // orb (2)
printf("%5d ", ((buf_part >> 16) & 0x00000FFF)); // BCID (12)
printf("\r\n");
//************************************************************
fprintf(fp,"%2d ", ((buf_part >> 31) & 0x00000001)); // V (1)
fprintf(fp,"%2d ", ((buf_part >> 30) & 0x00000001)); // P (1)
fprintf(fp,"%2d ", ((buf_part >> 28) & 0x00000003)); // orb (2)
fprintf(fp,"%5d ", ((buf_part >> 16) & 0x00000FFF)); // BCID (12)
fprintf(fp,"\r\n");
//------------------------------------------------------------------------
int i = 0; // Âûâîäèì äàííûå
for (i = 0; i < FSizes[Nom_Fram] - 1; i++) {
buf_part = *(unsigned int*)((char*)Frames[Nom_Fram] + sizeof(sFrame) + (i * 8 + 8));
printf("%2d ", ((buf_part >> 31) & 0x00000001)); // 1 (1)
printf("%2d ", ((buf_part >> 30) & 0x00000001)); // P (1)
printf("%2d ", ((buf_part >> 29) & 0x00000001)); // R (1)
printf("%5d ", ((buf_part >> 28) & 0x00000001)); // T (1)
printf("%6d ", ((buf_part >> 22) & 0x0000003F)); // Chan# (6)
printf("%7d ", ((buf_part >> 12) & 0x000003FF)); // ADC (10)
printf("%4d ", ((buf_part >> 4) & 0x000000FF)); // TDC (8)
printf("%2d ", ((buf_part >> 3) & 0x00000001)); // N (1)
printf("%3d ", ((buf_part >> 0) & 0x00000007)); // rel BCID (3)
printf("\r\n");
//********************************************
fprintf(fp,"%2d ", ((buf_part >> 31) & 0x00000001)); // 1 (1)
fprintf(fp,"%2d ", ((buf_part >> 30) & 0x00000001)); // P (1)
fprintf(fp,"%2d ", ((buf_part >> 29) & 0x00000001)); // R (1)
fprintf(fp,"%5d ", ((buf_part >> 28) & 0x00000001)); // T (1)
fprintf(fp,"%6d ", ((buf_part >> 22) & 0x0000003F)); // Chan# (6)
fprintf(fp,"%7d ", ((buf_part >> 12) & 0x000003FF)); // ADC (10)
fprintf(fp,"%4d ", ((buf_part >> 4) & 0x000000FF)); // TDC (8)
fprintf(fp,"%2d ", ((buf_part >> 3) & 0x00000001)); // N (1)
fprintf(fp,"%3d ", ((buf_part >> 0) & 0x00000007)); // rel BCID (3)
fprintf(fp,"\r\n");
}
// _getch();
}
// Frame[Íîìåð Ôðåéìà]->Ïîëÿ ôðåéìà
// Frames[0]->Frame_Number;// Íîìåð ôðåéìà
// Frames[0]->DDO[0];// DDO âñêèå
// Frames[0]->ART;// ART
// Çàïèñü â ôàéë ==========================================================
//f = fopen("test.txt","wb");
//StrToFile(f,source);// Òàì íåìíîãî õâîñòèê îáðàçóåòüñÿ
//=========================================================================
fclose(fp);
system("pause");
return 0;
}