comparison file-data-provider.c @ 88:9b7588cd4013

Move data acquisition into separate thread The data acquisition thread transfers data to the main thread trough a Thread Safe Queue. This separation allows for making the data acquisition system pluggable. The old file based communication to the KK counter is implemented along with a very simple fake data provider.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Thu, 21 Mar 2013 18:24:45 +0100
parents
children c9aec93005a4
comparison
equal deleted inserted replaced
87:0950d4b3a45c 88:9b7588cd4013
1 /* FXAnalise data provider reading data from KK data file on disc */
2
3 #include <ansi_c.h>
4 #include <utility.h>
5 #include <formatio.h>
6
7 //#define LOGFILEPATH "C:\\Femto\\Software\\FXQE80"
8 #define LOGFILEPATH "C:\\temp"
9
10 /* data acquisition flag */
11 extern int acquiring;
12 /* data queue */
13 extern CmtTSQHandle dataQueue;
14
15 void CurrentFileName(char *fname)
16 {
17 char day[3], month[3], year[3];
18 char *date = DateStr();
19 Scan(date, "%s>%s[w2]-%s[w2]-20%s[w2]", month, day, year);
20 Fmt(fname, "%s<%s\\%s%s%s_Frequ.txt", LOGFILEPATH, year, month, day);
21 }
22
23 #define FXLINELENGTH 123
24
25 int CVICALLBACK FileDataProvider (void *functionData)
26 {
27 int mainThreadId;
28
29 /* get main thread id to post messages to it */
30 mainThreadId = CmtGetMainThreadID();
31
32 int LogFile;
33 long LogFileSize;
34 char LogFileName[MAX_PATHNAME_LEN];
35 long OldLogFilePtr = 0;
36 char LineBuffer[FXLINELENGTH + 10];
37
38 char TimeTag[] = "100103 000000.000"; // K+K time tag meaning here 2010 january the 3rd at 00:00:00.000
39 char Year[] = "2010";
40 char Month[] = "01";
41 char Day[] = "03";
42 char Hour[] = "00";
43 char Min[] = "00" ;
44 char Sec[] = "00.000";
45 struct tm LocalTime ;
46 time_t utcTime;
47
48 double data[5];
49
50 CurrentFileName(LogFileName);
51 GetFileInfo(LogFileName, &OldLogFilePtr);
52 OldLogFilePtr -= OldLogFilePtr%FXLINELENGTH + FXLINELENGTH - 2;
53
54 while (acquiring) {
55
56 GetFileInfo(LogFileName, &LogFileSize);
57 if (LogFileSize > OldLogFilePtr+2*FXLINELENGTH-2) { // if a complete newline has been written
58
59 // Open Log file and get to the beginning of newly completed line
60 LogFile = OpenFile(LogFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
61 OldLogFilePtr += FXLINELENGTH;
62 SetFilePtr(LogFile, OldLogFilePtr, 0);
63
64 // return the last complete string from the log file and scan it for date and time information
65
66 // first, the time tag, and store it in various formats
67 ReadFile(LogFile, TimeTag, 17);
68
69 CopyBytes(Year,2,TimeTag,0,2); // first 2 bytes of year string remains "20"
70 CopyBytes(Month,0,TimeTag,2,2);
71 CopyBytes(Day,0,TimeTag,4,2);
72 CopyBytes(Hour,0,TimeTag,7,2);
73 CopyBytes(Min,0,TimeTag,9,2);
74 CopyBytes(Sec,0,TimeTag,11,6);
75
76 Fmt(&LocalTime.tm_year, "%d<%s", Year);
77 Fmt(&LocalTime.tm_mon, "%d<%s", Month);
78 Fmt(&LocalTime.tm_mday, "%d<%s", Day);
79 Fmt(&LocalTime.tm_hour, "%d<%s", Hour);
80 Fmt(&LocalTime.tm_min, "%d<%s", Min);
81 Fmt(&LocalTime.tm_sec, "%d<%s", "00"); // special case to handle non integer number of UTC seconds
82
83 LocalTime.tm_hour += 0;
84 LocalTime.tm_min -= 0;
85 LocalTime.tm_sec -= 0;
86 LocalTime.tm_mon -= 1; // january is month 0 for tm struct
87 LocalTime.tm_year -= 1900; // year is number of years since 1900 for tm struct
88 LocalTime.tm_isdst = -1; // daylight saving flag MUST be set to -1 (unallocated is bugging and +1 is making 1 hour error in summer)
89
90 utcTime = mktime (&LocalTime);
91 data[0] = (double) utcTime + strtod(Sec,NULL);
92
93 // scan the line for counters's channels information
94
95 ReadLine(LogFile, LineBuffer, FXLINELENGTH+9);
96 CloseFile(LogFile);
97
98 Scan(LineBuffer, "%f%f%f%f", &data[1], &data[2], &data[3], &data[4]);
99
100 /* convert from kHz to Hz */
101 data[1] = data[1] * 1000.0;
102 data[2] = data[2] * 1000.0;
103 data[3] = data[3] * 1000.0;
104 data[4] = data[4] * 1000.0;
105
106 /* push data into the data queue */
107 CmtWriteTSQData(dataQueue, data, 5, TSQ_INFINITE_TIMEOUT, 0);
108
109 // Special case to handle change of day at next second
110 if ( LocalTime.tm_hour==23 && LocalTime.tm_min==59 && strtod(Sec,NULL)>=58 ) {
111 do {
112 Delay(5.1);
113 CurrentFileName(LogFileName);
114 } while (!GetFileInfo(LogFileName, &OldLogFilePtr));
115 OldLogFilePtr = 2;
116 }
117 }
118 Delay(0.05);
119 }
120 return 0;
121 }