?? tcomm1.pas
字號:
//指定新值
dcb.BaudRate := tmpValue;
dcb.Flags := 1; //必須指定為1
dcb.Parity := Ord( FParity );//Parity的指定
FParityCheck:=False;
if Ord(FParity)<>0 then FParityCheck:=True;
if FParityCheck then
dcb.Flags := dcb.Flags or dcb_ParityCheck; // Enable parity check
// 設置硬件流量控制
Case FHwHandShaking of
hhNone:;
hhNoneRTSON:
dcb.Flags := dcb.Flags or dcb_RTSControlEnable;
hhRTSCTS:
dcb.Flags := dcb.Flags or dcb_RTSControlHandShake or dcb_OutxCtsFlow;
end;
//設置軟件流量控制
Case FSwHandShaking of
shNone:;
shXonXoff:
dcb.Flags := dcb.Flags or dcb_OutX or dcb_InX;
end;
//設置數據位數
dcb.ByteSize := Ord( FDataBits ) + 5;
//設置停止位數
dcb.StopBits := Ord( FStopBits );
//將設置寫入
SetCommState( hComm, dcb )
end;
procedure TComm.SetPortOpen(b:Boolean);
begin
if b then //若指定打開通信端口,則…
begin
if FPortOpen then
begin
MessageDlg('COM Port has been opened!', mtError, [mbOK], 0);
exit;
end; //FportOpen loop
OpenComm; //打開通信端口
exit;
end; //b loop
CloseComm;
end;
//指定傳輸速度
procedure TComm.SetBaudRate( Rate : TBaudRate );
begin
if Rate = FBaudRate then
Exit;
FBaudRate := Rate;
if hComm <> 0 then
_SetCommState
end;
//硬件流量控制
procedure TComm.SetHwHandShaking( c: THwHandShaking);
begin
if c = FHwHandShaking then
Exit;
FHwHandShaking := c;
if hComm <> 0 then
_SetCommState
end;
//軟件交握指定
procedure TComm.SetSwHandShaking( c : TSwHandShaking );
begin
if c = FSwHandShaking then
Exit;
FSwHandShaking := c;
if hComm <> 0 then
_SetCommState
end;
//設置數據位數
procedure TComm.SetDataBits( Size : TDataBits );
begin
if Size = FDataBits then
Exit;
FDataBits := Size;
if hComm <> 0 then
_SetCommState
end;
//設置極性檢查方式
procedure TComm.SetParity( p : TParity );
begin
if p = FParity then
Exit;
FParity := p;
if hComm <> 0 then
_SetCommState
end;
//設置停止位
procedure TComm.SetStopBits( Bits : TStopBits );
begin
if Bits = FStopBits then
Exit;
FStopBits := Bits;
if hComm <> 0 then
_SetCommState
end;
//讀取CD狀態
function TComm.ReadCDHolding():Boolean;
begin
Result:=FCDHolding;
end;
//讀取DSR狀態
function TComm.ReadDSRHolding():Boolean;
begin
Result:=FDSRHolding;
end;
//讀取RI狀態
function TComm.ReadRIHolding():Boolean;
begin
Result:=FRIHolding;
end;
//讀取CTS狀態
function TComm.ReadCTSHolding():Boolean;
begin
Result:=FCTSHolding;
end;
//設置DTR狀態
procedure TComm.SetDTRStatus(b:Boolean);
begin
if hComm=0 then exit ;
FDTR:=b;
if b then
EscapeCommFunction(hComm,SETDTR) //將DTR升至高電壓
else
EscapeCommFunction(hComm,CLRDTR);//將DTR降至低電壓
end;
//設置RTS狀態
procedure TComm.SetRTSStatus(b:Boolean);
begin
if hComm=0 then
begin
ECommError.Create('COM Port is not opened yet!');
exit ;
end;
FRTS:=b;
if b then
EscapeCommFunction(hComm,SETRTS) //將RTS升至高電壓
else
EscapeCommFunction(hComm,CLRRTS); //將RTS降至低電壓
end;
//返回數據
function TComm.ReadInputData():String;
begin
if hComm=0 then
begin
ECommError.Create('COM Port is not opened yet!');
end;
//決定每一次的指令要返回多少的字符(以Byte為單位)
ReadProcess;
Result:=FInputData;
end;
//返回數據
function TComm.ReadInputByte(var AP:PByte):DWORD;
begin
if hComm=0 then
begin
ECommError.Create('COM Port is not opened yet!');
end;
ReadProcess;//執行讀取函數
AP:= @FInputByteData[0];//取得數據地址
Result:=High(FInputByteData);//取得數據數組的最高索引值
end;
//讀取數據的字節數
function TComm.ReadInDataCount():DWORD;
var
CS: TCOMSTAT;
dwCommError:DWORD;
begin
ClearCommError(hComm,dwCommError,@CS); //取得狀態
Result:=CS.cbInQue;
end;
//清空數據緩沖區
procedure TComm.SetInDataCount(StrNO:DWORD);
begin
if StrNo<>0 then exit ;
PurgeComm(hComm, PURGE_RXCLEAR) // 清除COM 數據
end;
//線路狀態的數值
function TComm.ReadCommEvent():DWORD;
begin
Result:=FCommEvent;
end;
//錯誤狀態值的讀取
function TComm.ReadCommError():DWORD;
begin
Result:=FCommError;
end;
//設置引發接收事件的閥值
procedure TComm.SetRThreshold(RTNo:DWORD);
begin
FRThreshold:=RTNo;
end;
//以下是實際的讀取動作
Procedure TComm.ReadProcess;
var
nBytesRead: DWORD;
dwCommError: DWORD;
CS: TCOMSTAT;
i,ReadLen: DWORD;
begin
//使用ClearCommError得知有多少的數據在緩沖區中
//并得知錯誤種類
ClearCommError(hComm,dwCommError,@CS); //取得狀態
FCommError:=dwCommError; //錯誤數值
if cs.cbInQue <>0 then //若緩沖區有數據,則讀取
begin
if InputLen=0 then //指定讀取的數據數
ReadLen:=cs.cbInQue
else
ReadLen:=InputLen;
if cs.cbInQue > sizeof(szInputBuffer) then
PurgeComm(hComm, PURGE_RXCLEAR) // 清除COM 數據
else
begin
//讀取數據
if ReadFile(hComm, szInputBuffer,ReadLen,nBytesRead,nil) then // 接收COM 的數據
begin
//取出數據
FInputData:=Copy(szInputBuffer,1,ReadLen);
//設置字節數組長度
SetLength(FInputByteData,ReadLen);
//將數據搬到數組中
for i:=0 to ReadLen-1 do
FInputByteData[i]:=ord(szInputBuffer[i]);
end; //ReadFile Loop
end;//else Loop
end; //cs.binQue Loop
end;
//取得線路的狀態
procedure TComm.GetModemState;
var
dwModemState : DWORD;
begin
if hComm=0 then
begin
raise ECommError.Create('COM Port is not opened yet!');
end;
//取得線路狀態
FCommEvent:=0;
if GetCommModemStatus( hComm, dwModemState ) then
begin
//判斷CD狀態
if (dwModemState and MS_RLSD_ON)=MS_RLSD_ON then
begin
if not FCDHolding then FCommEvent:= EV_RLSD;
FCDHolding:=True;
end
else
begin
if FCDHolding then FCommEvent:= EV_RLSD;
FCDHolding:=False;
end;
//判斷DSR狀態
if (dwModemState and MS_DSR_ON)=MS_DSR_ON then
begin
if not FDSRHolding then FCommEvent:=FCommEvent + EV_DSR;
FDSRHolding:=True;
end
else
begin
if FDSRHolding then FCommEvent:=FCommEvent + EV_DSR;
FDSRHolding:=False;
end;
//判斷RI狀態
if (dwModemState and MS_RING_ON)=MS_RING_ON then
begin
if not FRIHolding then FCommEvent:=FCommEvent + EV_RING;
FRIHolding:=True;
end
else
begin
if FRIHolding then FCommEvent:=FCommEvent + EV_RING;
FRIHolding:=False;
end;
//判斷CTS狀態
if (dwModemState and MS_CTS_ON)=MS_CTS_ON then
begin
if not FCTSHolding then FCommEvent:=FCommEvent + EV_CTS;
FCTSHolding:=True;
end
else
begin
if FCTSHolding then FCommEvent:=FCommEvent + EV_CTS;
FCTSHolding:=False;
end;
end;
end;
procedure Register;
begin
RegisterComponents('System', [TComm])
end;
//組件的定時器程序,在此會決定事件是否被觸發
procedure TComm.ProcTimer(Sender: TObject);
var
tmpValue: DWORD;
dwCommError:DWORD;
CS: TCOMSTAT;
begin
if hComm=0 then exit;
//若設置讀取的字符數,檢查并觸發事件
ClearCommError(hComm,dwCommError,@CS); //取得狀態
FCommError:=dwCommError; //錯誤數值
if FRThreshold>0 then
begin
if cs.cbInQue >=FRthreshold then
ReceiveData();
end;
GetModemState;
Application.ProcessMessages; //看有無其它的指令需執行,以免鎖住
//檢查線路狀態是否發生改變,若改變則觸發事件
tmpValue:=ReadCommEvent;
if tmpValue<>0 then ModemStateChange(tmpValue);
Application.ProcessMessages; //看有無其它的指令需執行,以免鎖住
//若發生錯誤,則引發錯誤
tmpValue:=ReadCommError;
if tmpValue<>0 then ReceiveError(tmpValue);
Application.ProcessMessages; //看有無其它的指令需執行,以免鎖住
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -