Rev 310 | Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 308 | f9daq | 1 | #include <userint.h> |
| 2 | #include "K2231A-ctrl.h" |
||
| 3 | #include <stdlib.h> |
||
| 4 | #include <stdio.h> |
||
| 5 | #include <windows.h> |
||
| 6 | #include <visa.h> |
||
| 7 | //#include <K2231A.h> |
||
| 8 | |||
| 9 | |||
| 10 | static ViStatus istat; |
||
| 11 | static ViSession RedPHandle,RMHandle; |
||
| 12 | int K2231A_DeviceId=0; |
||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | int K2231A_Initialize() |
||
| 19 | { |
||
| 20 | |||
| 21 | return 0; |
||
| 22 | } |
||
| 23 | |||
| 24 | |||
| 25 | int K2231A_Test() |
||
| 26 | { |
||
| 27 | char str[0xFF]; |
||
| 28 | printf("K2231A_ModelNameQ %s\n",str); |
||
| 29 | return TRUE; |
||
| 30 | } |
||
| 31 | |||
| 32 | int K2231A_Close() |
||
| 33 | { |
||
| 34 | |||
| 35 | |||
| 36 | return 0; |
||
| 37 | } |
||
| 38 | |||
| 39 | int K2231A_Open() |
||
| 40 | { |
||
| 41 | K2231A_Initialize(); |
||
| 42 | return TRUE; |
||
| 43 | } |
||
| 44 | |||
| 45 | int K2231A_OpenHandle(char * dev, char * usb ){ |
||
| 46 | |||
| 47 | K2231A_DeviceId = 0; |
||
| 48 | return K2231A_DeviceId; |
||
| 49 | } |
||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | /****************TOKOVI (potek, kako se spreminjajo tokovi s casom)****************/ |
||
| 55 | |||
| 56 | void K2231A_ReadCurrent(double * I , double * V, unsigned char * mode, unsigned char *onoff){ |
||
| 57 | int iRet; |
||
| 58 | char ch=0; |
||
| 59 | double Voltage; |
||
| 60 | double Current; |
||
| 61 | char cv_cc; |
||
| 62 | |||
| 63 | |||
| 64 | //iRet = K2231A_TimeOut(K2231A_DeviceId, 1); |
||
| 65 | //iRet = K2231A_Refresh(K2231A_DeviceId); |
||
| 66 | |||
| 67 | |||
| 68 | //iRet = K2231A_MainOutput(K2231A_DeviceId, Output); |
||
| 69 | //iRet = K2231A_MainOutputQ(K2231A_DeviceId, &Output); |
||
| 70 | for (ch=0;ch<3;ch++){ |
||
| 71 | |||
| 72 | |||
| 73 | //iRet = K2231A_MoniDataQ(K2231A_DeviceId, ch+1, &Voltage, &Current, &cv_cc); |
||
| 74 | //if (debug) printf("[%d] %g V %g A CVCC %d\t",ch, Voltage, Current, cv_cc ); |
||
| 75 | I[ch]= Current; //s tem definiras kaj sta I in V. |
||
| 76 | V[ch]= Voltage; |
||
| 77 | |||
| 78 | |||
| 79 | } |
||
| 80 | //if (debug) printf("\n"); |
||
| 81 | |||
| 82 | |||
| 83 | return; |
||
| 84 | |||
| 85 | |||
| 86 | } |
||
| 87 | |||
| 88 | |||
| 89 | #ifdef MAIN |
||
| 90 | |||
| 91 | #include <ansi_c.h> |
||
| 92 | #include <utility.h> |
||
| 93 | |||
| 94 | |||
| 95 | #define RSTREG(a,x) (a&=(~(x))) |
||
| 96 | #define SETREG(a,x) (a|=x) |
||
| 97 | |||
| 98 | int gLogToFile; |
||
| 99 | int vmon[3]={P1_VMON_1,P1_VMON_2,P1_VMON_3 }; |
||
| 100 | int imon[3]={P1_IMON_1,P1_IMON_2,P1_IMON_3 }; |
||
| 101 | int vset[3]={P1_U_1,P1_U_2,P1_U_3 }; |
||
| 102 | int iset[3]={P1_I_1,P1_I_2,P1_I_3 }; |
||
| 103 | int radiob[3]={P1_BOX_1,P1_BOX_2,P1_BOX_3 }; |
||
| 104 | |||
| 105 | int cvcc[3]={P1_CVCC_1,P1_CVCC_2,P1_CVCC_3 }; |
||
| 106 | |||
| 107 | int gMask=0xF; |
||
| 108 | |||
| 109 | int pnl; |
||
| 110 | FILE *gFp; |
||
| 111 | int main (int argc, char *argv[]) |
||
| 112 | { |
||
| 113 | int DeviceId=0; |
||
| 114 | unsigned char MainOutput, preset; |
||
| 115 | double Voltage, Current, tinterval; |
||
| 116 | char str[0xFF]; |
||
| 117 | if (InitCVIRTE (0, argv, 0) == 0) |
||
| 118 | return -1; /* out of memory */ |
||
| 119 | SetStdioPort (CVI_STDIO_WINDOW); |
||
| 120 | SetStdioWindowOptions (1000, 0, 0); |
||
| 121 | SetStdioWindowSize (150, 600); |
||
| 122 | SetStdioWindowPosition (825, 20); |
||
| 123 | |||
| 124 | istat = viOpenDefaultRM (&RMHandle); |
||
| 125 | if (RMHandle) istat = viOpen (RMHandle, "TCPIP0::178.172.43.91::5000::SOCKET", VI_NULL, VI_NULL, &RedPHandle); |
||
| 126 | if (RedPHandle){ |
||
| 127 | istat = viSetAttribute (RedPHandle, VI_ATTR_TCPIP_KEEPALIVE, VI_TRUE); |
||
| 128 | istat = viSetAttribute (RedPHandle, VI_ATTR_TCPIP_NODELAY, VI_TRUE); |
||
| 129 | istat = viSetAttribute (RedPHandle, VI_ATTR_TERMCHAR, '\n'); |
||
| 130 | istat = viSetAttribute (RedPHandle, VI_ATTR_TERMCHAR_EN, VI_TRUE); |
||
| 131 | istat = viSetAttribute (RedPHandle, VI_ATTR_TMO_VALUE, 1000); |
||
| 132 | } |
||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | if ((pnl = LoadPanel (0, "K2231A-ctrl.uir", P1)) < 0) return -1; |
||
| 138 | |||
| 139 | if (K2231A_Open()== 0) MessagePopup("Error","Cannot open USB device"); |
||
| 140 | //DeviceId = K2231A_OpenHandle ("PW-A","USB:1:1"); |
||
| 141 | if (K2231A_Test() == 0 )MessagePopup("DLL error","Dll Error"); |
||
| 142 | if (DeviceId < 0) MessagePopup("Error","Not Connected"); |
||
| 143 | printf("TMI device ID %d\n",K2231A_DeviceId); |
||
| 144 | |||
| 145 | //K2231A_MainOutputQ(K2231A_DeviceId, &MainOutput); |
||
| 146 | SetCtrlVal(pnl, P1_ONOFF, MainOutput); |
||
| 147 | GetCtrlVal(pnl, P1_TINTERVAL, &tinterval); |
||
| 148 | SetCtrlAttribute (pnl, P1_TIMER, ATTR_INTERVAL, tinterval); |
||
| 149 | //K2231A_PresetQ(K2231A_DeviceId, &preset); |
||
| 150 | SetCtrlVal(pnl, P1_PRESET, preset); |
||
| 151 | SetTraceAttributeEx (pnl, P1_GRAPH_IMON, 1, ATTR_TRACE_LG_TEXT, "CH 30V"); |
||
| 152 | SetTraceAttributeEx (pnl, P1_GRAPH_IMON, 2, ATTR_TRACE_LG_TEXT, "CH 30V"); |
||
| 153 | SetTraceAttributeEx (pnl, P1_GRAPH_IMON, 3, ATTR_TRACE_LG_TEXT, "CH 5V"); |
||
| 154 | |||
| 155 | SetTraceAttributeEx (pnl, P1_GRAPH_VMON, 1, ATTR_TRACE_LG_TEXT, "CH 30V"); |
||
| 156 | SetTraceAttributeEx (pnl, P1_GRAPH_VMON, 2, ATTR_TRACE_LG_TEXT, "CH 30V"); |
||
| 157 | SetTraceAttributeEx (pnl, P1_GRAPH_VMON, 3, ATTR_TRACE_LG_TEXT, "CH 5V"); |
||
| 158 | for (unsigned char ch=0;ch<4;ch++){ |
||
| 159 | int ison; |
||
| 160 | //K2231A_VoltageQ(K2231A_DeviceId, ch+1, preset, &Voltage); |
||
| 161 | //K2231A_CurrentQ(K2231A_DeviceId, ch+1, preset, &Current); |
||
| 162 | SetCtrlVal(pnl, vset[ch], Voltage); |
||
| 163 | SetCtrlVal(pnl, iset[ch], Current); |
||
| 164 | GetCtrlVal(pnl, radiob[ch], &ison); |
||
| 165 | if (ison) SETREG(gMask,(1<<ch)); else RSTREG(gMask,(1<<ch)); |
||
| 166 | } |
||
| 167 | |||
| 168 | DisplayPanel (pnl); |
||
| 169 | RunUserInterface (); |
||
| 170 | DiscardPanel (pnl); |
||
| 171 | K2231A_Close(); |
||
| 172 | |||
| 173 | if (gFp) fclose(gFp); |
||
| 174 | return 0; |
||
| 175 | } |
||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | int CVICALLBACK SwitchOnOffCB (int panel, int control, int event, |
||
| 181 | void *callbackData, int eventData1, int eventData2) |
||
| 182 | { |
||
| 183 | unsigned char state; |
||
| 184 | switch (event) |
||
| 185 | { |
||
| 186 | case EVENT_COMMIT: |
||
| 187 | GetCtrlVal(panel, control, &state); |
||
| 188 | //K2231A_MainOutput(K2231A_DeviceId, state); |
||
| 189 | break; |
||
| 190 | } |
||
| 191 | return 0; |
||
| 192 | } |
||
| 193 | |||
| 194 | int CVICALLBACK SetCB (int panel, int control, int event, |
||
| 195 | void *callbackData, int eventData1, int eventData2) |
||
| 196 | { |
||
| 197 | |||
| 198 | |||
| 199 | switch (event) |
||
| 200 | { |
||
| 201 | case EVENT_COMMIT:{ |
||
| 202 | unsigned char preset; |
||
| 203 | //K2231A_PresetQ(K2231A_DeviceId, &preset); |
||
| 204 | SetCtrlVal(pnl, P1_PRESET, preset); |
||
| 205 | for (unsigned char ch = 0; ch<3;ch++){ |
||
| 206 | //printf("ch %d %x\n", ch, gMask); |
||
| 207 | if (gMask & (1<<ch)){ |
||
| 208 | double Voltage; |
||
| 209 | double Current; |
||
| 210 | |||
| 211 | GetCtrlVal(panel, vset[ch], &Voltage); |
||
| 212 | GetCtrlVal(panel, iset[ch], &Current); |
||
| 213 | //printf("->ch %d VSet %g Iset %g\n", ch, Voltage, Current); |
||
| 214 | //K2231A_Voltage(K2231A_DeviceId, ch+1, preset, Voltage); |
||
| 215 | //K2231A_VoltageQ(K2231A_DeviceId, ch+1, preset, &Voltage); |
||
| 216 | //K2231A_Current(K2231A_DeviceId, ch+1, preset, Current); |
||
| 217 | //K2231A_CurrentQ(K2231A_DeviceId, ch+1, preset, &Current); |
||
| 218 | //printf("<-ch %d VSet %g Iset %g\n", ch, Voltage, Current); |
||
| 219 | |||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | break; |
||
| 224 | } |
||
| 225 | return 0; |
||
| 226 | } |
||
| 227 | |||
| 228 | int CVICALLBACK ReadCB (int panel, int control, int event, |
||
| 229 | void *callbackData, int eventData1, int eventData2) |
||
| 230 | { |
||
| 231 | |||
| 232 | int iRet; |
||
| 233 | char ch=0; |
||
| 234 | double Voltage; |
||
| 235 | double Current; |
||
| 236 | char cv_cc; |
||
| 237 | switch (event) |
||
| 238 | { |
||
| 239 | case EVENT_COMMIT: |
||
| 240 | for (ch = 0; ch<3;ch++){ |
||
| 241 | if (gMask & (1<<ch)){ |
||
| 242 | //iRet = K2231A_TimeOut(K2231A_DeviceId, 1); |
||
| 243 | //iRet = K2231A_Refresh(K2231A_DeviceId); |
||
| 244 | //iRet = K2231A_MoniDataQ(K2231A_DeviceId, ch+1, &Voltage, &Current, &cv_cc); |
||
| 245 | //printf("ch %d VSet %g Iset %g\n", ch, Voltage, Current); |
||
| 246 | SetCtrlVal(panel, vmon[ch], Voltage); |
||
| 247 | SetCtrlVal(panel, imon[ch], Current); |
||
| 248 | SetCtrlVal(panel, cvcc[ch], cv_cc); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | break; |
||
| 252 | } |
||
| 253 | return 0; |
||
| 254 | } |
||
| 255 | |||
| 256 | int CVICALLBACK SetIntervalCB (int panel, int control, int event, |
||
| 257 | void *callbackData, int eventData1, int eventData2) { |
||
| 258 | double tinterval; |
||
| 259 | switch (event) { |
||
| 260 | case EVENT_COMMIT: |
||
| 261 | GetCtrlVal(panel, control, &tinterval); |
||
| 262 | SetCtrlAttribute (panel, P1_TIMER, ATTR_INTERVAL, tinterval); |
||
| 263 | break; |
||
| 264 | } |
||
| 265 | return 0; |
||
| 266 | } |
||
| 267 | |||
| 268 | int CVICALLBACK TimerOnOffCB (int panel, int control, int event, |
||
| 269 | void *callbackData, int eventData1, int eventData2) |
||
| 270 | { |
||
| 271 | int state; |
||
| 272 | switch (event) |
||
| 273 | { |
||
| 274 | case EVENT_COMMIT: |
||
| 275 | GetCtrlVal(panel, control, &state); |
||
| 276 | if (state){ |
||
| 277 | ResumeTimerCallbacks(); |
||
| 278 | } else { |
||
| 279 | SuspendTimerCallbacks (); |
||
| 280 | printf ("Disabling timer....\n"); |
||
| 281 | } |
||
| 282 | break; |
||
| 283 | } |
||
| 284 | return 0; |
||
| 285 | } |
||
| 286 | |||
| 287 | int CVICALLBACK ExitCB (int panel, int control, int event, |
||
| 288 | void *callbackData, int eventData1, int eventData2) |
||
| 289 | { |
||
| 290 | switch (event) |
||
| 291 | { |
||
| 292 | case EVENT_COMMIT: |
||
| 293 | QuitUserInterface(0); |
||
| 294 | break; |
||
| 295 | } |
||
| 296 | return 0; |
||
| 297 | } |
||
| 298 | |||
| 299 | int CVICALLBACK SetMaskCB (int panel, int control, int event, |
||
| 300 | void *callbackData, int eventData1, int eventData2) |
||
| 301 | { int ison; |
||
| 302 | switch (event) |
||
| 303 | { |
||
| 304 | int i=0; |
||
| 305 | case EVENT_COMMIT: |
||
| 306 | GetCtrlVal(panel, control, &ison); |
||
| 307 | for (i=0;i<3;i++) if (control == radiob[i]) break; |
||
| 308 | if (ison) SETREG(gMask,(1<<i)); else RSTREG(gMask,(1<<i)); |
||
| 309 | break; |
||
| 310 | } |
||
| 311 | return 0; |
||
| 312 | } |
||
| 313 | |||
| 314 | int CVICALLBACK LogToFileCB (int panel, int control, int event, |
||
| 315 | void *callbackData, int eventData1, int eventData2) |
||
| 316 | { |
||
| 317 | |||
| 318 | switch (event) |
||
| 319 | { |
||
| 320 | case EVENT_COMMIT: |
||
| 321 | GetCtrlVal(panel, control, &gLogToFile); |
||
| 322 | |||
| 323 | break; |
||
| 324 | } |
||
| 325 | return 0; |
||
| 326 | } |
||
| 327 | |||
| 328 | int CVICALLBACK TimerCB (int panel, int control, int event, |
||
| 329 | void *callbackData, int eventData1, int eventData2) |
||
| 330 | { |
||
| 331 | switch (event) |
||
| 332 | { |
||
| 333 | |||
| 334 | unsigned char ch; |
||
| 335 | double current[3]; |
||
| 336 | double voltage[3]; |
||
| 337 | char cv_cc; |
||
| 338 | case EVENT_TIMER_TICK: |
||
| 339 | for (ch = 0; ch<3;ch++){ |
||
| 340 | if (gMask & (1<<ch)){ |
||
| 341 | //K2231A_TimeOut(K2231A_DeviceId, 1); |
||
| 342 | //K2231A_Refresh(K2231A_DeviceId); |
||
| 343 | //K2231A_MoniDataQ(K2231A_DeviceId, ch+1, &voltage[ch], ¤t[ch], &cv_cc); |
||
| 344 | SetCtrlVal(panel, vmon[ch], voltage[ch]); |
||
| 345 | SetCtrlVal(panel, imon[ch], current[ch]); |
||
| 346 | SetCtrlVal(panel, cvcc[ch], cv_cc); |
||
| 347 | if (gLogToFile){ |
||
| 348 | if (!gFp) { |
||
| 349 | gFp= fopen("pw18-1.8aq.log","w"); |
||
| 350 | fprintf(gFp,"#time\tch\tU\tI\tCV_CC\n"); |
||
| 351 | } |
||
| 352 | fprintf(gFp,"%ul\t%d\t%g\t%g\t%d\n", time(NULL), ch+1, voltage[ch], current[ch], cv_cc); |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | PlotStripChart (panel, P1_GRAPH_IMON, current, 3, 0, 0, VAL_DOUBLE); |
||
| 357 | PlotStripChart (panel, P1_GRAPH_VMON, voltage, 3, 0, 0, VAL_DOUBLE); |
||
| 358 | break; |
||
| 359 | } |
||
| 360 | return 0; |
||
| 361 | } |
||
| 362 | |||
| 363 | int CVICALLBACK SetPresetCB (int panel, int control, int event, |
||
| 364 | void *callbackData, int eventData1, int eventData2) { |
||
| 365 | |||
| 366 | switch (event) { |
||
| 367 | case EVENT_COMMIT:{ |
||
| 368 | unsigned char preset; |
||
| 369 | double Voltage, Current; |
||
| 370 | GetCtrlVal(panel, control, &preset); |
||
| 371 | //K2231A_Preset(K2231A_DeviceId, preset); |
||
| 372 | |||
| 373 | for (unsigned char ch=0;ch<3;ch++){ |
||
| 374 | int ison; |
||
| 375 | //K2231A_VoltageQ(K2231A_DeviceId, ch+1, preset, &Voltage); |
||
| 376 | //K2231A_CurrentQ(K2231A_DeviceId, ch+1, preset, &Current); |
||
| 377 | SetCtrlVal(pnl, vset[ch], Voltage); |
||
| 378 | SetCtrlVal(pnl, iset[ch], Current); |
||
| 379 | GetCtrlVal(pnl, radiob[ch], &ison); |
||
| 380 | if (ison) SETREG(gMask,(1<<ch)); else RSTREG(gMask,(1<<ch)); |
||
| 381 | } |
||
| 382 | break; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | return 0; |
||
| 386 | } |
||
| 387 | |||
| 388 | |||
| 389 | #endif MAIN |
||
| 390 |