#include <formatio.h>
 
#include <utility.h>
 
#include <ansi_c.h>
 
#include <rs232.h>
 
#include "SP2155.h"
 
 
 
#define COMWAIT 1.0
 
#define COMDELAY 0.05
 
 
 
static int SP2155_Port;
 
static int nin, nout, ires;
 
static char SP2155_Send[100], SP2155_Receive[100];
 
 
 
void _VI_FUNC SP2155_Open (int port)
 
{
 
  SP2155_Port=port;
 
 
 
  OpenComConfig (SP2155_Port, "", 38400, 0, 8, 1, 512, 512);
 
  SetXMode (SP2155_Port, 0);
 
  SetCTSMode (SP2155_Port, LWRS_HWHANDSHAKE_OFF);
 
  SetComTime (SP2155_Port, COMWAIT);
 
 
 
  return;
 
}
 
 
 
int _VI_FUNC SP2155_Cmd (char *cmd, double wait)
 
{
 
  Delay(COMDELAY);
 
  FlushInQ (SP2155_Port);
 
  nout 
= sprintf (SP2155_Send
, "%s\r", cmd
); 
  ComWrt (SP2155_Port, SP2155_Send, nout);
 
  if (wait!=0.) SetComTime (SP2155_Port, wait);
 
  nin = ComRdTerm (SP2155_Port, SP2155_Receive, 90, 0xa);
 
  if (wait!=0.) SetComTime (SP2155_Port, COMWAIT);
 
  if (CompareStrings (SP2155_Receive, nin-3, "ok\r", 0, 0)) return -1;
 
  nin=nin-5;
 
  SP2155_Receive[nin]=0;
 
  return (nin);
 
}
 
 
 
void _VI_FUNC SP2155_Echo (int mode)
 
{
 
  if (mode) {
 
    SP2155_Cmd ("ECHO",0);
 
  }else{
 
    SP2155_Cmd ("NO-ECHO",0);
 
  }
 
  return;
 
}
 
 
 
int _VI_FUNC SP2155_GetWavelength (void)
 
{
 
//** returns wavelength in angstrems
 
 
 
  int ia;
 
  float wl;
 
  
 
  SP2155_Cmd ("?NM",0);
 
  sscanf(SP2155_Receive
," %f ",&wl
);  
  return (ia);
 
}
 
 
 
void _VI_FUNC SP2155_SetWavelength (int wavel)
 
{
 
//** sets wavelength in angstrems
 
 
 
  float wl;
 
  char cmd[100];
 
  
 
  wl=(float)wavel/10.;
 
  SP2155_Cmd (cmd,10);
 
}
 
 
 
int _VI_FUNC SP2155_GetGrating (void)
 
{
 
  int ig;
 
  
 
  SP2155_Cmd ("?GRATING",0);
 
  sscanf(SP2155_Receive
," %d ",&ig
);  
  return (ig);
 
}
 
 
 
void _VI_FUNC SP2155_SetGrating (int grating)
 
{
 
  char cmd[100];
 
 
 
  if ((grating!=1)&&(grating!=2)) return;
 
  sprintf(cmd
,"%0d GRATING",grating
);  
  SP2155_Cmd (cmd,30);
 
}
 
 
 
int _VI_FUNC SP2155_GetTurret (void)
 
{
 
  return (1);    
 
}
 
 
 
void _VI_FUNC SP2155_SetTurret (int turret)
 
{
 
    
 
}
 
 
 
void _VI_FUNC SP2155_SetSpeed (int aperm)
 
{
 
    
 
}
 
 
 
void _VI_FUNC SP2155_Scan (int wavel)
 
{
 
    
 
}
 
 
 
void _VI_FUNC SP2155_Close (void)
 
{
 
  CloseCom (SP2155_Port);
 
}
 
 
 
#ifdef SP2155_MAIN
 
 
 
#include <cvirte.h>
 
 
 
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
 
                                           LPSTR lpszCmdLine, int nCmdShow)
 
{
 
  int ig,iangstrem;
 
  
 
  if (InitCVIRTE (hInstance, 0, 0) == 0) return -1;    /* out of memory */
 
 
 
  SP2155_Open (7);
 
  SP2155_Echo (0);
 
  SP2155_SetGrating (1);
 
  ig=SP2155_GetGrating ();
 
//  SP2155_Cmd ("?TURRET",0);
 
  
 
  SP2155_Close ();
 
        
 
  return 0;
 
}
 
 
 
#endif