并口(打印机接口)应该不能进行8位通信,唯一的通信方法是用$37A的五位信号
线来传输数据,五位信号线有一位用于控制,故只能用四位信号线传输数据,下
面是一段传输程序,该程序未进行过调试,但可供参考:
我的地址是:
E-Mail: lyd_yl@163.net
Tel: 1361206862
Function OpenLPT(LPTNo:Byte;OutSize,InSize:SmallInt):Integer;
Begin
Try
GetMem(inBuff,InSize);
Except
Result:=-1;
Exit;
End;
Try
GetMem(OutBuff,OutSize);
Except
FreeMem(inBuff,InSize);
Result:=-1;
Exit;
End;
Result:=1;
End;
Function InitLPT:Boolean;
Begin
Asm
Mov Dx,StatePort
In Al,Dx
And Al,CanSend
Shr Al,7
Mov @Result,Al
End;
End;
Function SetLPTPort(Port:Byte):Boolean;
Begin
if Port=1 Then
Begin
outDataPort:=$378;
StatePort:=$379;
Result:=True;
End
Else if Port=2 Then
Begin
outDataPort:=$278;
StatePort:=$279;
Result:=True;
End
Else Result:=False;
End;
Procedure SendState(State:Boolean);
Var
TempVar:Byte;
Begin
Asm
Mov Dx,outDataPort {得到数据位第五位的值}
In Al,Dx
Mov TempVar,Al
End;
if State Then TempVar:=TempVar And $7f {如果状态为真(能发送数据)将第五位设置}
Else TempVar:=TempVar Or $80; {为0,反之设置为1,反向的原因是BUSY为低电}
Asm {平有效}
Mov Dx,outDataPort
Mov Al,TempVar
Out Dx,Al
End;
End;
Function GetLPTState:Boolean;
Begin
Asm
Mov Dx,StatePort
In Al,Dx
Test Al,$80
Jnz @CanSend
Mov @Result,0
Jmp @Exit
@CanSend:
Mov @Result,1
@Exit:
End;
End;
Function SendData(Data:Byte):Boolean;
Begin
Asm
Mov Dx,StatePort
Mov Bx,0
@Delay:
Dec Bx
Jnz @CheckState
Mov @Result,0
Jmp @Exit
@CheckState:
In Al,Dx
Test Al,CanSend
Jnz @Delay
Mov Al,Data
Shr Al,4
Mov Dx,outDataPort
Out Dx,Al
Mov Dx,StatePort
Mov Bx,0
@Delay1:
Dec Bx
Jnz @CheckState1
Mov @Result,0
Jmp @Exit
@CheckState1:
In Al,Dx
Test Al,CanSend
Jz @Delay1
Mov Dx,outDataPort
Mov Al,$10
Out Dx,Al
Mov Bx,0
Mov Dx,StatePort
@Delay2:
Dec Bx
Jnz @CheckState2
Mov @Result,0
Jmp @Exit
@CheckState2:
In Al,Dx
Test Al,CanSend
Jnz @Delay2
Mov Dx,outDataPort
Mov Al,Data
And Al,$0f
Out Dx,Al
Mov Bx,0
Mov Dx,StatePort
@Delay3:
Dec Bx
Jnz @CheckState3
Mov @Result,0
Jmp @Exit
@CheckState3:
In Al,Dx
Test Al,CanSend
Jz @Delay3
Mov @Result,1
@Exit:
Mov Al,$10
Mov Dx,outDataPort
Out Dx,Al
Mov Bx,0
Mov Dx,StatePort
@Delay4:
Dec Bx
Jz @Return
In Al,Dx
Test Al,CanSend
Jnz @Delay4
@Return:
End;
End;
Function GetData:SmallInt;
Begin
Asm
Mov Dx,StatePort
In Al,Dx
Test Al,CanSend
Jz @NoRecive
Shr Al,3
Mov Cl,Al
Shl Cl,4
Xor Al,Al
Mov Dx,outDataPort
Out Dx,Al
Mov Bx,0
Mov Dx,StatePort
@Wait:
Dec Bx
Jz @NoRecive
In Al,Dx
Test Al,CanSend
Jnz @Wait
Mov Al,$10
Mov Dx,outDataPort
Out Dx,Al
Mov Bx,0
Mov Dx,StatePort
@Wait1:
Dec Bx
Jnz @Wait1
In Al,Dx
Test Al,CanSend
Jz @Wait1
Shr Al,3
And Al,$0f
Or Cl,Al
Xor Al,Al
Mov Dx,outDataPort
Out Dx,Al
Mov Bx,0
Mov Dx,StatePort
@Wait2:
Dec Bx
Jz @Recived
In Al,Dx
Test Al,CanSend
Jnz @Wait2
@Recived:
Xor Ch,Ch
Mov @Result,Cx
Jmp @Exit
@NoRecive:
Mov @Result,-1
@Exit:
Mov Al,$10
Mov Dx,outDataPort
Out Dx,Al
End;
End;
Function ReciveSingleByte:Integer;
Begin
Asm
Call timeGetTime
Add Ax,DelayTime
Adc Dx,0
Push Ax
Push Dx
Jmp @WaitSendByte
@Delay1:
Call timeGetTime
Pop Cx
Pop Bx
Push Cx
Push Bx
Sub Ax,Cx
Sbb Dx,Bx
Jb @WaitSendByte
Pop Bx
Pop Bx
Mov Ax,-1
Jmp @Exit
@WaitSendByte:
Mov Dx,StatePort
In Al,Dx
Test Al,ACKSignal
Jz @Delay1
Pop Bx
Pop Bx
Mov Dx,outDataPort
In Al,Dx
Xor Ah,Ah
Mov @Result,Ax
Mov Dx,outStatePort
Mov Al,SendSignal
Out Dx,Al
Call timeGetTime
Add Ax,DelayTime
Adc Dx,0
Push Ax
Push Dx
Jmp @WaitClearSendSignal
@Delay2:
Call timeGetTime
Pop Cx
Pop Bx
Push Cx
Push Bx
Sub Ax,Cx
Sbb Dx,Bx
Jb @WaitClearSendSignal
Pop Bx
Pop Bx
Mov Ax,-1
Jmp @Exit
@WaitClearSendSignal:
Mov Dx,StatePort
In Al,Dx
Test Al,ACKSignal
Jnz @WaitClearSendSignal
@Exit:
End;
End;
Function SendSingleByte(SendByte:Byte):Boolean;
Begin
Asm
Mov Dx,OutDataPort
Mov Al,SendByte
Out Dx,Al
Mov Dx,OutStatePort
Mov Al,SendSignal
Out Dx,Al
Call timeGetTime
Add Ax,DelayTime
Adc Dx,0
Push Ax
Push Dx
Jmp @WaitReciveACK
@Delay1:
Call timeGetTime
Pop Cx
Pop Bx
Push Cx
Push Bx
Sub Ax,Cx
Sbb Dx,Bx
Jb @WaitReciveACK
Pop Bx
Pop Bx
Mov Ax,0
Jmp @Exit
@WaitReciveACK:
Mov Dx,StatePort
In Al,Dx
Test Al,ACKSignal
Jz @Delay1
Pop Bx
Pop Bx
Mov Dx,outStatePort
Mov Al,SendSignal
Not Al
Out Dx,Al
Call timeGetTime
Add Ax,DelayTime
Adc Dx,0
Push Ax
Push Dx
Jmp @WaitClearACK
@Delay2:
Call timeGetTime
Pop Cx
Pop Bx
Push Cx
Push Bx
Sub Ax,Cx
Sbb Dx,Bx
Jb @WaitClearACK
Pop Bx
Pop Bx
Mov Ax,0
Jmp @Exit
@WaitClearACK:
Mov Dx,StatePort
In Al,Dx
Test Al,ACKSignal
Jnz @Delay2
Pop Bx
Pop Bx
Mov Ax,1
@Exit:
End;
End;
Procedure ReadInTurn;
Var
Msg:TMsg;
CurrState:Byte;
TempBuf:Integer;
DelayTimes1,DelayTimes:LongInt;
Begin
if RunRecive Then
Begin
Exit;
End
Else RunRecive:=True;
While True Do
Begin
if PeekMessage(Msg,0,0,0,PM_REMOVE) Then
Begin
if Msg.wParam=27 Then
Begin
RunRecive:=False;
Exit;
End;
TranslateMessage(Msg);
DispatchMessage(Msg);
End
Else
Begin
Asm
Mov Dx,StatePort
In Al,Dx
And Al,$80
Mov CurrState,Al
End;
if CurrState<>0 Then
Begin
TempBuf:=GetData;
if TempBuf=-1 Then
Begin
MessageBox(0,'通信线路错误!','提示信息',MB_OK);
End;
End;
End;
End;
End;