Subversion Repositories f9daq

Rev

Rev 306 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 306 Rev 311
1
#include "K6517-ctrl.h"
1
#include "K6517-ctrl.h"
2
#include <tcpsupp.h>
2
#include <tcpsupp.h>
3
#include <rs232.h>
3
#include <rs232.h>
4
#include <utility.h>
4
#include <utility.h>
5
#include <userint.h>
5
#include <userint.h>
6
#include <ansi_c.h>
6
#include <ansi_c.h>
7
#include <pw18-1.8aq.h>
7
#include <pw18-1.8aq.h>
8
#include "TempCtrl.h"
8
#include "TempCtrl.h"
9
FILE *gFp;
9
FILE *gFp;
10
 
10
 
11
#define RSTREG(a,x) (a&=(~(x)))
11
#define RSTREG(a,x) (a&=(~(x)))
12
#define SETREG(a,x) (a|=x)
12
#define SETREG(a,x) (a|=x)
13
 
13
 
14
int gMask=0xF;
14
int gMask=0xF;
15
 
15
 
16
static int pa;
16
static int pa;
17
 
17
 
18
#define TEXT_LENGTH     2000
18
#define TEXT_LENGTH     2000
19
char read_data[TEXT_LENGTH];
19
char read_data[TEXT_LENGTH];
20
 
20
 
21
int read_term_index;
21
int read_term_index;
22
int read_term;
22
int read_term;
23
int read_cnt;
23
int read_cnt;
24
int bytes_read;
24
int bytes_read;
25
 
25
 
26
#define COM_PORT 11
26
#define COM_PORT 11
27
 
27
 
28
 
28
 
29
typedef struct {
29
typedef struct {
30
        double dState;          // Last temperature input
30
        double dState;          // Last temperature input
31
        double iState;          // Integrator state
31
        double iState;          // Integrator state
32
        double iMax, iMin;      // Maximum and minimum allowable integrator state
32
        double iMax, iMin;      // Maximum and minimum allowable integrator state
33
        double  iGain,          // integral gain
33
        double  iGain,          // integral gain
34
                                        pGain,          // proportional gain
34
                                        pGain,          // proportional gain
35
                                        dGain;          // derivative gain
35
                                        dGain;          // derivative gain
36
} SPid;
36
} SPid;
37
 
37
 
38
SPid pid;
38
SPid pid;
39
 
39
 
40
double UpdatePID(SPid *pid, double error, double temperature) {
40
double UpdatePID(SPid *pid, double error, double temperature) {
41
        double pTerm, dTerm, iTerm;
41
        double pTerm, dTerm, iTerm;
42
        pTerm = pid->pGain * error;
42
        pTerm = pid->pGain * error;
43
        // calculate the proportional term
43
        // calculate the proportional term
44
        // calculate the integral state with appropriate limiting
44
        // calculate the integral state with appropriate limiting
45
        pid->iState += error;
45
        pid->iState += error;
46
        if (pid->iState > pid->iMax)      pid->iState = pid->iMax;
46
        if (pid->iState > pid->iMax)      pid->iState = pid->iMax;
47
        else if (pid->iState < pid->iMin) pid->iState = pid->iMin;
47
        else if (pid->iState < pid->iMin) pid->iState = pid->iMin;
48
        //if (debug )
48
        //if (debug )
49
        iTerm = pid->iGain * pid->iState;  // calculate the integral term
49
        iTerm = pid->iGain * pid->iState;  // calculate the integral term
50
        dTerm = pid->dGain * (temperature - pid->dState);
50
        dTerm = pid->dGain * (temperature - pid->dState);
51
        pid->dState = temperature;
51
        pid->dState = temperature;
52
        return pTerm + iTerm - dTerm;
52
        return pTerm + iTerm - dTerm;
53
}
53
}
54
 
54
 
55
void SetDimming(int state) {
55
void SetDimming(int state) {
56
        SetCtrlAttribute (pa, PA_START, ATTR_DIMMED, state);
56
        SetCtrlAttribute (pa, PA_START, ATTR_DIMMED, state);
57
        SetCtrlAttribute (pa, PA_EXIT, ATTR_DIMMED, state);
57
        SetCtrlAttribute (pa, PA_EXIT, ATTR_DIMMED, state);
58
        SetCtrlAttribute (pa, PA_STOP, ATTR_DIMMED, !state);
58
        SetCtrlAttribute (pa, PA_STOP, ATTR_DIMMED, !state);
59
}
59
}
60
 
60
 
61
 
61
 
62
 
62
 
63
int CVICALLBACK ReadVoltageCurrentCB (int panel, int control, int event,
63
int CVICALLBACK ReadVoltageCurrentCB (int panel, int control, int event,
64
                                                                                                                                                        void *callbackData, int eventData1, int eventData2) {
64
                                                                                                                                                        void *callbackData, int eventData1, int eventData2) {
65
 
65
 
66
        int iRet;
66
        int iRet;
67
        char ch=0;
67
        char ch=0;
68
        double Voltage;
68
        double Voltage;
69
        double Current;
69
        double Current;
70
        char cv_cc;
70
        char cv_cc;
71
        switch (event) {
71
        switch (event) {
72
                case EVENT_COMMIT:
72
                case EVENT_COMMIT:
73
                        GetCtrlVal(pa,PA_CHANNEL, &ch);
73
                        GetCtrlVal(pa,PA_CHANNEL, &ch);
74
                        iRet = TMI_TimeOut(TMI_DeviceId, 1);
74
                        iRet = TMI_TimeOut(TMI_DeviceId, 1);
75
                        iRet = TMI_Refresh(TMI_DeviceId);
75
                        iRet = TMI_Refresh(TMI_DeviceId);
76
 
-
 
77
 
-
 
78
                        iRet = TMI_MoniDataQ(TMI_DeviceId, ch+1, &Voltage, &Current, &cv_cc);
76
                        iRet = TMI_MoniDataQ(TMI_DeviceId, ch+1, &Voltage, &Current, &cv_cc);
79
 
77
 
80
                        SetCtrlVal(panel, PA_VMON, Voltage);
78
                        SetCtrlVal(panel, PA_VMON, Voltage);
81
                        SetCtrlVal(panel, PA_IMON, Current);
79
                        SetCtrlVal(panel, PA_IMON, Current);
82
                        SetCtrlVal(panel, PA_CVCC, cv_cc);
80
                        SetCtrlVal(panel, PA_CVCC, cv_cc);
83
 
81
 
84
                        break;
82
                        break;
85
        }
83
        }
86
        return 0;
84
        return 0;
87
}
85
}
88
 
86
 
89
 
87
 
90
int CVICALLBACK SelectChannelCB (int panel, int control, int event,
88
int CVICALLBACK SelectChannelCB (int panel, int control, int event,
91
                                                                                                                                 void *callbackData, int eventData1, int eventData2) {
89
                                                                                                                                 void *callbackData, int eventData1, int eventData2) {
92
        unsigned char state, preset;
90
        unsigned char state, preset;
93
        double Voltage, Current;
91
        double Voltage, Current;
94
        unsigned char ch;
92
        unsigned char ch;
95
        switch (event) {
93
        switch (event) {
96
                case EVENT_COMMIT:
94
                case EVENT_COMMIT:
97
                        GetCtrlVal(pa,PA_CHANNEL, &ch);
95
                        GetCtrlVal(pa,PA_CHANNEL, &ch);
98
                        GetCtrlVal(pa,PA_PRESET, &preset);
96
                        GetCtrlVal(pa,PA_PRESET, &preset);
99
 
-
 
100
 
97
 
101
 
-
 
102
                        TMI_VoltageQ(TMI_DeviceId, ch+1, preset, &Voltage);
98
                        TMI_VoltageQ(TMI_DeviceId, ch+1, preset, &Voltage);
103
                        TMI_CurrentQ(TMI_DeviceId, ch+1, preset, &Current);
99
                        TMI_CurrentQ(TMI_DeviceId, ch+1, preset, &Current);
-
 
100
           
104
                        SetCtrlVal(pa, PA_VSET, Voltage);
101
                        SetCtrlVal(pa, PA_VSET, Voltage);
105
                        SetCtrlVal(pa, PA_ISET, Current);
102
                        SetCtrlVal(pa, PA_ISET, Current);
106
 
103
 
107
                        break;
104
                        break;
108
        }
105
        }
109
        return 0;
106
        return 0;
110
}
107
}
111
 
108
 
-
 
109
int SetCooling(char onoff){
-
 
110
        int nb=0;
-
 
111
  nb = ComWrt (COM_PORT, &onoff, 1);  
-
 
112
        return nb;
-
 
113
}
112
 
114
 
113
#define tcpChk(f) if ((g_TCPError=(f)) < 0) {ReportTCPError(); return -1; }
115
#define tcpChk(f) if ((g_TCPError=(f)) < 0) {ReportTCPError(); return -1; }
114
 
116
 
115
/*---------------------------------------------------------------------------*/
117
/*---------------------------------------------------------------------------*/
116
/* Module-globals                                                            */
118
/* Module-globals                                                            */
117
/*---------------------------------------------------------------------------*/
119
/*---------------------------------------------------------------------------*/
118
static unsigned int g_hconversation;
120
static unsigned int g_hconversation;
119
static int                      g_TCPError = 0;
121
static int                      g_TCPError = 0;
120
 
122
 
121
/*---------------------------------------------------------------------------*/
123
/*---------------------------------------------------------------------------*/
122
/* Internal function prototypes                                              */
124
/* Internal function prototypes                                              */
123
/*---------------------------------------------------------------------------*/
125
/*---------------------------------------------------------------------------*/
124
/*---------------------------------------------------------------------------*/
126
/*---------------------------------------------------------------------------*/
125
/* Report TCP Errors if any                                                                      */
127
/* Report TCP Errors if any                                                                      */
126
/*---------------------------------------------------------------------------*/
128
/*---------------------------------------------------------------------------*/
127
static void ReportTCPError (void) {
129
static void ReportTCPError (void) {
128
        if (g_TCPError < 0) {
130
        if (g_TCPError < 0) {
129
                char    messageBuffer[1024];
131
                char    messageBuffer[1024];
130
                sprintf(messageBuffer,
132
                sprintf(messageBuffer,
131
                                                "TCP library error message: %s\nSystem error message: %s",
133
                                                "TCP library error message: %s\nSystem error message: %s",
132
                                                GetTCPErrorString (g_TCPError), GetTCPSystemErrorString());
134
                                                GetTCPErrorString (g_TCPError), GetTCPSystemErrorString());
133
                MessagePopup ("Error", messageBuffer);
135
                MessagePopup ("Error", messageBuffer);
134
        }
136
        }
135
}
137
}
136
 
138
 
137
 
139
 
138
int CVICALLBACK ServerTCPCB (unsigned handle, int event, int error,
140
int CVICALLBACK ServerTCPCB (unsigned handle, int event, int error,
139
                                                                                                                 void *callbackData) {
141
                                                                                                                 void *callbackData) {
140
        char receiveBuf[256] = {0};
142
        char receiveBuf[256] = {0};
141
        ssize_t dataSize        = sizeof (receiveBuf) - 1;
143
        ssize_t dataSize        = sizeof (receiveBuf) - 1;
142
        char addrBuf[31];
144
        char addrBuf[31];
143
 
145
 
144
        switch (event) {
146
        switch (event) {
145
                case TCP_CONNECT:
147
                case TCP_CONNECT:
146
                        if (g_hconversation) {
148
                        if (g_hconversation) {
147
                                /* We already have one client, don't accept another... */
149
                                /* We already have one client, don't accept another... */
148
                                tcpChk (GetTCPPeerAddr (handle, addrBuf, 31));
150
                                tcpChk (GetTCPPeerAddr (handle, addrBuf, 31));
149
                                sprintf (receiveBuf, "-- Refusing conection request from "
151
                                sprintf (receiveBuf, "-- Refusing conection request from "
150
                                                                 "%s --\n", addrBuf);
152
                                                                 "%s --\n", addrBuf);
151
                                printf("%s\n", receiveBuf);
153
                                printf("%s\n", receiveBuf);
152
                                tcpChk (DisconnectTCPClient (handle));
154
                                tcpChk (DisconnectTCPClient (handle));
153
                        } else {
155
                        } else {
154
                                /* Handle this new client connection */
156
                                /* Handle this new client connection */
155
                                g_hconversation = handle;
157
                                g_hconversation = handle;
156
                                tcpChk (GetTCPPeerAddr (g_hconversation, addrBuf, 31));
158
                                tcpChk (GetTCPPeerAddr (g_hconversation, addrBuf, 31));
157
                                printf("%s\n", addrBuf);
159
                                printf("%s\n", addrBuf);
158
                                tcpChk (GetTCPPeerName (g_hconversation, receiveBuf, 256));
160
                                tcpChk (GetTCPPeerName (g_hconversation, receiveBuf, 256));
159
                                printf("%s\n", receiveBuf);
161
                                printf("%s\n", receiveBuf);
160
                                sprintf (receiveBuf, "-- New connection from %s --\n",
162
                                sprintf (receiveBuf, "-- New connection from %s --\n",
161
                                                                 addrBuf);
163
                                                                 addrBuf);
162
                                printf("%s\n", receiveBuf);
164
                                printf("%s\n", receiveBuf);
163
 
165
 
164
                                /* Set the disconect mode so we do not need to terminate */
166
                                /* Set the disconect mode so we do not need to terminate */
165
                                /* connections ourselves. */
167
                                /* connections ourselves. */
166
                                tcpChk (SetTCPDisconnectMode (g_hconversation, TCP_DISCONNECT_AUTO));
168
                                tcpChk (SetTCPDisconnectMode (g_hconversation, TCP_DISCONNECT_AUTO));
167
                        }
169
                        }
168
                        break;
170
                        break;
169
                case TCP_DATAREADY:
171
                case TCP_DATAREADY:
170
                        if ((dataSize = ServerTCPRead (g_hconversation, receiveBuf, dataSize, 1000)) < 0) {
172
                        if ((dataSize = ServerTCPRead (g_hconversation, receiveBuf, dataSize, 1000)) < 0) {
171
                                printf("Receive Error\n");
173
                                printf("Receive Error\n");
172
                        } else {
174
                        } else {
173
                                receiveBuf[dataSize] = '\0';
175
                                receiveBuf[dataSize] = '\0';
174
                                printf("Received %s\n", receiveBuf);
176
                                printf("Received %s\n", receiveBuf);
175
                                int cmd=0;
177
                                int cmd=0;
176
                                float arg;
178
                                float arg;
177
                                sscanf("%d%f",&cmd,&arg);
179
                                sscanf(receiveBuf,"%d%f",&cmd,&arg);
178
                                switch (cmd){
180
                                switch (cmd){
179
                                        case 1:
181
                                        case 1:
180
                                                SetCtrlVal(ps.PA_TSET,arg);
182
                                                SetCtrlVal(pa,PA_TSET,arg);
181
                                                break;
183
                                                break;
182
                                        default:
184
                                        default:
183
                                                break;
185
                                                break;
184
                                 
186
                                 
185
                                }
187
                                }
186
                        }
188
                        }
187
                        break;
189
                        break;
188
                case TCP_DISCONNECT:
190
                case TCP_DISCONNECT:
189
                        if (handle == g_hconversation) {
191
                        if (handle == g_hconversation) {
190
                                /* The client we were talking to has disconnected... */
192
                                /* The client we were talking to has disconnected... */
191
                                g_hconversation = 0;
193
                                g_hconversation = 0;
192
                                printf("-- Client disconnected --\n");
194
                                printf("-- Client disconnected --\n");
193
 
195
 
194
                                /* Note that we do not need to do any more because we set the*/
196
                                /* Note that we do not need to do any more because we set the*/
195
                                /* disconnect mode to AUTO. */
197
                                /* disconnect mode to AUTO. */
196
                        }
198
                        }
197
                        break;
199
                        break;
198
        }
200
        }
199
        return 0;
201
        return 0;
200
}
202
}
201
 
203
 
202
 
204
 
203
 
205
 
204
 
206
 
205
 
207
 
206
int main (int argc, char *argv[]) {
208
int main (int argc, char *argv[]) {
207
        int DeviceId=0;
209
        int DeviceId=0;
208
        unsigned char ch;
210
        unsigned char ch;
209
        unsigned char MainOutput, preset;
211
        unsigned char MainOutput, preset;
210
        double Voltage, Current, tinterval;
212
        double Voltage, Current, tinterval;
211
        char str[0xFF];
213
        char str[0xFF];
212
        if (InitCVIRTE (0, argv, 0) == 0)
214
        if (InitCVIRTE (0, argv, 0) == 0)
213
                return -1;      /* out of memory */
215
                return -1;      /* out of memory */
214
        SetStdioPort (CVI_STDIO_WINDOW);
216
        SetStdioPort (CVI_STDIO_WINDOW);
215
        pid.iGain= 0.1;
217
        pid.iGain= 0.1;
216
        pid.dGain= 2;
218
        pid.dGain= 2;
217
        pid.pGain = 5;
219
        pid.pGain = 5;
218
        pid.iMax =100;
220
        pid.iMax =100;
219
        pid.iMin =-100 ;
221
        pid.iMin =-100 ;
220
        pid.iState=0;
222
        pid.iState=0;
221
        pid.dState=0;
223
        pid.dState=0;
222
 
224
 
223
        if ((pa = LoadPanel (0, "TempCtrl.uir", PA)) < 0)
225
        if ((pa = LoadPanel (0, "TempCtrl.uir", PA)) < 0)
224
                return -1;
226
                return -1;
225
        DisableBreakOnLibraryErrors();
227
        DisableBreakOnLibraryErrors();
226
        if (TMI_Open()== 0) MessagePopup("Error","Cannot open USB device");
228
        if (TMI_Open()== 0) MessagePopup("Error","Cannot open USB device");
227
        DeviceId = TMI_OpenHandle ("PW-A","USB:1:1");
229
        DeviceId = TMI_OpenHandle ("PW-A","USB:1:1");
228
        if (TMI_Test() == 0 )MessagePopup("DLL error","Dll Error");
230
        if (TMI_Test() == 0 )MessagePopup("DLL error","Dll Error");
229
        if (DeviceId < 0) MessagePopup("Error","Not Connected");
231
        if (DeviceId < 0) MessagePopup("Error","Not Connected");
230
        printf("TMI device ID %d\n",TMI_DeviceId);
232
        printf("TMI device ID %d\n",TMI_DeviceId);
231
 
233
 
232
        TMI_MainOutputQ(TMI_DeviceId, &MainOutput);
234
        TMI_MainOutputQ(TMI_DeviceId, &MainOutput);
233
        TMI_PresetQ(TMI_DeviceId, &preset);
235
        TMI_PresetQ(TMI_DeviceId, &preset);
234
        SetCtrlVal(pa, PA_ONOFF, MainOutput);
236
        SetCtrlVal(pa, PA_ONOFF, MainOutput);
235
        SetCtrlVal(pa, PA_PRESET, preset);
237
        SetCtrlVal(pa, PA_PRESET, preset);
236
        /*
238
        /*
237
        GetCtrlVal(pa, P1_TINTERVAL, &tinterval);
239
        GetCtrlVal(pa, P1_TINTERVAL, &tinterval);
238
        SetCtrlAttribute (pa, P1_TIMER, ATTR_INTERVAL, tinterval);
240
        SetCtrlAttribute (pa, P1_TIMER, ATTR_INTERVAL, tinterval);
239
        */
241
        */
240
 
242
 
241
        SetWaitCursor (1);
243
        SetWaitCursor (1);
242
        int portNum = 10000;
244
        int portNum = 10000;
243
        int              registered = 0;
245
        int              registered = 0;
244
        char     tempBuf[256] = {0};
246
        char     tempBuf[256] = {0};
245
        if (RegisterTCPServer (portNum, ServerTCPCB, 0) < 0)
247
        if (RegisterTCPServer (portNum, ServerTCPCB, 0) < 0)
246
                MessagePopup("TCP Server", "Server registration failed!");
248
                MessagePopup("TCP Server", "Server registration failed!");
247
        else {
249
        else {
248
                SetWaitCursor (0);
250
                SetWaitCursor (0);
249
                registered = 1;
251
                registered = 1;
250
 
252
 
251
                /* We are successfully connected -- gather info */
253
                /* We are successfully connected -- gather info */
252
 
254
 
253
                if (GetTCPHostAddr (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf);
255
                if (GetTCPHostAddr (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf);
254
                if (GetTCPHostName (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf);
256
                if (GetTCPHostName (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf);
255
 
257
 
256
 
258
 
257
 
259
 
258
        }
260
        }
259
 
261
 
260
        SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
262
        SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
261
        ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
263
        ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
262
 
264
 
263
 
265
 
264
 
266
 
265
        DisplayPanel (pa);
267
        DisplayPanel (pa);
266
        SetDimming(0);
268
        SetDimming(0);
267
        RunUserInterface ();
269
        RunUserInterface ();
268
        CloseCom(COM_PORT);
270
        CloseCom(COM_PORT);
269
        if (registered)
271
        if (registered)
270
                UnregisterTCPServer (portNum);
272
                UnregisterTCPServer (portNum);
271
        DiscardPanel (pa);
273
        DiscardPanel (pa);
272
        TMI_Close();
274
        TMI_Close();
273
        if (gFp)  fclose(gFp);
275
        if (gFp)  fclose(gFp);
274
        return 0;
276
        return 0;
275
}
277
}
276
 
278
 
277
 
279
 
278
int CVICALLBACK ExitCB (int panel, int control, int event,
280
int CVICALLBACK ExitCB (int panel, int control, int event,
279
                                                                                                void *callbackData, int eventData1, int eventData2) {
281
                                                                                                void *callbackData, int eventData1, int eventData2) {
280
        switch (event) {
282
        switch (event) {
281
                case EVENT_COMMIT:
283
                case EVENT_COMMIT:
282
                        QuitUserInterface (0);
284
                        QuitUserInterface (0);
-
 
285
                        break;
-
 
286
        }
-
 
287
        return 0;
-
 
288
}
-
 
289
 
-
 
290
int CVICALLBACK SwitchCoolingOnOffCB (int panel, int control, int event,
-
 
291
                                                                                                                         void *callbackData, int eventData1, int eventData2) {
-
 
292
        int onoff=0;
-
 
293
 
-
 
294
        switch (event) {
-
 
295
                case EVENT_COMMIT:
-
 
296
                        GetCtrlVal(pa,control,&onoff);
-
 
297
                        SetCooling(onoff);
-
 
298
                       
283
                        break;
299
                        break;
284
        }
300
        }
285
        return 0;
301
        return 0;
286
}
302
}
287
 
-
 
288
 
-
 
289
 
303
 
290
int CVICALLBACK SwitchOnOffCB (int panel, int control, int event,
304
int CVICALLBACK SwitchOnOffCB (int panel, int control, int event,
291
                                                                                                                         void *callbackData, int eventData1, int eventData2) {
305
                                                                                                                         void *callbackData, int eventData1, int eventData2) {
292
        unsigned char state;
306
        unsigned char state;
293
        switch (event) {
307
        switch (event) {
294
                case EVENT_COMMIT:
308
                case EVENT_COMMIT:
295
                        GetCtrlVal(panel, control, &state);
309
                        GetCtrlVal(panel, control, &state);
296
                        TMI_MainOutput(TMI_DeviceId, state);
310
                        TMI_MainOutput(TMI_DeviceId, state);
297
                        break;
311
                        break;
298
        }
312
        }
299
        return 0;
313
        return 0;
300
}
314
}
301
 
315
 
302
 
316
 
303
 
317
 
304
/* Callback Function */
318
/* Callback Function */
305
void ComCallback(int portNumber, int eventMask, void *callbackdata) {
319
void ComCallback(int portNumber, int eventMask, void *callbackdata) {
306
        static double told=0;
320
        static double told=0;
307
        if (eventMask & LWRS_RXFLAG) {
321
        if (eventMask & LWRS_RXFLAG) {
308
//printf("Received specified character\n");
322
//printf("Received specified character\n");
309
                int strLen = GetInQLen (COM_PORT);
323
                int strLen = GetInQLen (COM_PORT);
310
                bytes_read = ComRd (COM_PORT, read_data, strLen);
324
                bytes_read = ComRd (COM_PORT, read_data, strLen);
311
                double temp= atof(read_data);
325
                double temp= atof(read_data);
312
                double f[10];
326
                double f[10];
313
                int debug1;
327
                int debug1;
314
                GetCtrlVal(pa,PA_DEBUG_1, &debug1);
328
                GetCtrlVal(pa,PA_DEBUG_1, &debug1);
315
//printf("%f#%s#", temp, read_data);
329
//printf("%f#%s#", temp, read_data);
-
 
330
                // temp1 , temp2 , temp3 , temp4 , temp humidity sens, rel.humidity
-
 
331
                if (bytes_read >0) {
-
 
332
                        SetCtrlVal(pa,PA_ARDUINO, read_data);
-
 
333
                }      
316
                sscanf(read_data,"%lf %lf %lf %lf %lf %lf",&f[0],&f[1],&f[2],&f[3],&f[4],&f[5]);
334
                sscanf(read_data,"%lf %lf %lf %lf %lf %lf",&f[0],&f[1],&f[2],&f[3],&f[4],&f[5]);
317
                double humidity = f[5];
335
                double humidity = f[5];
-
 
336
                double htemp = f[4];
318
//              printf("%lf %lf %lf %lf %lf %lf",f[0],f[1],f[2],f[3],f[4],f[5]);
337
//              printf("%lf %lf %lf %lf %lf %lf",f[0],f[1],f[2],f[3],f[4],f[5]);
319
                int RS232Error = ReturnRS232Err ();
338
                int RS232Error = ReturnRS232Err ();
320
                if (ReturnRS232Err ()) {
339
                if (ReturnRS232Err ()) {
321
                        sprintf(read_data,"#%s\n", GetRS232ErrorString(RS232Error));
340
                        sprintf(read_data,"#%s\n", GetRS232ErrorString(RS232Error));
322
                        MessagePopup("RS232Err",read_data);
341
                        MessagePopup("RS232Err",read_data);
323
                } else {
342
                } else {
324
 
343
 
325
 
344
 
326
                        int polar;
345
                        int polar;
327
                        SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
346
                        SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
328
                        ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
347
                        ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
329
                        double tset,vmon,imon,vset,iset,vmax;
348
                        double tset,vmon,imon,vset,iset,vmax;
330
                        unsigned char ch, preset;
349
                        unsigned char ch, preset;
331
                        GetCtrlVal(pa,PA_TSET,&tset);
350
                        GetCtrlVal(pa,PA_TSET,&tset);
332
                        GetCtrlVal(pa,PA_VMON,&vmon);
351
                        GetCtrlVal(pa,PA_VMON,&vmon);
333
                        GetCtrlVal(pa,PA_IMON,&imon);
352
                        GetCtrlVal(pa,PA_IMON,&imon);
334
                        GetCtrlVal(pa,PA_CHANNEL, &ch);
353
                        GetCtrlVal(pa,PA_CHANNEL, &ch);
335
                        GetCtrlVal(pa,PA_PRESET,&preset);
354
                        GetCtrlVal(pa,PA_PRESET,&preset);
336
                        TMI_Preset(TMI_DeviceId,preset);
355
                        TMI_Preset(TMI_DeviceId,preset);
337
                        GetCtrlVal(pa,PA_POLAR,&polar);
356
                        GetCtrlVal(pa,PA_POLAR,&polar);
338
 
357
 
339
                        double tdiff = temp - tset;
358
                        double tdiff = temp - tset;
340
                        double retpid  = UpdatePID(&pid, tdiff, temp);
359
                        double retpid  = UpdatePID(&pid, tdiff, temp);
341
                        const double troom = 20;
360
                        const double troom = htemp;
342
                        double Pheat= 0.2 * (temp - troom);
361
                        double Pheat= 0.2 * (temp - troom);
343
                        double Ptec = retpid  - Pheat*2;
362
                        double Ptec = retpid  - Pheat*2;
344
                        vset = (polar*Ptec>0) ? sqrt(fabs(Ptec)) : 0;
363
                        vset = (polar*Ptec>0) ? sqrt(fabs(Ptec)) : 0;
345
                        if (debug1) printf("%d PID tmon=%f tset=%f tdiff=%f vmon=%f imom=%f pid=%f Pheat=%f =>vset %f", ch, temp, tset, tdiff, vmon, imon,retpid ,Pheat, vset);
364
                        if (debug1) printf("%d PID tmon=%f tset=%f tdiff=%f vmon=%f imom=%f pid=%f Pheat=%f =>vset %f", ch, temp, tset, tdiff, vmon, imon,retpid ,Pheat, vset);
346
                        GetCtrlVal(pa,PA_VMAX,&vmax);
365
                        GetCtrlVal(pa,PA_VMAX,&vmax);
347
                        if (vset >vmax) vset=vmax;
366
                        if (vset >vmax) vset=vmax;
348
                        if (vset <0) vset =0;
367
                        if (vset <0) vset =0;
349
                        if (debug1) printf("vset --->%f \n",vset);
368
                        if (debug1) printf("vset --->%f \n",vset);
350
                        TMI_Voltage(TMI_DeviceId, ch+1, preset, vset);
369
                        TMI_Voltage(TMI_DeviceId, ch+1, preset, vset);
351
                        GetCtrlVal(pa,PA_IMAX,&iset);
370
                        GetCtrlVal(pa,PA_IMAX,&iset);
352
                        TMI_Current(TMI_DeviceId, ch+1, preset, iset);
371
                        TMI_Current(TMI_DeviceId, ch+1, preset, iset);
353
 
372
 
354
 
373
 
355
 
374
 
356
 
375
 
357
 
376
 
358
                        ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
377
                        ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0);
359
 
378
 
360
                        PlotStripChart (pa, PA_GRAPH, &temp, 1, 0, 0, VAL_DOUBLE);
379
                        PlotStripChart (pa, PA_GRAPH, &temp, 1, 0, 0, VAL_DOUBLE);
361
                        double pgraph[3]= {vmon,imon, 100*(temp-told)};
380
                        double pgraph[3]= {vmon,imon, 100*(temp-told)};
362
                        PlotStripChart (pa, PA_GRAPH_VMON, pgraph, 3, 0, 0, VAL_DOUBLE);
381
                        PlotStripChart (pa, PA_GRAPH_VMON, pgraph, 3, 0, 0, VAL_DOUBLE);
363
                        PlotStripChart (pa, PA_GRAPH_3, &humidity, 1, 0, 0, VAL_DOUBLE);
382
                        PlotStripChart (pa, PA_GRAPH_3, &humidity, 1, 0, 0, VAL_DOUBLE);
364
                        SetCtrlVal(pa,PA_TMON,temp);
383
                        SetCtrlVal(pa,PA_TMON,temp);
365
 
-
 
-
 
384
                        SetCtrlVal(pa,PA_HTMON,htemp);  
366
                        SetCtrlVal(pa,PA_HUMIDITY,humidity);
385
                        SetCtrlVal(pa,PA_HUMIDITY,humidity);
367
 
386
 
368
                        char transmitBuf[512]= {0};
387
                        char transmitBuf[512]= {0};
369
                        sprintf(transmitBuf, "%u %f %f %f %f\n", time(NULL), humidity, temp, tdiff, temp-told);
388
                        sprintf(transmitBuf, "%u %f %f %f %f\n", time(NULL), humidity, temp, tdiff, temp-told);
370
                        if (g_hconversation)
389
                        if (g_hconversation)
371
                                if ( ServerTCPWrite (g_hconversation, transmitBuf, strlen (transmitBuf), 1000) < 0) printf("Transmit Error\n");
390
                                if ( ServerTCPWrite (g_hconversation, transmitBuf, strlen (transmitBuf), 1000) < 0) printf("Transmit Error\n");
372
                        int log=0;
391
                        int log=0;
373
                        GetCtrlVal(pa,PA_LOG, &log);
392
                        GetCtrlVal(pa,PA_LOG, &log);
374
                        if (log) {
393
                        if (log) {
375
                                char fname[0xFF];
394
                                char fname[0xFF];
376
                                GetCtrlVal(pa,PA_FNAME, fname);
395
                                GetCtrlVal(pa,PA_FNAME, fname);
377
                                FILE *fp = fopen (fname,"a");
396
                                FILE *fp = fopen (fname,"a");
378
                                fprintf(fp,transmitBuf);
397
                                fprintf(fp,transmitBuf);
379
                                fclose(fp);
398
                                fclose(fp);
380
 
399
 
381
                        }
400
                        }
382
                        told= temp;
401
                        told= temp;
383
 
402
 
384
                }
403
                }
385
        }
404
        }
386
 
405
 
387
        if (eventMask & LWRS_TXEMPTY)
406
        if (eventMask & LWRS_TXEMPTY)
388
 
407
 
389
                printf("Transmit queue now empty\n");
408
                printf("Transmit queue now empty\n");
390
 
409
 
391
        if (eventMask & LWRS_RECEIVE) {
410
        if (eventMask & LWRS_RECEIVE) {
392
 
411
 
393
                printf("50 or more bytes in input queue\n");
412
                printf("50 or more bytes in input queue\n");
394
 
413
 
395
        }
414
        }
396
 
415
 
397
}
416
}
398
 
417
 
399
 
418
 
400
int CVICALLBACK StartCB (int panel, int control, int event,
419
int CVICALLBACK StartCB (int panel, int control, int event,
401
                                                                                                 void *callbackData, int eventData1, int eventData2) {
420
                                                                                                 void *callbackData, int eventData1, int eventData2) {
402
        switch (event) {
421
        switch (event) {
403
                case EVENT_COMMIT:
422
                case EVENT_COMMIT:
404
                        OpenComConfig (COM_PORT, "", 115200, 0, 8, 1, 512, 512);
423
                        OpenComConfig (COM_PORT, "", 115200, 0, 8, 1, 512, 512);
405
                        /*      Turn off Hardware handshaking (loopback test will not function with it on) */
424
                        /*      Turn off Hardware handshaking (loopback test will not function with it on) */
406
                        SetCTSMode (COM_PORT, LWRS_HWHANDSHAKE_OFF);
425
                        SetCTSMode (COM_PORT, LWRS_HWHANDSHAKE_OFF);
407
 
426
 
408
                        /*      Make sure Serial buffers are empty */
427
                        /*      Make sure Serial buffers are empty */
409
                        FlushInQ (COM_PORT);
428
                        FlushInQ (COM_PORT);
410
                        FlushOutQ (COM_PORT);
429
                        FlushOutQ (COM_PORT);
411
 
430
 
412
                        int notifyCount = 50; /* Wait for at least 50 bytes in queue. */
431
                        int notifyCount = 50; /* Wait for at least 50 bytes in queue. */
413
                        int eventChar = 10; /* Wait for LF. */
432
                        int eventChar = 10; /* Wait for LF. */
414
                        int eventMask = LWRS_RXFLAG | LWRS_TXEMPTY | LWRS_RECEIVE;
433
                        int eventMask = LWRS_RXFLAG | LWRS_TXEMPTY | LWRS_RECEIVE;
415
                        InstallComCallback (COM_PORT, eventMask, notifyCount, eventChar, ComCallback, NULL);
434
                        InstallComCallback (COM_PORT, eventMask, notifyCount, eventChar, ComCallback, NULL);
416
                        SetDimming(1);
435
                        SetDimming(1);
417
 
436
 
418
                        break;
437
                        break;
419
        }
438
        }
420
        return 0;
439
        return 0;
421
}
440
}
422
 
441
 
423
 
442
 
424
int CVICALLBACK StopCB (int panel, int control, int event,
443
int CVICALLBACK StopCB (int panel, int control, int event,
425
                                                                                                void *callbackData, int eventData1, int eventData2) {
444
                                                                                                void *callbackData, int eventData1, int eventData2) {
426
        switch (event) {
445
        switch (event) {
427
                case EVENT_COMMIT:
446
                case EVENT_COMMIT:
428
                        CloseCom(COM_PORT);
447
                        CloseCom(COM_PORT);
429
                        SetDimming(0);
448
                        SetDimming(0);
430
                        break;
449
                        break;
431
        }
450
        }
432
        return 0;
451
        return 0;
433
}
452
}
434
 
453
 
435
int CVICALLBACK SetPresetCB (int panel, int control, int event,
454
int CVICALLBACK SetPresetCB (int panel, int control, int event,
436
                                                                                                                 void *callbackData, int eventData1, int eventData2) {
455
                                                                                                                 void *callbackData, int eventData1, int eventData2) {
437
 
456
 
438
        switch (event) {
457
        switch (event) {
439
                case EVENT_COMMIT: {
458
                case EVENT_COMMIT: {
440
                        unsigned char preset;
459
                        unsigned char preset;
441
                        double Voltage, Current;
460
                        double Voltage, Current;
442
                        GetCtrlVal(panel, control, &preset);
461
                        GetCtrlVal(panel, control, &preset);
443
                        TMI_Preset(TMI_DeviceId, preset);
462
                        TMI_Preset(TMI_DeviceId, preset);
444
 
463
 
445
                        break;
464
                        break;
446
                }
465
                }
447
        }
466
        }
448
        return 0;
467
        return 0;
449
}
468
}
450
 
469
 
451
 
470