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