一个例子:
{****************************************************************************}
{* *}
{* Procedure ComParams(ComPort:Byte; Baud:Longint; *}
{* WordSize:Byte; Parity:Char; StopBits:Byte); *}
{* *}
{* ComPort:Byte -> Port # to initialize. Must be (1 - C_MaxCom) *}
{* Procedure aborted if port # invalid or unopened. *}
{* Baud:Longint -> Desired baud rate. Should be (C_MinBaud - C_MaxBaud)*}
{* C_MinBaud or C_MaxBaud used if out of range. *}
{* WordSize:Byte -> Word size, in bits. Must be 5 - 8 bits. *}
{* 8-bit word used if out of range. *}
{* Parity:Char -> Parity classification. *}
{* May be N)one, E)ven, O)dd, M)ark or S)pace. *}
{* N)one selected if classification unknown. *}
{* StopBits:Byte -> # of stop bits to pad character with. Range (1-2) *}
{* 1 stop bit used if out of range. *}
{* *}
{* ComParams is used to configure an OPEN'ed port for the desired comm- *}
{* unications parameters, namely baud rate, word size, parity form and *}
{* # of stop bits. A call to this procedure will set up the port approp- *}
{* riately, as well as assert the DTR, RTS and OUT2 control lines and *}
{* clear all buffers. *}
{* *}
{****************************************************************************}
Procedure ComParams(ComPort:Byte; Baud:LongInt; WordSize:Byte; Parity:Char; StopBits:Byte);
Const
C_Stopbit1 = $00; { Bit masks for parity, stopbits }
C_Stopbit2 = $04;
C_NoParity = $00;
C_OddParity = $08;
C_EvenParity = $18;
C_MarkParity = $28;
C_SpaceParity = $38;
Const
C_IER = 1; { 8250 register offsets }
C_IIR = 2;
C_LCR = 3;
C_MCR = 4;
C_LSR = 5;
C_MSR = 6;
C_SCR = 7;
Var
X : Real;
Y,P : Word;
DivMSB,DivLSB : Byte;
WS,SB,PTY : Byte;
Begin
If (ComPort<1) Or (ComPort>C_MaxPort) Or (Not C_PortOpen[ComPort]) Then Exit;
Inline($FA);
P := C_PortAddr[ComPort];
{ Calculate baud rate divisors }
X := Baud;
If X < C_MinBaud Then X := C_MinBaud;
If X > C_MaxBaud Then X := C_MaxBaud;
Y := Round($900/(X/50));
DivMSB := Hi(Y);
DivLSB := Lo(Y);
{ Determine parity mask }
{ Default if unknown: No parity }
Case UpCase(Parity) Of
'N' : PTY := C_NoParity;
'E' : PTY := C_EvenParity;
'O' : PTY := C_OddParity;
'M' : PTY := C_MarkParity;
'S' : PTY := C_SpaceParity;
Else
PTY := C_NoParity;
End;
{ Determine stop-bit mask }
{ Default if out of range: 1 Stop bit }
Case StopBits Of
1 : SB := C_StopBit1;
2 : SB := C_StopBit2;
Else
SB := C_StopBit1;
End;
{ Determine word-size mask }
{ Default if out of range: 8 bit word size }
If (WordSize >= 5) And (WordSize <= 8) Then
WS := WordSize - 5
Else
WS := 3;
{ Initialize line-control register }
Y := Port+ Port[P+C_LSR];
Port[P+C_LCR] := WS + SB + PTY;
{ Initialize baud rate divisor latches }
Port[P+C_LCR] := Port[P+C_LCR] Or $80;
Port:= DivLSB;
Port[P+1] := DivMSB;
Port[P+C_LCR] := Port[P+C_LCR] And $7F;
X := Port + Port[P+C_LSR] + Port[P+C_MSR] + Port[P+C_IIR];
{ Assert RS323 control lines (DTR,RTS,OUT2) & exit }
Port[P+C_MCR] := $0B;
ClearCom(ComPort,'B');
Inline($FB);
End;
[此贴子已经被作者于2005-5-30 16:46:00编辑过]
|