1,3 → 1,4 |
#include <tcpsupp.h> |
#include <formatio.h> |
#include <userint.h> |
#include <ansi_c.h> |
11,6 → 12,7 |
static int K6517_Interface,K6517_Port,K6517_Gdev; |
static int iret; |
static int stop; |
static int debugCode; |
int _VI_FUNC K6517_status_print (void); |
void GpibError(char *msg) { |
|
120,7 → 122,7 |
|
void _VI_FUNC K6517_send (char *cmd, int len) |
{ |
printf("-> %s\n", cmd); |
if (debugCode) printf("-> %s\n", cmd); |
if (!len) len = strlen(cmd); |
switch (K6517_Interface){ |
case 1: |
141,7 → 143,7 |
case 1: |
PROLOGIX_Send("++read eoi"); |
ierr= PROLOGIX_Receive (response,maxbyt); |
printf("<---Read [%d] %s\n", ierr, response); |
if (debugCode) printf("<---Read [%d] %s\n", ierr, response); |
return ierr; |
break; |
default: |
194,7 → 196,7 |
|
cres[0]= 0; |
int len = K6517_receive (cres, 50); |
printf("current >%d, %s\n",len,cres); |
if (debugCode) printf("current >%d, %s\n",len,cres); |
|
return atof(cres); |
/* |
269,6 → 271,8 |
char cmd[100]; |
len=sprintf(cmd,":SOURCE:VOLTAGE %f",value); |
K6517_send (cmd, len); |
len=sprintf(cmd,":SOURCE:VOLTAGE:MCONNECT ON"); |
K6517_send (cmd, len); |
return; |
} |
|
388,7 → 392,203 |
int pnl; |
FILE *gFp; |
|
static int tfID; |
static int rID; |
static CmtThreadPoolHandle poolHandle = 0; |
#define MAX_THREADS 10 |
|
|
|
/*---------------------------------------------------------------------------*/ |
/* Macros */ |
/*---------------------------------------------------------------------------*/ |
#define tcpChk(f) if ((g_TCPError=(f)) < 0) {ReportTCPError();} |
|
|
/*---------------------------------------------------------------------------*/ |
/* Internal function prototypes */ |
/*---------------------------------------------------------------------------*/ |
int CVICALLBACK ClientTCPCB (unsigned handle, int event, int error, |
void *callbackData); |
static void ReportTCPError (void); |
|
/*---------------------------------------------------------------------------*/ |
/* Module-globals */ |
/*---------------------------------------------------------------------------*/ |
static unsigned int g_hconversation; |
static int g_hmainPanel; |
static int g_connected = 0; |
static int g_TCPError = 0; |
|
/*---------------------------------------------------------------------------*/ |
/* Report TCP Errors if any */ |
/*---------------------------------------------------------------------------*/ |
static void ReportTCPError(void) |
{ |
if (g_TCPError < 0) |
{ |
char messageBuffer[1024]; |
sprintf(messageBuffer, |
"TCP library error message: %s\nSystem error message: %s", |
GetTCPErrorString (g_TCPError), GetTCPSystemErrorString()); |
MessagePopup ("Error", messageBuffer); |
g_TCPError = 0; |
} |
} |
/*---------------------------------------------------------------------------*/ |
|
|
int CVICALLBACK DisconnectCB (int panel, int control, int event, |
void *callbackData, int eventData1, int eventData2) { |
switch (event) { |
case EVENT_COMMIT: |
if (g_connected) |
DisconnectFromTCPServer (g_hconversation); |
g_hconversation = 0; |
g_connected = 0; |
break; |
} |
return 0; |
} |
|
int CVICALLBACK ConnectCB (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { |
int portNum=10000; |
char tempBuf[512]; |
sprintf(tempBuf,"localhost"); |
switch (event) { |
case EVENT_COMMIT: |
if (ConnectToTCPServer (&g_hconversation, portNum, tempBuf, ClientTCPCB, NULL, 5000) < 0) |
MessagePopup("TCP Client", "Connection to server failed !"); |
else |
{ |
SetWaitCursor (0); |
g_connected = 1; |
|
/* We are successfully connected -- gather info */ |
|
if (GetTCPHostAddr (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf); |
if (GetTCPHostName (tempBuf, 256) >= 0) printf("%s\n" ,tempBuf); |
|
tcpChk (GetTCPPeerAddr (g_hconversation, tempBuf, 256)); |
printf("%s\n" ,tempBuf); |
tcpChk (GetTCPPeerName (g_hconversation, tempBuf, 256)); |
printf("%s\n" ,tempBuf); |
} |
break; |
} |
return 0; |
} |
|
|
int CVICALLBACK ClientTCPCB (unsigned handle, int event, int error, void *callbackData) |
{ |
char receiveBuf[256] = {0}; |
ssize_t dataSize = sizeof (receiveBuf) - 1; |
|
switch (event) |
{ |
case TCP_DATAREADY: |
if ((dataSize = ClientTCPRead (g_hconversation, receiveBuf, |
dataSize, 1000)) |
< 0) |
{ |
printf( "Receive Error\n"); |
} |
else |
{ |
receiveBuf[dataSize] = '\0'; |
//printf("%s", receiveBuf); |
float temp = 0; |
float humidity = 0; |
float dt=0; |
float tdiff=0; |
int t0 = 0; |
sscanf(receiveBuf, "%d%f%f%f%f", &t0,&humidity,&temp, &tdiff, &dt); |
SetCtrlVal(pn2,P2_TMON, temp); |
SetCtrlVal(pn2,P2_HUMIDITY, humidity); |
if (fabs(tdiff)<0.5 && fabs(dt) < 0.2) SetCtrlVal(pn2,P2_LED, 1); |
else SetCtrlVal(pn2,P2_LED, 0); |
} |
break; |
case TCP_DISCONNECT: |
MessagePopup ("TCP Client", "Server has closed connection!"); |
|
g_connected = 0; |
|
break; |
} |
return 0; |
} |
|
|
void SetDimming(int state) { |
SetCtrlAttribute (pnl, P2_IVSCAN, ATTR_DIMMED, state); |
SetCtrlAttribute (pnl, P2_EXIT, ATTR_DIMMED, state); |
SetCtrlAttribute (pnl, P2_STOP, ATTR_DIMMED, !state); |
} |
|
void CVICALLBACK EndOfThread ( CmtThreadPoolHandle poolhandle, |
CmtThreadFunctionID functionID, unsigned int event, |
int value, void *callbackData ) { |
SetDimming(0); |
printf("End of Thread \n"); |
return ; |
|
} |
|
int ivscan (void *arg); |
|
int TScan () { |
|
double temp; |
int n=0; |
int led; |
GetNumTableRows (pn2, P2_TABLE, &n ); |
for(int i=0; i<n; i++) { |
GetTableCellVal (pn2, P2_TABLE, MakePoint (1,i+1), &temp); |
if (temp>100) break; |
char transmitBuf[512]= {0}; |
sprintf(transmitBuf, "1 %f\n", temp); |
SetCtrlVal(pn2,P2_LED, 0); |
if (g_hconversation) |
if ( ClientTCPWrite (g_hconversation, transmitBuf, strlen (transmitBuf), 1000) < 0) printf("Transmit Error\n"); |
|
do { |
time_t t0 = time(NULL); |
SetCtrlVal(pn2,P2_TIME, ctime(&t0)); |
Delay(1); |
GetCtrlVal(pn2,P2_LED, &led); |
if (stop) break; |
} while (!led); |
if (stop) break; |
|
int itemp = (int) temp; |
ivscan(&itemp); |
printf("%d %f\n",i,temp); |
} |
|
return 0; |
} |
|
|
int CVICALLBACK TScanCB (int panel, int control, int event, |
void *callbackData, int eventData1, int eventData2) { |
|
switch (event) { |
case EVENT_COMMIT:{ |
ThreadFunctionPtr mythread = TScan; |
CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &rID, |
DEFAULT_THREAD_PRIORITY, |
EndOfThread, |
EVENT_TP_THREAD_FUNCTION_END, |
NULL, RUN_IN_SCHEDULED_THREAD, |
&tfID); |
} |
break; |
} |
return 0; |
} |
|
int CVICALLBACK TimerOnOffCB (int panel, int control, int event, |
void *callbackData, int eventData1, int eventData2) |
{ |
438,6 → 638,7 |
return current_run+1; |
} |
|
|
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, |
LPSTR lpszCmdLine, int nCmdShow) |
{ |
444,10 → 645,13 |
char cres[100]; |
double Voltage, Current, tinterval; |
if (InitCVIRTE (hInstance, 0, 0) == 0) return -1; /* out of memory */ |
SetStdioPort (CVI_STDIO_WINDOW); |
SetStdioPort (CVI_STDIO_WINDOW); |
SetSleepPolicy(VAL_SLEEP_MORE); |
CmtNewThreadPool (MAX_THREADS, &poolHandle); |
|
if ((pnl = LoadPanel (0, "K6517-ctrl.uir", P1)) < 0) return -1; |
if ((pn2 = LoadPanel (0, "K6517-ctrl.uir", P2)) < 0) return -1; |
|
DisableBreakOnLibraryErrors(); |
K6517_open (1,4,3,0,13); |
|
|
468,6 → 672,9 |
DisplayPanel (pnl); |
DisplayPanel (pn2); |
RunUserInterface (); |
CmtDiscardThreadPool (poolHandle); |
if (g_connected) |
DisconnectFromTCPServer (g_hconversation); |
DiscardPanel (pn2); |
DiscardPanel (pnl); |
K6517_close(); |
660,8 → 867,8 |
} |
|
|
int CVICALLBACK IVSCANCB (int panel, int control, int event, |
void *callbackData, int eventData1, int eventData2) { |
int ivscan (void *arg) { |
int *iarg = (int *) arg; |
int nsteps; |
double umin, umax, u; |
double *xpoints; |
682,32 → 889,41 |
FILE *fp; |
time_t mtime; |
ssize_t size; |
|
switch (event) { |
case EVENT_COMMIT: |
int repeatscan = 1; |
while (repeatscan){ |
|
|
K6517_send (":OUTP ON", 0); |
// GetCtrlVal(panel, P2_UMIN, &umin); |
// GetCtrlVal(panel, P2_UMAX, &umax); |
// GetCtrlVal(panel, P2_NSTEPS, &nsteps); |
|
SetCtrlVal(pnl, P1_TIMERON, 0); |
GetCtrlVal(panel, P2_FILENAME, fileName); |
GetCtrlVal(panel, P2_PATH, path); |
// GetCtrlVal(panel, P2_RANGE, &range); |
GetCtrlVal(pn2, P2_FILENAME, fileName); |
GetCtrlVal(pn2, P2_PATH, path); |
|
TimerOnOffCB(pnl, P1_TIMERON, EVENT_COMMIT, NULL, 0, 0); |
sprintf(fname, "%s/%s.dat", path, fileName); |
if ( !GetFileInfo(fname,&size) ) |
GetCtrlVal(pn2, P2_REPEAT, &repeatscan); |
if (repeatscan) { |
int month, day, year, hours,minutes,seconds; |
GetSystemDate (&month,&day ,&year ); |
GetSystemTime(&hours, &minutes, &seconds); |
sprintf(fname ,"%s/%s.%d_%d_%d_%d_%d.dat", path, fileName,year,month,day,hours,minutes ); |
fp = fopen(fname,"w"); |
} else { |
if (*iarg>=100) sprintf(fname, "%s/%s.dat", path, fileName); |
else sprintf(fname, "%s/%s_T%d.dat", path, fileName,*iarg); |
if ( !GetFileInfo(fname,&size) || strstr("test",fileName)!=NULL ) |
fp = fopen(fname,"w"); |
else { |
sprintf(cres, "File %s exist\n Remove it first",fname); |
else { |
sprintf(cres, "File %s exist\n Remove it first",fname); |
MessagePopup ("Info", cres); |
return 0; |
} |
} |
} |
time(&mtime); |
printf("#%s %s\n",DateStr(), TimeStr()); |
if (fp) fprintf(fp, "#%s %s\n",DateStr(), TimeStr()); |
// K6517_current_mode (range); |
stop = 0; |
LogScaleCB (panel, P2_LOGSCALE, EVENT_COMMIT,NULL, 0, 0); |
LogScaleCB (pn2, P2_LOGSCALE, EVENT_COMMIT,NULL, 0, 0); |
int voltageMax[4]={P2_UMAX_1,P2_UMAX_2,P2_UMAX_3,P2_UMAX_4}; |
int voltageMin[4]={P2_UMIN_1,P2_UMIN_2,P2_UMIN_3,P2_UMIN_4}; |
int numberOfSteps[4]={P2_NSTEPS_1,P2_NSTEPS_2,P2_NSTEPS_3,P2_NSTEPS_4}; |
718,50 → 934,117 |
int N=0; |
int K=0; |
for (int j=0;j<4;j++){ |
GetCtrlVal(panel, intervals[j], &nrOfInt); |
GetCtrlVal(pn2, intervals[j], &nrOfInt); |
N = N + nrOfInt; |
GetCtrlVal(panel, numberOfSteps[j], &nrOfSteps); |
GetCtrlVal(pn2, numberOfSteps[j], &nrOfSteps); |
K = K + nrOfSteps; |
} |
xpoints = (double *) malloc ((K-N+2)*sizeof(double)); |
ypoints = (double *) malloc ((K-N+2)*sizeof(double)); |
xpoints = (double *) malloc ((K-N+2)*sizeof(double)*4); |
ypoints = (double *) malloc ((K-N+2)*sizeof(double)*4); |
int tockaK=0; |
double t1,t2; |
GetCtrlVal(pn2, P2_DELAY, &t1); |
GetCtrlVal(pn2, P2_DELAY2, &t2); |
|
for (int i=0;i<N;i++){ |
GetCtrlVal(panel, voltageMin[i], &umin); |
GetCtrlVal(panel, voltageMax[i], &umax); |
GetCtrlVal(panel, numberOfSteps[i], &nsteps); |
GetCtrlVal(panel, selectRange[i], &range); |
GetCtrlVal(pn2, voltageMin[i], &umin); |
GetCtrlVal(pn2, voltageMax[i], &umax); |
GetCtrlVal(pn2, numberOfSteps[i], &nsteps); |
GetCtrlVal(pn2, selectRange[i], &range); |
K6517_current_mode (range); |
for (int n=0;n<nsteps+1;n++){ |
|
if (i>0) { |
if(n==0) n++; |
}; |
u= umin+ n*(umax-umin)/nsteps; |
K6517_vsource_set (u); |
if (n==0) { |
K6517_vsource_set (u); |
Delay(1); |
K6517_get(cres,&k); |
K6517_vsource_set (u); |
Delay(1); |
K6517_get(cres,&k); |
|
} |
K6517_vsource_set (u); |
current =K6517_get(cres,&k); |
if (n==0) Delay(t1); |
Delay(t2); |
// voltage =K6517_vsource_get(); |
current =K6517_get(cres,&k); |
if (current > range) printf("Overflow +\n"); |
if (current < -range) printf("Overflow -\n"); |
// if (current > range) printf("Overflow +\n"); |
// if (current < -range) printf("Overflow -\n"); |
voltage = u; |
xpoints[tockaK]= voltage; |
ypoints[tockaK]= current; |
SetCtrlVal(panel, P2_VMON, voltage); |
if (fp) fprintf(fp, "%d %g %g %g\n",tockaK, u, voltage,current); |
printf("n=%d u=%g voltage=%g current=%g\n",tockaK, u, voltage,current); |
if (plivhandle) DeleteGraphPlot (panel, P2_GRAPHIV, plivhandle, VAL_IMMEDIATE_DRAW); |
plivhandle = PlotXY (panel, P2_GRAPHIV, xpoints, ypoints, tockaK+1, VAL_DOUBLE, VAL_DOUBLE, VAL_CONNECTED_POINTS, VAL_BOLD_X, VAL_SOLID, 1, VAL_RED); |
SetCtrlVal(pn2, P2_VMON, voltage); |
SetCtrlVal(pn2, P2_VCUR, current); |
double temperature=0; |
double humidity=0; |
GetCtrlVal(pn2, P2_TMON, &temperature); |
GetCtrlVal(pn2, P2_HUMIDITY, &humidity); |
if (fp) fprintf(fp, "%d %g %g %g %g %g\n",tockaK, u, voltage,current, humidity, temperature); |
if (debugCode) printf("n=%d u=%g voltage=%g current=%g\n",tockaK, u, voltage,current); |
if (plivhandle) DeleteGraphPlot (pn2, P2_GRAPHIV, plivhandle, VAL_IMMEDIATE_DRAW); |
plivhandle = PlotXY (pn2, P2_GRAPHIV, xpoints, ypoints, tockaK+1, VAL_DOUBLE, VAL_DOUBLE, VAL_CONNECTED_POINTS, VAL_BOLD_X, VAL_SOLID, 1, VAL_RED); |
ProcessSystemEvents(); |
tockaK++; |
if (stop) break; |
} |
if (stop) break; |
} |
u = 0; |
K6517_vsource_set (u); |
SetCtrlVal(pn2, P2_VMON, u); |
|
SetCtrlVal(pnl, P1_ONOFF, 0); |
K6517_vsource_operate (0); |
fclose(fp); |
free(xpoints); |
free(ypoints); |
if (stop) break; |
if (repeatscan){ |
double period; |
GetCtrlVal(pn2,P2_PERIOD,&period); |
while (period>0){ |
Delay(1); |
period--; |
SetCtrlVal(pn2,P2_NEXTSCAN, period); |
if (stop) { |
SetCtrlVal(pn2,P2_NEXTSCAN, 0); |
return 0; |
} |
} |
} |
|
} |
return 0; |
} |
|
|
|
|
|
int CVICALLBACK IVSCANCB (int panel, int control, int event, |
void *callbackData, int eventData1, int eventData2) { |
|
rID=100; |
switch (event) { |
case EVENT_COMMIT:{ |
ThreadFunctionPtr mythread = ivscan; |
CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &rID, |
DEFAULT_THREAD_PRIORITY, |
EndOfThread, |
EVENT_TP_THREAD_FUNCTION_END, |
NULL, RUN_IN_SCHEDULED_THREAD, |
&tfID); |
} |
break; |
} |
return 0; |
} |
|
|
int CVICALLBACK SETVOLTCB (int panel, int control, int event, |
void *callbackData, int eventData1, int eventData2) { |
770,6 → 1053,7 |
case EVENT_COMMIT: |
GetCtrlVal(panel, P2_VOLT, &voltage); |
K6517_vsource_set (voltage); |
SetCtrlVal(panel, P2_VMON, voltage); |
break; |
} |
return 0; |
877,7 → 1161,7 |
int CVICALLBACK DebugCB (int panel, int control, int event, |
void *callbackData, int eventData1, int eventData2) { |
int selected; |
int debugCode; |
|
switch (event) { |
case EVENT_COMMIT: |
GetCtrlVal(panel, control, &selected); |
898,4 → 1182,7 |
} |
return 0; |
} |
|
|
|
#endif |