1,3 → 1,5 |
#include "K6517-ctrl.h" |
#include <tcpsupp.h> |
#include <rs232.h> |
#include <utility.h> |
#include <userint.h> |
24,8 → 26,7 |
#define COM_PORT 11 |
|
|
typedef struct |
{ |
typedef struct { |
double dState; // Last temperature input |
double iState; // Integrator state |
double iMax, iMin; // Maximum and minimum allowable integrator state |
36,8 → 37,7 |
|
SPid pid; |
|
double UpdatePID(SPid * pid, double error, double temperature) |
{ |
double UpdatePID(SPid *pid, double error, double temperature) { |
double pTerm, dTerm, iTerm; |
pTerm = pid->pGain * error; |
// calculate the proportional term |
61,8 → 61,7 |
|
|
int CVICALLBACK ReadVoltageCurrentCB (int panel, int control, int event, |
void *callbackData, int eventData1, int eventData2) |
{ |
void *callbackData, int eventData1, int eventData2) { |
|
int iRet; |
char ch=0; |
69,8 → 68,7 |
double Voltage; |
double Current; |
char cv_cc; |
switch (event) |
{ |
switch (event) { |
case EVENT_COMMIT: |
GetCtrlVal(pa,PA_CHANNEL, &ch); |
iRet = TMI_TimeOut(TMI_DeviceId, 1); |
90,13 → 88,11 |
|
|
int CVICALLBACK SelectChannelCB (int panel, int control, int event, |
void *callbackData, int eventData1, int eventData2) |
{ |
void *callbackData, int eventData1, int eventData2) { |
unsigned char state, preset; |
double Voltage, Current; |
unsigned char ch; |
switch (event) |
{ |
switch (event) { |
case EVENT_COMMIT: |
GetCtrlVal(pa,PA_CHANNEL, &ch); |
GetCtrlVal(pa,PA_PRESET, &preset); |
114,10 → 110,89 |
} |
|
|
#define tcpChk(f) if ((g_TCPError=(f)) < 0) {ReportTCPError(); return -1; } |
|
/*---------------------------------------------------------------------------*/ |
/* Module-globals */ |
/*---------------------------------------------------------------------------*/ |
static unsigned int g_hconversation; |
static int g_TCPError = 0; |
|
int main (int argc, char *argv[]) |
{ |
/*---------------------------------------------------------------------------*/ |
/* Internal function prototypes */ |
/*---------------------------------------------------------------------------*/ |
/*---------------------------------------------------------------------------*/ |
/* 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); |
} |
} |
|
|
int CVICALLBACK ServerTCPCB (unsigned handle, int event, int error, |
void *callbackData) { |
char receiveBuf[256] = {0}; |
ssize_t dataSize = sizeof (receiveBuf) - 1; |
char addrBuf[31]; |
|
switch (event) { |
case TCP_CONNECT: |
if (g_hconversation) { |
/* We already have one client, don't accept another... */ |
tcpChk (GetTCPPeerAddr (handle, addrBuf, 31)); |
sprintf (receiveBuf, "-- Refusing conection request from " |
"%s --\n", addrBuf); |
printf("%s\n", receiveBuf); |
tcpChk (DisconnectTCPClient (handle)); |
} else { |
/* Handle this new client connection */ |
g_hconversation = handle; |
tcpChk (GetTCPPeerAddr (g_hconversation, addrBuf, 31)); |
printf("%s\n", addrBuf); |
tcpChk (GetTCPPeerName (g_hconversation, receiveBuf, 256)); |
printf("%s\n", receiveBuf); |
sprintf (receiveBuf, "-- New connection from %s --\n", |
addrBuf); |
printf("%s\n", receiveBuf); |
|
/* Set the disconect mode so we do not need to terminate */ |
/* connections ourselves. */ |
tcpChk (SetTCPDisconnectMode (g_hconversation, TCP_DISCONNECT_AUTO)); |
} |
break; |
case TCP_DATAREADY: |
if ((dataSize = ServerTCPRead (g_hconversation, receiveBuf, dataSize, 1000)) < 0) { |
printf("Receive Error\n"); |
} else { |
receiveBuf[dataSize] = '\0'; |
printf("Received %s\n", receiveBuf); |
} |
break; |
case TCP_DISCONNECT: |
if (handle == g_hconversation) { |
/* The client we were talking to has disconnected... */ |
g_hconversation = 0; |
printf("-- Client disconnected --\n"); |
|
/* Note that we do not need to do any more because we set the*/ |
/* disconnect mode to AUTO. */ |
} |
break; |
} |
return 0; |
} |
|
|
|
|
|
int main (int argc, char *argv[]) { |
int DeviceId=0; |
unsigned char ch; |
unsigned char MainOutput, preset; |
126,9 → 201,9 |
if (InitCVIRTE (0, argv, 0) == 0) |
return -1; /* out of memory */ |
SetStdioPort (CVI_STDIO_WINDOW); |
pid.iGain= 0.3; |
pid.dGain= 0.5; |
pid.pGain = 3; |
pid.iGain= 0.1; |
pid.dGain= 2; |
pid.pGain = 5; |
pid.iMax =100; |
pid.iMin =-100 ; |
pid.iState=0; |
136,7 → 211,7 |
|
if ((pa = LoadPanel (0, "TempCtrl.uir", PA)) < 0) |
return -1; |
|
DisableBreakOnLibraryErrors(); |
if (TMI_Open()== 0) MessagePopup("Error","Cannot open USB device"); |
DeviceId = TMI_OpenHandle ("PW-A","USB:1:1"); |
if (TMI_Test() == 0 )MessagePopup("DLL error","Dll Error"); |
151,6 → 226,26 |
GetCtrlVal(pa, P1_TINTERVAL, &tinterval); |
SetCtrlAttribute (pa, P1_TIMER, ATTR_INTERVAL, tinterval); |
*/ |
|
SetWaitCursor (1); |
int portNum = 10000; |
int registered = 0; |
char tempBuf[256] = {0}; |
if (RegisterTCPServer (portNum, ServerTCPCB, 0) < 0) |
MessagePopup("TCP Server", "Server registration failed!"); |
else { |
SetWaitCursor (0); |
registered = 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); |
|
|
|
} |
|
SelectChannelCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
|
160,6 → 255,8 |
SetDimming(0); |
RunUserInterface (); |
CloseCom(COM_PORT); |
if (registered) |
UnregisterTCPServer (portNum); |
DiscardPanel (pa); |
TMI_Close(); |
if (gFp) fclose(gFp); |
180,11 → 277,9 |
|
|
int CVICALLBACK SwitchOnOffCB (int panel, int control, int event, |
void *callbackData, int eventData1, int eventData2) |
{ |
void *callbackData, int eventData1, int eventData2) { |
unsigned char state; |
switch (event) |
{ |
switch (event) { |
case EVENT_COMMIT: |
GetCtrlVal(panel, control, &state); |
TMI_MainOutput(TMI_DeviceId, state); |
197,7 → 292,7 |
|
/* Callback Function */ |
void ComCallback(int portNumber, int eventMask, void *callbackdata) { |
|
static double told=0; |
if (eventMask & LWRS_RXFLAG) { |
//printf("Received specified character\n"); |
int strLen = GetInQLen (COM_PORT); |
252,10 → 347,17 |
ReadVoltageCurrentCB (pa, PA_CHANNEL, EVENT_COMMIT, NULL, 0,0); |
|
PlotStripChart (pa, PA_GRAPH, &temp, 1, 0, 0, VAL_DOUBLE); |
double pgraph[2]={vmon,imon}; |
PlotStripChart (pa, PA_GRAPH_VMON, pgraph, 2, 0, 0, VAL_DOUBLE); |
double pgraph[3]= {vmon,imon, 100*(temp-told)}; |
PlotStripChart (pa, PA_GRAPH_VMON, pgraph, 3, 0, 0, VAL_DOUBLE); |
PlotStripChart (pa, PA_GRAPH_3, &humidity, 1, 0, 0, VAL_DOUBLE); |
SetCtrlVal(pa,PA_TMON,temp); |
|
SetCtrlVal(pa,PA_HUMIDITY,humidity); |
|
char transmitBuf[512]= {0}; |
sprintf(transmitBuf, "%u %f %f %f %f\n", time(NULL), humidity, temp, tdiff, temp-told); |
if (g_hconversation) |
if ( ServerTCPWrite (g_hconversation, transmitBuf, strlen (transmitBuf), 1000) < 0) printf("Transmit Error\n"); |
int log=0; |
GetCtrlVal(pa,PA_LOG, &log); |
if (log) { |
262,10 → 364,11 |
char fname[0xFF]; |
GetCtrlVal(pa,PA_FNAME, fname); |
FILE *fp = fopen (fname,"a"); |
fprintf(fp, "%u 0 %f\n", time(NULL), temp); |
fprintf(fp,transmitBuf); |
fclose(fp); |
|
} |
told= temp; |
|
} |
} |
333,3 → 436,4 |
} |
return 0; |
} |
|