//==============================================================================
 
//
 
// Title:               prologix.c
 
// Purpose:             A short description of the implementation.
 
//
 
// Created on:  29.11.2014 at 20:51:03 by korpar.
 
// Copyright:   . All Rights Reserved.
 
//
 
//==============================================================================
 
 
 
//==============================================================================
 
// Include files
 
 
 
#include <rs232.h>
 
#include <utility.h>
 
#include <ansi_c.h>
 
#include "prologix.h"
 
 
 
//==============================================================================
 
// Constants
 
 
 
//==============================================================================
 
// Types
 
 
 
//==============================================================================
 
// Static global variables
 
 
 
//==============================================================================
 
// Static functions
 
 
 
//==============================================================================
 
// Global variables
 
 
 
//==============================================================================
 
// Global functions
 
 
 
#ifdef PROLOGIX_MAIN
 
#  include <cvirte.h>
 
//#  include "PROLOGIX_uic.h"
 
#  include "PROLOGIX_ui.h"
 
#endif
 
 
 
#define COMWAIT 3.1
 
#define COMDELAY 0.05
 
 
 
static char PROLOGIX_Out[100], PROLOGIX_In[100];
 
static int PROLOGIX_Port;
 
static int nin, nout;
 
 
 
int _VI_FUNC PROLOGIX_Open (int port)
 
{
 
  int status;
 
  PROLOGIX_Port=port;
 
  status=OpenComConfig (PROLOGIX_Port, "", 38400, 0, 8, 1, 512, 512);
 
  if (status) return status;
 
  SetXMode (PROLOGIX_Port, 0);
 
  SetCTSMode (PROLOGIX_Port, LWRS_HWHANDSHAKE_OFF);
 
  SetComTime (PROLOGIX_Port, COMWAIT);
 
  PROLOGIX_Send("++savecfg 0");
 
  return 0;
 
}
 
 
 
int _VI_FUNC PROLOGIX_Send (char command[])
 
{
 
  Delay(COMDELAY);
 
  FlushInQ (PROLOGIX_Port);
 
  nout 
= sprintf (PROLOGIX_Out
, "%s\r", command
); 
  if (nout != ComWrt (PROLOGIX_Port, PROLOGIX_Out, nout)){
 
    return -1;
 
  }else{
 
    return 0;
 
  }
 
}
 
 
 
int _VI_FUNC PROLOGIX_Receive (char received[], int length)
 
{
 
  nin = ComRdTerm (PROLOGIX_Port, received, length, 0xa);
 
  if (nin>=0) received[nin]=0;
 
  return nin;
 
}
 
 
 
int _VI_FUNC PROLOGIX_SetAddr (int *primaryAddr, int *secondaryAddr)
 
{
 
  int nread;
 
  char cmd[100];
 
 
 
  if (*primaryAddr<0) {
 
    PROLOGIX_Send("++addr");
 
    PROLOGIX_Receive(cmd,100);
 
    return sscanf(cmd
,"%d %d",primaryAddr
,secondaryAddr
);  
  } else if (*primaryAddr<31) {
 
    sprintf(cmd
,"++addr %0d",*primaryAddr
);  
    if ((*secondaryAddr>=0)&&(*secondaryAddr<31))
 
      sprintf(cmd
,"%s %0d",cmd
,*secondaryAddr
);  
    return PROLOGIX_Send(cmd);
 
  } else return -1;
 
}
 
 
 
void _VI_FUNC PROLOGIX_Close (void)
 
{
 
  CloseCom (PROLOGIX_Port);
 
}
 
 
 
#ifdef PROLOGIX_MAIN
 
 
 
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
 
                       LPSTR lpszCmdLine, int nCmdShow)
 
{
 
  int i,j,nch;
 
  int paddr,saddr;
 
  int nlines,atline;
 
  int evcontrl,evpanel,p1_h,ierr;
 
  int port,comled;
 
  char cmd[100],res[100];
 
 
 
  if (InitCVIRTE (hInstance, 0, 0) == 0)
 
    return -1;    /* out of memory */
 
 
 
  p1_h = LoadPanel (0,"PROLOGIX_ui.uir", P1);
 
//  p1_h = BuildP1 (0);
 
  ierr = DisplayPanel (p1_h);
 
 
 
  while (1)
 
  {
 
    ierr = GetUserEvent (1, &evpanel, &evcontrl);
 
    if (evcontrl == P1_EXIT_B) break;
 
    GetCtrlVal (p1_h, P1_COMLED, &comled);
 
    if (!comled){
 
      GetCtrlVal (p1_h, P1_PORT, &port);
 
      if (PROLOGIX_Open (port)) continue;
 
      SetCtrlVal (p1_h, P1_COMLED, 1);
 
    }
 
    switch (evcontrl)
 
    {
 
      case P1_SEND_B:
 
      case P1_SEND_S:
 
        ierr = GetCtrlVal (p1_h, P1_SEND_S, cmd);
 
        PROLOGIX_Send (cmd);
 
        nch=PROLOGIX_Receive(res,100);
 
        if (nch>0) {
 
          ierr = SetCtrlVal (p1_h, P1_REPLY_N, nch);
 
          ierr = SetCtrlVal (p1_h, P1_REPLY_S, res);
 
        }
 
        break;
 
      case P1_STATUS_B:
 
        PROLOGIX_Send("++savecfg");
 
        nch=PROLOGIX_Receive(res,100);
 
        ierr=SetCtrlVal(p1_h,P1_SAVECFG_L,i);
 
        PROLOGIX_Send("++mode");
 
        nch=PROLOGIX_Receive(res,100);
 
        ierr=SetCtrlVal(p1_h,P1_MODE_L,i);
 
        paddr=-1;
 
        if (PROLOGIX_SetAddr(&paddr,&saddr)==1) saddr=-1;
 
        ierr=SetCtrlVal(p1_h,P1_PADDR_N,paddr);
 
        ierr=SetCtrlVal(p1_h,P1_SADDR_N,saddr);
 
        sprintf(res
,"PrimaryAddr=%d, SecondaryAddr=%d",paddr
,saddr
);  
        ierr=InsertTextBoxLine(p1_h,P1_MESSAGE_TB,-1,res);
 
        PROLOGIX_Send("++auto");
 
        nch=PROLOGIX_Receive(res,100);
 
        ierr=SetCtrlVal(p1_h,P1_AUTO_L,i);
 
        PROLOGIX_Send("++eoi");
 
        nch=PROLOGIX_Receive(res,100);
 
        ierr=SetCtrlVal(p1_h,P1_EOI_L,i);
 
        PROLOGIX_Send("++eos");
 
        nch=PROLOGIX_Receive(res,100);
 
        ierr=SetCtrlVal(p1_h,P1_EOS_R,i);
 
        PROLOGIX_Send("++eot_enable");
 
        nch=PROLOGIX_Receive(res,100);
 
        ierr=SetCtrlVal(p1_h,P1_EOTEN_L,i);
 
        PROLOGIX_Send("++eot_char");
 
        nch=PROLOGIX_Receive(res,100);
 
        ierr=SetCtrlVal(p1_h,P1_EOTCH_N,i);
 
        PROLOGIX_Send("++read_tmo_ms");
 
        nch=PROLOGIX_Receive(res,100);
 
        ierr=SetCtrlVal(p1_h,P1_READTO_N,i);
 
        break;
 
      default:
 
        break;
 
    }
 
  }
 
 
 
  GetCtrlVal (p1_h, P1_COMLED, &comled);
 
  if (comled) PROLOGIX_Close ();
 
 
 
  return 0;
 
}
 
 
 
#endif PROLOGIX_MAIN