1 | // NI_daq_2.cpp: Hauptprojektdatei.
|
2 | // Diese Software erkennt ein digitales Ausgangssignal eines Schmitt- Triggers welches auf ein Drucksignal triggern soll.
|
3 | // Der Trigger gibt den Startzeitpunkt um ein Analoges Signal eines Sensors auszulesen. Hier elektrochemische Sensor.
|
4 |
|
5 | #include "stdafx.h"
|
6 | #include <stdio.h>
|
7 | #include "NIDAQmx.h"
|
8 | #include <iostream>
|
9 |
|
10 | using namespace System;
|
11 | using namespace std;
|
12 | using namespace System::IO;
|
13 |
|
14 | #define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
|
15 |
|
16 | // ------Deklaration der Funktionen------//
|
17 |
|
18 |
|
19 | int ReadPulse(void); // Funktion für das digitale Signal
|
20 | double AnalogRead(void); // Funktion für das analoge Signal
|
21 |
|
22 | //------Auslesebereich des Analogen Signals------//
|
23 |
|
24 |
|
25 | double AnalogRead (void)
|
26 |
|
27 | {
|
28 | const int N=1000; // N: Anzahl der Abtastpunkte
|
29 |
|
30 | int32 error=0;
|
31 |
|
32 | TaskHandle taskHandle=0;
|
33 |
|
34 | int32 read;
|
35 |
|
36 | float64 data[N]; // Puffer für die abzuspeichernden Daten
|
37 |
|
38 | char errBuff[2048]={'\0'};
|
39 |
|
40 | int i;
|
41 |
|
42 | int u;
|
43 |
|
44 | int p;
|
45 |
|
46 | int t;
|
47 |
|
48 | int l;
|
49 |
|
50 | int r;
|
51 |
|
52 | int g;
|
53 |
|
54 | int x;
|
55 |
|
56 | int j;
|
57 |
|
58 | float neu_Integral;
|
59 |
|
60 | float IntegralFlächen[N-1];
|
61 |
|
62 | float Integral_Basisabgleich;
|
63 |
|
64 | float data_neu[N];
|
65 |
|
66 | float integral;
|
67 |
|
68 | float data_time[N]; // Puffer für die Zeitpunkte der Daten
|
69 |
|
70 | float minimum;
|
71 |
|
72 | float f_abtast; // f_abtast: Abtastfrequenz in Hz
|
73 |
|
74 | f_abtast = 75.0 ;
|
75 |
|
76 | String ^dateiname="..\\e.chem.Sensor.txt"; // Erstellen einer Textdatei
|
77 |
|
78 | FileStream ^fs=nullptr;
|
79 | StreamWriter ^sw =nullptr;
|
80 |
|
81 | // ------Task wird erstellt ("Messaufgabe")------//
|
82 |
|
83 |
|
84 | DAQmxErrChk (DAQmxCreateTask("", &taskHandle));
|
85 |
|
86 | //------Erstellen eines Channels um analoge Signale auszulesen------//
|
87 | //------(TaskHandle taskHandle, const char physicalChannel[], const char nameToAssignToChannel[], int32 terminalConfig,
|
88 | //------float64 minVal, float64 maxVal, int32 units, const char customScaleName[]);------//
|
89 |
|
90 | DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle, "Dev3/ai0", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));
|
91 |
|
92 | //------Erstellen der Abtastfunktion------//
|
93 | //------(TaskHandle taskHandle, const char source[], float64 rate, int32 activeEdge, int32 sampleMode, uInt64 sampsPerChanToAcquire);------//
|
94 |
|
95 | DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle, "", f_abtast, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, N));
|
96 |
|
97 | // ------Start des Tasks (Start der Messung)------//
|
98 |
|
99 |
|
100 | DAQmxErrChk(DAQmxStartTask(taskHandle));
|
101 |
|
102 | // ------Auslesen der anaolgen Abtastpunkte------//
|
103 | // ------(TaskHandle taskHandle, int32 numSampsPerChan, float64 timeout, bool32 fillMode, float64 readArray[], uInt32 arraySizeInSamps,
|
104 | // int32 *sampsPerChanRead, bool32 *reserved);------//
|
105 |
|
106 |
|
107 | DAQmxErrChk(DAQmxReadAnalogF64(taskHandle, N, 20.0, DAQmx_Val_GroupByChannel, data, N, &read, NULL)); // Timeout ( Zeit, die das Programm auf Daten wartet) = 20 s
|
108 |
|
109 | //------Ausgabe der Daten auf der Console------//
|
110 | if( read>1 )
|
111 | printf("Acquired %d samples\n",read);
|
112 | i=0;
|
113 | for(i=0;i<read;i++)
|
114 | {
|
115 | cout<<data[i]<<endl;
|
116 | }
|
117 | printf("--------------\n");
|
118 |
|
119 | //------Rechnung für die Zeitpunkte der Datenpunkte------//
|
120 |
|
121 | p=0;
|
122 | for(p=0;p<read;p++)
|
123 | {
|
124 | data_time[p]=p/f_abtast; // Zeit des Datenpunkts = Datenpunkt / Abtastfrequenz
|
125 | }
|
126 |
|
127 | //------Integral der Kurve ( grobe Näherung---keine Grenzwertbetrachtung----numerisch )------//
|
128 |
|
129 | r=0;
|
130 | for(r=0;r<read;r++)
|
131 | {
|
132 | data_neu[r]=((int) ((data[r]*10000)+0.5))/10000.0; // Runden der Datenpunkte auf die 4. Nachkommastelle
|
133 | }
|
134 |
|
135 |
|
136 | x=0;
|
137 | for(x=0;x<read-1;x++) // Ausrechnen des kompletten Integrals inclusive Basis
|
138 | {
|
139 | IntegralFlächen[x]=(data_neu[x]*(1/f_abtast));
|
140 | }
|
141 |
|
142 | g=0;
|
143 | neu_Integral=0;
|
144 | for(g=0;g<read;g++)
|
145 | {
|
146 | integral=neu_Integral+IntegralFlächen[g]; // Aufsummieren aller Teilflächen
|
147 | neu_Integral=integral;
|
148 | }
|
149 |
|
150 | j=0; // Finden des kleinsten Datenpunkts
|
151 | minimum=data_neu[0];
|
152 | for(j=0;j<(read-1);j++)
|
153 | {
|
154 | if (minimum<data_neu[j+1])
|
155 | {
|
156 | minimum=minimum;
|
157 | }
|
158 |
|
159 | else
|
160 | {
|
161 | minimum=data_neu[j+1];
|
162 | }
|
163 | }
|
164 |
|
165 | Integral_Basisabgleich= integral -(minimum*(N/f_abtast)); // Basisabgleich
|
166 |
|
167 | Integral_Basisabgleich=((int) ((Integral_Basisabgleich*10000)+0.5))/10000.0; // Runden des Integrals
|
168 |
|
169 |
|
170 | //------Schreiben der Textdaie in der die Datenpunkte aufgelistet sind------//
|
171 |
|
172 | try
|
173 | {
|
174 | fs= gcnew FileStream(dateiname,FileMode::Create);
|
175 | sw= gcnew StreamWriter(fs);
|
176 |
|
177 | u=0;
|
178 | for(u=0;u<read;u++)
|
179 | {
|
180 | sw->WriteLine ("{0} {1} {2}",data_neu[u],data_time[u],Integral_Basisabgleich);
|
181 | }
|
182 |
|
183 | }
|
184 |
|
185 | catch(Exception ^e)
|
186 | {
|
187 | Console::WriteLine(e->Message);
|
188 | }
|
189 |
|
190 | finally
|
191 | {
|
192 | if(sw)
|
193 | sw->Close();
|
194 | }
|
195 |
|
196 | Console::WriteLine();
|
197 |
|
198 |
|
199 |
|
200 | Error:
|
201 |
|
202 | if( DAQmxFailed(error) )
|
203 |
|
204 | DAQmxGetExtendedErrorInfo(errBuff,2048);
|
205 |
|
206 | if( taskHandle!=0 ) {
|
207 |
|
208 | DAQmxStopTask(taskHandle);
|
209 |
|
210 | DAQmxClearTask(taskHandle);
|
211 |
|
212 | }
|
213 |
|
214 | if( DAQmxFailed(error) )
|
215 |
|
216 | printf("DAQmx Error: %s\n",errBuff);
|
217 |
|
218 | return 0;
|
219 |
|
220 |
|
221 | }
|
222 |
|
223 |
|
224 | //------Hauptprogramm------Enthält die Funktion für das digitale Signal------//
|
225 |
|
226 | int main(void)
|
227 |
|
228 | {
|
229 |
|
230 | int binaer; // binaer : Variable die den Zustand des Schmitt-Triggers ( digitales Signal) übergeben bekommt
|
231 | int oldbinaer; // oldbinaer : Vergleichsvariable
|
232 |
|
233 | oldbinaer=1; // Initialisierung
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | for(;;){ // Endlosschleife in der permanent das digitale Signal abgefragt wird
|
239 | binaer = ReadPulse(); // ReadPulse : Funktion um das digitale Signal abzufragen
|
240 |
|
241 | if (binaer < oldbinaer ) // Idee : wenn der jetztige Zustand kleiner ist als der vorherige,
|
242 | { // soll getriggert werden ( starten das analoge Signal auszulesen).
|
243 | AnalogRead(); // ---> auf abfallende Flanke des Schmitt- Triggers wird getriggert
|
244 | }
|
245 |
|
246 | oldbinaer=binaer; // oldbinaer übernimmt für den nächsten Vergleich den jetztigen Zusatnd an
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | } return 0;
|
253 | }
|
254 |
|
255 | int ReadPulse (void)
|
256 | {
|
257 | int32 error=0;
|
258 | TaskHandle taskHandle=0;
|
259 | uInt8 data_0[100];
|
260 | char errBuff[2048]={'\0'};
|
261 | int32 i;
|
262 | int32 read,bytesPerSamp;
|
263 |
|
264 | /*********************************************/
|
265 | // DAQmx Configure Code
|
266 | /*********************************************/
|
267 | DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
|
268 | DAQmxErrChk (DAQmxCreateDIChan(taskHandle,"Dev3/port0/line0","",DAQmx_Val_ChanForAllLines));
|
269 |
|
270 | /*********************************************/
|
271 | // DAQmx Start Code
|
272 | /*********************************************/
|
273 | DAQmxErrChk (DAQmxStartTask(taskHandle));
|
274 |
|
275 | /*********************************************/
|
276 | // DAQmx Read Code
|
277 | /*********************************************/
|
278 | DAQmxErrChk (DAQmxReadDigitalLines(taskHandle,1,10.0,DAQmx_Val_GroupByChannel,data_0,100,&read,&bytesPerSamp,NULL));
|
279 |
|
280 | // assuming 8 channels acquired
|
281 | for(i=0;i<1;++i)
|
282 | //printf("channel %d: 0x%X\n",i,data_0[i]);
|
283 |
|
284 | Error:
|
285 | if( DAQmxFailed(error) )
|
286 | DAQmxGetExtendedErrorInfo(errBuff,2048);
|
287 | if( taskHandle!=0 ) {
|
288 | /*********************************************/
|
289 | // DAQmx Stop Code
|
290 | /*********************************************/
|
291 | DAQmxStopTask(taskHandle);
|
292 | DAQmxClearTask(taskHandle);
|
293 | }
|
294 | if( DAQmxFailed(error) )
|
295 | printf("DAQmx Error: %s\n",errBuff);
|
296 | //printf("End of program, press Enter key to quit\n");
|
297 | //getchar();
|
298 | return( data_0[0]);
|
299 | }
|
300 |
|
301 |
|
302 | -------------------------------------------------
|