Using Socket Class
#include <stdio.h>
#include <stdlib.h>
#include "UtilSocket.h"
static UtilSocket *socket = NULL;
static const int TCP_PORT = 923;
static char lastStatus[4096];
static const char *STATUS_NOT_READY = "[none]";
static const char *STATUS_OKAY = "[none]" ;
void OpenSocket(char *domainName)
{
CloseSocket();
socket = new UtilSocket(AF_INET);
socket->Connect(domainName,TCP_PORT);
}
void CloseSocket()
{
lastStatus[0]=0;
if(socket!=NULL)
{
delete socket;
socket=NULL;
}
}
void SendCommand( const char *command ) // always ends with '\n' character
{
lastStatus[0]=0;
if(socket==NULL)
throw("[Socket_Is_Not_Opened]");
socket->Send("stc\n"); // clear status
socket->Send(command);
socket->Send("st?\n"); // retrieve status
socket->Receive(lastStatus, 4096 );
if( strcmp(lastStatus, STATUS_OKAY) != 0 )
throw("[Command_Failed]");
}
void QueryProperty( const char *query, char *responseBuffer, int maxLength )
{
lastStatus[0]=0;
if(socket==NULL)
throw("[Socket_Is_Not_Opened]");
socket->Send("stc\n"); // clear status
socket->Send(query);
socket->Receive(responseBuffer, maxLength );
socket->Send("st?\n"); // retrieve status
socket->Receive(lastStatus, 4096 );
if( strcmp(lastStatus, STATUS_OKAY) !=0 )
throw("[Query_Failed]");
return responseBuffer;
}
// examples of how to use lower-level SendCommand and QueryProperty procedures:
void SetEyeColumns( int newValue )
{
char cmdBuffer[4096];
sprintf("Eye:Cfg:Columns %d\n", newValue);
SendCommand(cmdBuffer);
}
int GetEyeColumns()
{
int retn=0;
char queryBuffer[4096];
QueryProperty("Eye:Cfg:Columns?\n",queryBuffer,4096);
sscanf(queryBuffer,"%d",&retn);
return retn;
}
void DoEyeAutoAlign()
{
char Buffer[4096] ;
SendCommand("Eye:Align\n");
do {
sleep(1);
QueryResponse("Eye:AlignResult?\n",Buffer,4096);
}
while( strcmp(Buffer, STATUS_NOT_READY )==0 ) ;
printf("Alignment result is: %s\n", Buffer );
}
void main( int argc, char *argv[] )
{
try
{
OpenSocket("192.168.1.65");
SetEyeColumns(200);
printf("Colums are now: %d\n", GetEyeColumns() );
DoEyeAutoAlign();
CloseSocket();
}
catch(const char *msg)
{
printf("Error encountered: %s %s\n", msg, lastStatus );
CloseSocket();
}
}
// EOF