Subversion Repositories f9daq

Rev

Blame | Last modification | View Log | RSS feed

#include <utility.h>
#include <ansi_c.h>
#include <tcpsupp.h>
#include <cvirte.h>            
#include <userint.h>
#include "testcli_ui.h"

static int panelHandle;
static CmtThreadPoolHandle poolHandle = 0;  
#define MAX_THREADS 10  

static unsigned int chandle = 0;
static int tfID;

int main (int argc, char *argv[]) {
  if (InitCVIRTE (0, argv, 0) == 0)
    return -1;  /* out of memory */
  SetStdioPort (CVI_STDIO_WINDOW);
  SetSleepPolicy(VAL_SLEEP_MORE);
  CmtNewThreadPool (MAX_THREADS,  &poolHandle);
 
 
  if ((panelHandle = LoadPanel (0, "testcli_ui.uir", PANEL)) < 0)
    return -1;
  DisplayPanel (panelHandle);
  RunUserInterface ();
  DiscardPanel (panelHandle);
  return 0;
}


int CVICALLBACK SocketCB (unsigned handle, int xType, int errCode, void *callbackData) {

  int nb = 0 ;

  const int maxlen=0xFF;
  char msg[maxlen];    
  for (int i=0;i<maxlen; i++) msg[i]=0;
  switch (xType) {
    case TCP_DISCONNECT:
      printf("TCP_DISCONNECT ErrorString %s\n",GetTCPErrorString(errCode));
      printf("TCP_DISCONNECT SystemErrorString %s\n",GetTCPSystemErrorString());
      //DisconnectFromTCPServer (&chandle);
      chandle = 0;
      break;
    case TCP_DATAREADY: {
     
      int  hdr[2]={0,0};
      nb = ClientTCPRead(handle,&hdr[0],8,1000);
     
      int size = hdr[1] - 8;
      if (size>maxlen) size=maxlen;
      nb = 0;
      while (nb < size) {
         int retval = ClientTCPRead(handle,&msg[nb],size-nb,1000);
         if (retval<1) break;
         nb += retval;
      }
      printf("Received RECID %d LEN %d: %s\n", hdr[0], hdr[1],msg);
     
      break;
     
    }
  }
  return 0;
}

int CVICALLBACK ExitCB (int panel, int control, int event,
                        void *callbackData, int eventData1, int eventData2) {
  switch (event) {
    case EVENT_COMMIT:
      QuitUserInterface (0);
      break;
  }
  return 0;
}

int CVICALLBACK SendCB (int panel, int control, int event,
                        void *callbackData, int eventData1, int eventData2) {
  char data[0xFF];
  char *msg = &data[8];
  int  *hdr = data;  
  static int ncount=0;
  switch (event) {
    case EVENT_COMMIT:
      GetCtrlVal(panel,PANEL_TXT  , msg);
      sprintf(msg,"%s %d\n", msg, ncount++);
      hdr[0]=ncount%4;
      hdr[1]=strlen(msg) + 8;
      ClientTCPWrite(chandle,data,hdr[1],1000);  // init
      break;
  }
  return 0;
}

int CVICALLBACK ConnectCB (int panel, int control, int event,
                           void *callbackData, int eventData1, int eventData2) {
  switch (event) {
    case EVENT_COMMIT:
      ConnectToTCPServerEx (&chandle, 3210, "192.168.1.254", SocketCB, NULL, 0, TCP_ANY_LOCAL_PORT);  
      break;
  }
  return 0;
}