FryeCom DLL Example Program
 9/13/10 -med

Here is a very simplified C++ example showing how to open a com port
and collect the software version number from a Fonix instrument.

You would actually want to expand this a bit as it is limited and 
too simple for real world use. Searching for the com port for every 
command is not the correct way to go about this. 

For a more complete example look at the FippCore.CPP file provided in 
the BCRVF programming example. If you wish, you can include the 
FippCore.CPP file and it's assocated support files in your own project 
as a starting point, or just pull out the parts of it you need. 

The BCRVF project is a simple real world Windows example showing 
how to communicate with a Fonix instrument using C++. 


----------------------------------------------------------------
The main routine in this example is the GetVersion() routine (at the end).

//----
//Search for a Fonix instrument com port
int FindComPort(int* ComPort) {
  int i;
  int Result;
  int ThisComPort = COM1;                         //Start port search at COM1
  int PortControl = FCOM_USE_PORT_SEEK + FCOM_USE_BAUD_SEEK; //autoseek instrument and baudrate
  int StartBaud = 9600;                           //start baud seek at 9600 baud
  FCOM_TfcCallback Callback = NULL;               //no callback provided

  //Get the last possible com port available
  Result = FCOM_MaxComPort(&MaxPort);
  if (Result != SUCCESS) return(Result);          //return failure if any

  //search all available ports for a Fonix instrument
  for (i=ThisComPort; i<MaxPort; i++) }
    Result = FCOM_OpenPort(ThisComPort, PortControl, StartBaud, Callback);
    if (Result = FCOM_SUCCESS) return(Result);    //we found the port
  }
  return(Result); //return failure if instrument port not found
};

//----
//Close up the com port to let somone else have a chance at it
int CloseComPort(int ComPort) {
  FCOM_ClosePort(ComPort);
};  
 
//----
int WaitForSendReady(FCOM_TfcCallback Callback) {
  int Result;
  //wait until the port is ready to send or an error occurs
  while(FOREVER) {
    Result = FCOM_SendReady(ThisComPort, Callback);
    if (Result != FCOM_NOT_READY) break;
  }
  return(Result);
};

//----
int WaitForRspReady(FCOM_TfcCallback Callback) {
  int Result;
  //wait until the response is received or an error occurs
  while(FOREVER) {
    Result = FCOM_RspReady(ThisComPort);
    if (Result != FCOM_NOT_READY) break;
    if (Callback != NULL) Callback(ThisComPort);
  }
  return(Result);
};

 
//----
//Simplified FIPP command/response procedure
int DoCmd(int ComPort, FCOM_TfcArray* pCmdArray, FCOM_TfcArray* pRspArray) {
  int Result;
  FCOM_TfcCallback Callback = NULL;  //no callback provided

  //wait for the driver to be ready to send a FIPP command
  Result = WaitForSendReady(Callback);
  if (Result != FIPP_SUCCESS) return(Result);        //return if failed

  //Send the FIPP command
  Result = SendCmd(FIPP_STD_POLL_TIMEOUT,pCmdArray); //std poll timeout
  if (Result != FIPP_SUCCESS) return(Result);        //return if failed

  //All FIPP commands except Quick Terminate have a response
  if (pCmdArray->Cmd != FCOM_QTC) {

    //wait for the response from the instrument
    Result = WaitForRspReady(Callback);  
    if (Result != FIPP_SUCCESS) return(Result);      //return if failed

    //Get the FIPP response from the Fonix instrument
    Result = GetResponse(FIPP_ACK_OK,pRspArray);
  }
  return(Result); //return the final FIPP response
};
 
//----
//Simplified routine to get the version number from a Fonix instrument
int GetVersion(int* FonixVersion) {
  int Result;
  int ComPort;
  FCOM_TfcArray FippCmd;
  FCOM_TfcArray FippRsp;
  FIPP_tMachineVersion FVersion;

  //Find the Fonix instrument Com Port
  Result = FindComPort(&ComPort);
  if (Result != FCOM_SUCCESS) return(Result);

  //Build command to get Software version from instrument
  CmdArray.Cmd = 28;  //28=get instrument version information
  CmdArray.Size = 0;  //no data passed with this command

  //Send the command and wait for the response
  Result = DoCmd(ComPort, &FippCmd, &FippRsp);
  if (Result != FCOM_SUCCESS) return(Result);

  //Copy instrument response to local data structure
  memcpy(&FVersion,FippRsp.Data,sizeof(FVersion));

  //Copy Software version to function call return parameter
  *FonixVersion = FVersion.SoftwareVersion;

  //Close the com port
  ClosePort(ComPort);

  return(FCOM_SUCCESS);  //the end
};
