-
Notifications
You must be signed in to change notification settings - Fork 0
/
BBVA3.C
307 lines (231 loc) · 7.07 KB
/
BBVA3.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/****************************************************************************/
/* */
/* PROGRAMA : BBVA.c */
/* */
/* PROPOSITO: Puente de SDLC a Puerto Serial */
/* Escrito por Alvaro Medina de PERURIMPEX S.A. */
/****************************************************************************/
#include <windows.h>
#include "bbva.h"
#include "msa.h"
void GateWay(char wParam);
void Sleep(int Time);
int y=0;
HANDLE hInst;
HDC hDC;
NPTTYINFO npTTYInfo ;
DCB dcb;
int err;
char szTemp[ TXQUEUE ] ;
HFILE hfTempFile;
char szBuf[TXQUEUE];
TIMERPROC lpfnMyTimerProc;
int nTimer, pTimer, lTrans;
int lFirst;
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
BOOL bMsgAvail;
if (!hPrevInstance)
if (!InitApplication(hInstance))
return (FALSE);
if (!InitInstance(hInstance, nCmdShow))
return (FALSE);
while (GetMessage(&msg, NULL, NULL, NULL)) {
TranslateMessage((LPMSG)&msg);
DispatchMessage((LPMSG)&msg);
}
return (msg.wParam);
}
BOOL InitApplication(HANDLE hInstance)
{
WNDCLASS wc;
wc.style = NULL;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE( ICONO_BBVA ) ); //LoadIcon(hInstance, ID_BBVA);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = "SDLC";
wc.lpszClassName = "SDLCClass";
return (RegisterClass(&wc));
}
BOOL InitInstance(HANDLE hInstance, int nCmdShow)
{
HWND hWnd;
int i;
char tmp[100];
hInst = hInstance;
hWnd = CreateWindow(
"SDLCClass",
"Gateway from SDLC to Serial Port",
WS_SYSMENU | WS_MINIMIZEBOX,
200,
100,
290,
50,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
return (FALSE);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if ((i = MSAInit()) <= 0) { //**
wsprintf(tmp, "MOXA INIT error %d", i); //**
MessageBox(hWnd, tmp, NULL, MB_OK | MB_ICONHAND); //**
return(FALSE); //**
} //**
if ((COMDEV( npTTYInfo ) = OpenComm( "COM5", RXQUEUE, TXQUEUE )) < 0)
{
MessageBox(hWnd,"Error:OpenComm","",MB_OK);
return ( FALSE ) ;
}
err = BuildCommDCB("COM5:19200,n,8,1", &dcb);
if (err < 0) {
MessageBox(hWnd, "Ok", "BuildCommDCB", MB_OK);
return 0;
}
err = SetCommState(&dcb);
if (err < 0) {
MessageBox(hWnd, "Ok", "SetCommState", MB_OK);
return 0;
}
strcpy(szBuf, "c:\\SDLC.TXT");
hfTempFile = _lcreat(szBuf, 0);
if (hfTempFile == HFILE_ERROR) {
MessageBox(hWnd, "Ok", "OpenFile", MB_OK);
return ( FALSE);
}
pTimer = 1000;
lpfnMyTimerProc = (TIMERPROC) MakeProcInstance(MyTimerProc, hInstance);
SetTimer(hWnd, ID_MYTIMER, pTimer, lpfnMyTimerProc);
nTimer = 0;
lTrans = 0;
SendMessage( hWnd, WM_SYSCOMMAND, (WPARAM) SC_MINIMIZE, NULL ) ;
return (TRUE);
}
long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
WORD wParam, LONG lParam)
{
FARPROC lpProcAbout;
HMENU hMenu;
PAINTSTRUCT ps;
switch (message) {
case WM_CHAR:
GateWay(wParam);
break;
case WM_COMMAND:
switch (wParam) {
default: /* Lets Windows process it */
return (DefWindowProc(hWnd, message, wParam, lParam));
}
break;
case WM_DESTROY:
PostQuitMessage(0);
MSAEnd();
CloseComm( COMDEV( npTTYInfo ) ) ;
_lclose(hfTempFile);
break;
default:
// GateWay(wParam, hWnd);
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (NULL);
}
void CALLBACK MyTimerProc(HWND hWnd, UINT msg, UINT idTimer, DWORD dwTime)
{
unsigned long t1;
int i,n=0, nFlg = 0;
HDC hDC;
if (nTimer > 5000 && lTrans)
{
nTimer = 0;
lTrans = 0;
WriteComm(COMDEV( npTTYInfo ), (LPSTR) "FIN", 3);
TextOut(hDC, 10, y, (LPSTR) "fin", 3);
MessageBox(hWnd, "FIN" ,"ok", MB_OK);
}
else
{
nTimer += pTimer;
sprintf(szBuf, "%d" , nTimer);
TextOut(hDC, 10, y, (LPSTR) szBuf, strlen(szBuf));
}
hDC = GetDC(hWnd);
memset( szTemp, 0, TXQUEUE) ;
if ((i = MSARead(szTemp, TXQUEUE)) > 0)
{
if (strlen(szTemp)> 8 ) //(strcmp(Control, szTemp))
{
// KillTimer(hWnd, ID_MYTIMER);
// Sleep(2000);
for(n=0;i>n;n++)
{
if (!szTemp[n]) szTemp[n]=' ';
if (szTemp[n]== 12) { szTemp[n++]=0; nFlg = 1; break; }
}
memset( szBuf, 0, TXQUEUE) ;
strcpy(szBuf, szTemp + 37);
while ((i = MSARead(szTemp, TXQUEUE)) > 8)
{
for(n=0;i>n;n++)
{
if (!szTemp[n]) szTemp[n]=' ';
if (szTemp[n]== 12) { szTemp[n++]=0; nFlg = 1; break; }
}
strcat(szBuf, szTemp + 9);
//Sleep(100);
}
MessageBox(hWnd, szBuf ,"ok", MB_OK);
if (!nTimer ) {
lTrans = 1;
nTimer = pTimer;
WriteComm(COMDEV( npTTYInfo ), (LPSTR) "INICIO", 6);
sprintf(szBuf, "%d" , nTimer);
TextOut(hDC, 10, y, (LPSTR) szBuf, strlen(szBuf));
}
//TextOut(hDC, 10, y, (LPSTR) "Procesando....", 14);
// if (nFlg)
// {
//strcpy(szBuf, szTemp);
//strcpy(szBuf, "INICIO");
// strcat(szBuf, szTemp + 37);
//strcat(szBuf,"FIN");
// if (lTrans)
// MessageBox(hWnd, szTemp + 37 ,"ok", MB_OK);
// else
// MessageBox(hWnd, szTemp + 9 ,"ok", MB_OK);
//sprintf(szBuf, "%s --> %d ==>%d,%d,%d ---->i=%d\n", szTemp, strlen(szTemp),szTemp[0], szTemp[1], szTemp[2],i);
//TextOut(hDC, 10, y, (LPSTR) szBuf, strlen(szBuf));
WriteComm(COMDEV( npTTYInfo ), (LPSTR) szBuf, strlen(szBuf));
_lwrite(hfTempFile, (LPSTR) szBuf, strlen(szBuf) );
//TextOut(hDC, 10, y, (LPSTR) " ", 28);
//y+=15;
// }
SetTimer(hWnd, ID_MYTIMER, pTimer, lpfnMyTimerProc);
}
else lTrans = 0;
}
t1 = MSAIsUnderRun();
ReleaseDC(hWnd, hDC);
}
void GateWay(char wParam)
{
memset( szTemp, 0, TXQUEUE) ;
*szTemp = wParam;
//strcpy(szTemp, "Hola");
WriteComm(COMDEV( npTTYInfo ), (LPSTR) szTemp, strlen(szTemp));
}
void Sleep(int Time)
{
DWORD n;
n= GetTickCount();
while ((GetTickCount() - n) > Time) Yield();
return;
}