-
Notifications
You must be signed in to change notification settings - Fork 2
/
COMLNKIO.PAS
373 lines (308 loc) · 9.51 KB
/
COMLNKIO.PAS
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
{/////////////////////////////////////////////////////////////////////////
//
// Dos Navigator Version 1.51 Copyright (C) 1991-99 RIT Research Labs
//
// This programs is free for commercial and non-commercial use as long as
// the following conditions are aheared to.
//
// Copyright remains RIT Research Labs, and as such any Copyright notices
// in the code are not to be removed. If this package is used in a
// product, RIT Research Labs should be given attribution as the RIT Research
// Labs of the parts of the library used. This can be in the form of a textual
// message at program startup or in documentation (online or textual)
// provided with the package.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// "Based on Dos Navigator by RIT Research Labs."
//
// THIS SOFTWARE IS PROVIDED BY RIT RESEARCH LABS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The licence and distribution terms for any publically available
// version or derivative of this code cannot be changed. i.e. this code
// cannot simply be copied and put under another distribution licence
// (including the GNU Public Licence).
//
//////////////////////////////////////////////////////////////////////////}
unit ComLnkIO;
{$I LINK.INC} interface
const
TCL_TimerSz = 8;
type
TCL_Timer = array[1..TCL_TimerSz] of byte;
function CL_OutBuffFree(APort: Pointer): Word;
function CL_OutBuffUsed(APort: Pointer): Word;
function CL_GetBlock(APort: Pointer; var Buf; Size: Word): Word;
procedure CL_PutBlock(APort: Pointer; const Buf; Size: Word);
function CL_InitCOM: Pointer;
procedure CL_DoneCOM(APort: Pointer);
function CL_DCD(APort: Pointer): Boolean;
procedure CL_NewTimer(var Timer: TCL_Timer; Tics : LongInt);
procedure CL_ClearTimer(var Timer: TCL_Timer);
function CL_TimerExpired(const Timer: TCL_Timer): Boolean;
function CL_TimerInstalled(const Timer: TCL_Timer): Boolean;
function CL_GetCRC32ofs: Word;
{$IFDEF NLSLAVE}
var
_Speed: LongInt;
_ComNum: Byte;
{$ENDIF}
implementation uses
{$IFDEF DN} ModemIO, {$ENDIF}
{$IFDEF OS2}
CrtOS2,
OS2Def,
DosTypes,
DosProcs,
OS2Timer
{$ELSE}
apFossil,
ooCOM,
apPort,
apMisc,
xTime
{$ENDIF} ;
{$IFDEF OS2} {$I OS2IO.INC} {$ENDIF}
function CL_GetCRC32ofs: Word;
begin
CL_GetCRC32ofs := {$IFDEF OS2} Ofs(CRC32Table)
{$ELSE} CRC32TableOfs {$ENDIF};
end;
function CL_OutBuffFree;
{$IFDEF OS2}
var
StatStruc : record NumBytes, BufSize: Word end;
begin
rc := DosDevIOctl(StatStruc, (nil)^, ASYNC_GETOUTQUECOUNT, IOCTL_ASYNC, hfCOM);
if rc <> NO_ERROR then Usage;
with StatStruc do
begin
{WriteLn('NumBytes = ', NumBytes,', BufSize = ',BufSize);}
CL_OutBuffFree := BufSize - NumBytes;
end;
end;
{$ELSE}
begin CL_OutBuffFree := AbstractPortPtr(APort)^.OutBuffFree end;
{$ENDIF}
function CL_OutBuffUsed;
{$IFDEF OS2}
var
StatStruc : record NumBytes, BufSize: Word end;
begin
rc := DosDevIOctl(StatStruc, (nil)^, ASYNC_GETOUTQUECOUNT, IOCTL_ASYNC, hfCOM);
if rc <> NO_ERROR then Usage;
CL_OutBuffUsed := StatStruc.NumBytes;
end;
{$ELSE}
begin CL_OutBuffUsed := AbstractPortPtr(APort)^.OutBuffUsed end;
{$ENDIF}
procedure CL_PutBlock;
{$IFDEF OS2}
var
Actually: Word;
begin
DosWrite(hfCOM, (@Buf)^, Size, Actually);
end;
{$ELSE}
var Dummy : Word;
begin
AbstractPortPtr(APort)^.PutBlockDirect(Addr(Buf)^, Size, Dummy);
end;
{$ENDIF}
function CL_GetBlock;
{$IFDEF OS2}
var Actual: Word;
begin
DosRead(hfCOM, Buf, Size, Actual);
CL_GetBlock := Actual;
end;
{$ELSE}
var Actual : Word;
begin
AbstractPortPtr(APort)^.GetBlockDirect(Buf, Size, Actual, []);
CL_GetBlock := Actual;
end;
{$ENDIF}
{$IFNDEF DN}
procedure Usage;
begin
WriteLn('Usage: NLslave <COMn> [115200] [FOSSIL]');
WriteLn(' valid ports are COM1-COM8');
WriteLn(' valid BPS are 300, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600,');
WriteLn(' and 115200 for Registered Users Only');
WriteLn(' valid interfaces are DEFAULT, FOSSIL and DIGI14');
Halt;
end;
{$ENDIF}
function CL_DCD;
begin
CL_DCD := AbstractPortPtr(APort)^.CheckDCD;
end;
function CL_InitCOM;
{$IFDEF DN}
begin
CL_InitCOM := COMport;
end;
{$ELSE}
{$IFDEF OS2}
{ ------------------------------ OS/2 ------------------------------ }
var
ctrl: packed record
D, P, S: Byte;
end;
var
s: string;
sp: LongInt;
er: Integer;
procedure UpStr;var I:Byte;begin for I:=1 to Length(S) do S[I] := UpCase(S[I]) end;
begin
CL_InitCOM := nil;
s := ParamStr(1); UpStr;
if (Length(s)<>4) or (Copy(s,1,3)<>'COM') or (not (s[4] in ['1'..'8'])) then
begin
WriteLn('Illegal COM Port - '+s); Usage;
end;
s[Length(s)+1] := #0;
rc := DosOpen(@s[1], hfCOM, ComAction, 0, FILE_NORMAL, FILE_OPEN,
OPEN_ACCESS_READWRITE or OPEN_SHARE_DENYNONE, 0);
if rc = NO_ERROR then CL_InitCOM := @hfCOM else
begin
WriteLn('Failed to open '+s); Usage;
end;
s := ParamStr(2);
Val(s, sp, er);
if er>0 then
begin
WriteLn('Illegal COM Speed - '+s); Usage;
end;
WriteLn('Initializing ',s);
rc := DosDevIOctl((nil)^, sp, ASYNC_SETBAUDRATE, IOCTL_ASYNC, hfCOM);
if rc <> NO_ERROR then
begin
WriteLn('Failed to set baudrate '+s); Usage;
end;
with ctrl do begin D:=8;P:=0;S:=0 end;
rc := DosDevIOctl((nil)^, ctrl, ASYNC_SETLINECTRL, IOCTL_ASYNC, hfCOM);
if rc <> NO_ERROR then
begin
WriteLn('Failed to port characteristics of '+s); Usage;
end;
end;
{$ELSE}
{ ------------------------------ DOS ------------------------------ }
type
Tp = (tDefault, tFossil, tDigi);
const
bs = 16384;
var
s: string;
er: Integer;
p: Pointer;
optn : Word;
t: tp;
procedure UpStr;var I:Byte;begin for I:=1 to Length(S) do S[I] := UpCase(S[I]) end;
begin
if ParamCount < 1 then Usage;
InitApMisc;
s := ParamStr(1); UpStr;
if (Length(s)<>4) or (Copy(s,1,3)<>'COM') or (not (s[4] in ['1'..'8'])) then
begin
WriteLn('Illegal COM Port - '+s); Usage;
end;
_ComNum := byte(s[4])-byte('0');
if ParamCount > 1 then
begin
s := ParamStr(2);
Val(s, _Speed, er);
if er>0 then
begin
WriteLn('Illegal COM Speed - '+s); Usage;
end;
end else _Speed := 115200;
{FossilPortPtr}
if ParamCount = 3 then
begin
s := ParamStr(3); UpStr;
if s = 'DEFAULT' then t := tDefault else
if s = 'FOSSIL' then t := tFossil else
if s = 'DIGI14' then t := tDigi else
begin
WriteLn('Illegal interface - '+s); Usage;
end;
end else t := tDefault;
optn := ptReturnPartialGets +
ptExecutePartialPuts +
ptReturnDelimiter +
ptDropModemOnClose +
ptRaiseModemOnOpen +
ptTrueOutBuffFree +
ptRestoreOnClose;
case t of
tDefault : p := New(UARTPortPtr, InitCustom(ComNameType(_ComNum-1), _Speed, NoParity, 8, 1, bs, bs, optn));
tFossil : p := New(FossilPortPtr, InitCustom(ComNameType(_ComNum-1), _Speed, NoParity, 8, 1, bs, bs, optn));
tDigi : p := New(Digi14PortPtr, InitCustom(ComNameType(_ComNum-1), _Speed, NoParity, 8, 1, bs, bs, optn));
end;
if p = nil then
begin
WriteLn('Can''t initialize COM port!!!');
end;
CL_InitCOM := p;
end;
{$ENDIF} {-NLSLAVE-}
{$ENDIF DN}
{ ================================================================= }
procedure CL_DoneCOM;
{$IFDEF DN}
begin end;
{$ELSE}
{$IFDEF OS2}
begin
rc := DosClose(HFILE(APort^));
end;
{$ELSE}
begin
Dispose(AbstractPortPtr(APort), Done);
end;
{$ENDIF}
{$ENDIF DN}
procedure CL_NewTimer;
begin
NewTimer(TEventTimer(Timer), Tics);
end;
function CL_TimerExpired;
begin
CL_TimerExpired := TimerExpired(TEventTimer(Timer));
end;
procedure CL_ClearTimer;
begin
FillChar(Timer, TCL_TimerSz, 0);
end;
function CL_TimerInstalled;
var
ET : TEventTimer absolute Timer;
begin
CL_TimerInstalled := (ET.StartTics<>0) or (ET.ExpireTics<>0);
end;
end.
aptimer
dosprocs
dostypess