Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
254 f9daq 1
//==============================================================================
2
//
3
// Title:               prologix.c
4
// Purpose:             A short description of the implementation.
5
//
6
// Created on:  29.11.2014 at 20:51:03 by korpar.
7
// Copyright:   . All Rights Reserved.
8
//
9
//==============================================================================
10
 
11
//==============================================================================
12
// Include files
13
 
14
#include <rs232.h>
15
#include <utility.h>
16
#include <ansi_c.h>
17
#include "prologix.h"
18
 
19
//==============================================================================
20
// Constants
21
 
22
//==============================================================================
23
// Types
24
 
25
//==============================================================================
26
// Static global variables
27
 
28
//==============================================================================
29
// Static functions
30
 
31
//==============================================================================
32
// Global variables
33
 
34
//==============================================================================
35
// Global functions
36
 
37
#ifdef PROLOGIX_MAIN
38
#  include <cvirte.h>
39
//#  include "PROLOGIX_uic.h"
40
#  include "PROLOGIX_ui.h"
41
#endif
42
 
43
#define COMWAIT 3.1
44
#define COMDELAY 0.05
45
 
46
static char PROLOGIX_Out[100], PROLOGIX_In[100];
47
static int PROLOGIX_Port;
48
static int nin, nout;
49
 
50
int _VI_FUNC PROLOGIX_Open (int port)
51
{
52
  int status;
53
  PROLOGIX_Port=port;
54
  status=OpenComConfig (PROLOGIX_Port, "", 38400, 0, 8, 1, 512, 512);
55
  if (status) return status;
56
  SetXMode (PROLOGIX_Port, 0);
57
  SetCTSMode (PROLOGIX_Port, LWRS_HWHANDSHAKE_OFF);
58
  SetComTime (PROLOGIX_Port, COMWAIT);
59
  PROLOGIX_Send("++savecfg 0");
60
  return 0;
61
}
62
 
63
int _VI_FUNC PROLOGIX_Send (char command[])
64
{
65
  Delay(COMDELAY);
66
  FlushInQ (PROLOGIX_Port);
67
  nout = sprintf (PROLOGIX_Out, "%s\r", command);
68
  if (nout != ComWrt (PROLOGIX_Port, PROLOGIX_Out, nout)){
69
    return -1;
70
  }else{
71
    return 0;
72
  }
73
}
74
 
75
int _VI_FUNC PROLOGIX_Receive (char received[], int length)
76
{
77
  nin = ComRdTerm (PROLOGIX_Port, received, length, 0xa);
78
  if (nin>=0) received[nin]=0;
79
  return nin;
80
}
81
 
82
int _VI_FUNC PROLOGIX_SetAddr (int *primaryAddr, int *secondaryAddr)
83
{
84
  int nread;
85
  char cmd[100];
86
 
87
  if (*primaryAddr<0) {
88
    PROLOGIX_Send("++addr");
89
    PROLOGIX_Receive(cmd,100);
90
    return sscanf(cmd,"%d %d",primaryAddr,secondaryAddr);
91
  } else if (*primaryAddr<31) {
92
    sprintf(cmd,"++addr %0d",*primaryAddr);
93
    if ((*secondaryAddr>=0)&&(*secondaryAddr<31))
94
      sprintf(cmd,"%s %0d",cmd,*secondaryAddr);
95
    return PROLOGIX_Send(cmd);
96
  } else return -1;
97
}
98
 
99
void _VI_FUNC PROLOGIX_Close (void)
100
{
101
  CloseCom (PROLOGIX_Port);
102
}
103
 
104
#ifdef PROLOGIX_MAIN
105
 
106
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
107
                       LPSTR lpszCmdLine, int nCmdShow)
108
{
109
  int i,j,nch;
110
  int paddr,saddr;
111
  int nlines,atline;
112
  int evcontrl,evpanel,p1_h,ierr;
113
  int port,comled;
114
  char cmd[100],res[100];
115
 
116
  if (InitCVIRTE (hInstance, 0, 0) == 0)
117
    return -1;    /* out of memory */
118
 
119
  p1_h = LoadPanel (0,"PROLOGIX_ui.uir", P1);
120
//  p1_h = BuildP1 (0);
121
  ierr = DisplayPanel (p1_h);
122
 
123
  while (1)
124
  {
125
    ierr = GetUserEvent (1, &evpanel, &evcontrl);
126
    if (evcontrl == P1_EXIT_B) break;
127
    GetCtrlVal (p1_h, P1_COMLED, &comled);
128
    if (!comled){
129
      GetCtrlVal (p1_h, P1_PORT, &port);
130
      if (PROLOGIX_Open (port)) continue;
131
      SetCtrlVal (p1_h, P1_COMLED, 1);
132
    }
133
    switch (evcontrl)
134
    {
135
      case P1_SEND_B:
136
      case P1_SEND_S:
137
        ierr = GetCtrlVal (p1_h, P1_SEND_S, cmd);
138
        PROLOGIX_Send (cmd);
139
        nch=PROLOGIX_Receive(res,100);
140
        if (nch>0) {
141
          ierr = SetCtrlVal (p1_h, P1_REPLY_N, nch);
142
          ierr = SetCtrlVal (p1_h, P1_REPLY_S, res);
143
        }
144
        break;
145
      case P1_STATUS_B:
146
        PROLOGIX_Send("++savecfg");
147
        nch=PROLOGIX_Receive(res,100);
148
        sscanf(res,"%d",&i);
149
        ierr=SetCtrlVal(p1_h,P1_SAVECFG_L,i);
150
        PROLOGIX_Send("++mode");
151
        nch=PROLOGIX_Receive(res,100);
152
        sscanf(res,"%d",&i);
153
        ierr=SetCtrlVal(p1_h,P1_MODE_L,i);
154
        paddr=-1;
155
        if (PROLOGIX_SetAddr(&paddr,&saddr)==1) saddr=-1;
156
        ierr=SetCtrlVal(p1_h,P1_PADDR_N,paddr);
157
        ierr=SetCtrlVal(p1_h,P1_SADDR_N,saddr);
158
        sprintf(res,"PrimaryAddr=%d, SecondaryAddr=%d",paddr,saddr);
159
        ierr=InsertTextBoxLine(p1_h,P1_MESSAGE_TB,-1,res);
160
        PROLOGIX_Send("++auto");
161
        nch=PROLOGIX_Receive(res,100);
162
        sscanf(res,"%d",&i);
163
        ierr=SetCtrlVal(p1_h,P1_AUTO_L,i);
164
        PROLOGIX_Send("++eoi");
165
        nch=PROLOGIX_Receive(res,100);
166
        sscanf(res,"%d",&i);
167
        ierr=SetCtrlVal(p1_h,P1_EOI_L,i);
168
        PROLOGIX_Send("++eos");
169
        nch=PROLOGIX_Receive(res,100);
170
        sscanf(res,"%d",&i);
171
        ierr=SetCtrlVal(p1_h,P1_EOS_R,i);
172
        PROLOGIX_Send("++eot_enable");
173
        nch=PROLOGIX_Receive(res,100);
174
        sscanf(res,"%d",&i);
175
        ierr=SetCtrlVal(p1_h,P1_EOTEN_L,i);
176
        PROLOGIX_Send("++eot_char");
177
        nch=PROLOGIX_Receive(res,100);
178
        sscanf(res,"%d",&i);
179
        ierr=SetCtrlVal(p1_h,P1_EOTCH_N,i);
180
        PROLOGIX_Send("++read_tmo_ms");
181
        nch=PROLOGIX_Receive(res,100);
182
        sscanf(res,"%d",&i);
183
        ierr=SetCtrlVal(p1_h,P1_READTO_N,i);
184
        break;
185
      default:
186
        break;
187
    }
188
  }
189
 
190
  GetCtrlVal (p1_h, P1_COMLED, &comled);
191
  if (comled) PROLOGIX_Close ();
192
 
193
  return 0;
194
}
195
 
196
#endif PROLOGIX_MAIN