串口通訊的串口類模塊.doc_第1頁
串口通訊的串口類模塊.doc_第2頁
串口通訊的串口類模塊.doc_第3頁
串口通訊的串口類模塊.doc_第4頁
串口通訊的串口類模塊.doc_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

本模塊開發(fā)平臺BCB,用于串口通訊的串口類模塊,不需要使用系統(tǒng)或第三方帶的串口Com組件,不需要注冊,直接加入到軟件聲明中即可使用。Comm.h /頭文件聲明/-#ifndef CommH#define CommH/-#include #include #include #include #include #include /類型定義 enum TBaudRate br110=110, br300=300, br600=600, br1200=1200, br2400=2400, br4800=4800, br9600=9600, br14400=14400, br19200=19200, br38400=38400, br56000=56000, br57600=57600, br115200=115200; enum TComPortNumber pnCOM1, pnCOM2, pnCOM3, pnCOM4, pnCOM5, pnCOM6, pnCOM7, pnCOM8, pnCOM9, pnCOM10, pnCOM11, pnCOM12, pnCOM13, pnCOM14, pnCOM15, pnCOM16; enum TParity None, Odd, Even, Mark, Space ; enum TStopBits SB1, SB1_5, SB2 ; enum TDataBits DB5, DB6, DB7, DB8 ; enum THwHandShakinghhNone,hhNoneRTSON,hhRTSCTS; enum TSwHandShakingshNone,shXonXoff; / 輸入緩沖區(qū)的默認大小 const long INPUTBUFFERSIZE = 4096; / Line Status位定義 const int ME_CTS = 1; const int ME_DSR = 2; const int ME_RING = 4; const int ME_RLSD = 8; /DCB 位定義 const long dcb_Binary = 0x00000001; const long dcb_ParityCheck = 0x00000002; const long dcb_OutxCtsFlow = 0x00000004; const long dcb_OutxDsrFlow = 0x00000008; const long dcb_DtrControlMask = 0x00000030; const long dcb_DtrControlDisable = 0x00000000; const long dcb_DtrControlEnable = 0x00000010; const long dcb_DtrControlHandshake = 0x00000020; const long dcb_DsrSensivity = 0x00000040; const long dcb_TXContinueOnXoff = 0x00000080; const long dcb_OutX = 0x00000100; const long dcb_InX = 0x00000200; const long dcb_ErrorChar = 0x00000400; const long dcb_NullStrip = 0x00000800; const long dcb_RtsControlMask = 0x00003000; const long dcb_RtsControlDisable = 0x00000000; const long dcb_RtsControlEnable = 0x00001000; const long dcb_RtsControlHandShake = 0x00002000; const long dcb_RtsControlToggle = 0x00003000; const long dcb_AbortOnError = 0x00004000; const long dcb_Reserveds = 0xFFFF8000; /事件函數(shù)定位器聲明 typedef void _fastcall (_closure *TReceiveDataEvent)(System:TObject *Sender); typedef void _fastcall (_closure *TReceiveErrorEvent)(System:TObject *Sender, DWORD EventMask); typedef void _fastcall (_closure *TModemStateChangeEvent)(System:TObject *Sender, DWORD ModemEvent);/-class PACKAGE TComm : public TComponent private: TTimer *CommTimer; /組件用的定時器 char szInputBufferINPUTBUFFERSIZE; HANDLE hComm; TComPortNumber FCommPort; bool FPortOpen; TBaudRate FBaudRate; bool FParityCheck; THwHandShaking FHwHandShaking; TSwHandShaking FSwHandShaking; TDataBits FDataBits; TParity FParity; TStopBits FStopBits; int FInputLen; /每次執(zhí)行Input時所讀取的字符串長度 int FRThreshold; /設(shè)置引發(fā)接收事件的門坎 bool FDTR; bool FRTS; String FInputData; DynamicArray FInputByteData;/ FInputByteData: array of Byte; int FCommEvent; int FCommError; bool FCDHolding; bool FCTSHolding; bool FDSRHolding; bool FRIHolding; /設(shè)置函數(shù) void _fastcall SetCommPort(TComPortNumber Port); /設(shè)置COMPort void _fastcall SetBaudRate(TBaudRate Rate); /設(shè)置速率 void _fastcall SetHwHandShaking(THwHandShaking c ); /硬件交握 void _fastcall SetSwHandShaking(TSwHandShaking c ); /軟件交握 THwHandShaking _fastcall GetHwHandShaking(void); /硬件交握 TSwHandShaking _fastcall GetSwHandShaking(void); /軟件交握 void _fastcall SetDataBits(TDataBits Size); /數(shù)據(jù)位數(shù) void _fastcall SetParity(TParity p); /奇偶校驗 void _fastcall SetStopBits(TStopBits Bits); /停止位 void _fastcall SetInDataCount(DWORD StrNo); /設(shè)成0表示清除FInputData void _fastcall SetRThreshold(int RTNo); /接收門坎 void _fastcall SetPortOpen(bool b); /打開通信端口 void _SetCommState(void); /設(shè)置通信參數(shù) bool _fastcall GetRTSStatus(); bool _fastcall GetDTRStatus(); void _fastcall SetDTRStatus(bool b); /DTR 狀態(tài) void _fastcall SetRTSStatus(bool b); /RTS狀態(tài) void _fastcall ReadProcess(void); /讀取數(shù)據(jù)函數(shù) void _fastcall EventProcess(void); /事件檢測函數(shù) void _fastcall GetModemState(void); /線路狀態(tài)檢測函數(shù) void OpenComm(void); /打開通訊端口函數(shù) void _fastcall CloseComm(void); /開關(guān)通信端口函數(shù) int _fastcall ReadCommEvent(void); /硬件線路狀態(tài)值讀取 int _fastcall ReadCommError(void); /錯誤狀態(tài)值的讀取 String _fastcall ReadInputData(void); /返回收到的數(shù)據(jù) int _fastcall ReadInDataCount(void); /讀取有多少數(shù)據(jù) bool _fastcall ReadCDHolding(void); /取得CD線路狀態(tài) bool _fastcall ReadDSRHolding(void); /取得DSR線路狀態(tài) bool _fastcall ReadRIHolding(void); /取得RI線路狀態(tài) bool _fastcall ReadCTSHolding(void); /取得CTS線路狀態(tài) /事件 TReceiveDataEvent FOnReceiveData; TReceiveErrorEvent FOnReceiveError; TModemStateChangeEvent FOnModemStateChange; protected: void _fastcall ProcTimer(TObject *Sender); void _fastcall ReceiveData(void); void _fastcall ReceiveError(DWORD EvtMask); void _fastcall ModemStateChange(DWORD ModemEvent); public: _fastcall TComm(TComponent* Owner); /構(gòu)造函數(shù) bool OutputString(String DataToWrite); bool OutputByte(const DynamicArray ByteData); DynamicArray ReadInputByte(void); _property bool PortOpen =read = FPortOpen, write=SetPortOpen,default=false; _property DWORD DataCount = read = ReadInDataCount , write = SetInDataCount; _property bool DTREnabled = read = GetDTRStatus, write = SetDTRStatus; _property bool RTSEnabled = read = GetRTSStatus, write = SetRTSStatus; _published: /以下加入屬性 _property TComPortNumber CommPort = read = FCommPort, write = SetCommPort, default = pnCOM1; _property TBaudRate BaudRate = read = FBaudRate, write = SetBaudRate, default = br9600; _property THwHandShaking HwHandShaking = read = GetHwHandShaking, write = SetHwHandShaking, default = hhNone; _property TSwHandShaking SwHandShaking = read = GetSwHandShaking, write = SetSwHandShaking, default = shNone; _property TDataBits DataBits = read = FDataBits ,write =SetDataBits,default = DB8 ; _property TParity Parity = read = FParity ,write = SetParity,default = None; _property TStopBits StopBits = read = FStopBits ,write = SetStopBits,default =SB1; _property DWORD CommEvent = read = ReadCommEvent; _property DWORD CommError = read = ReadCommError; _property String Input = read = ReadInputData; _property int InputLen = read = FInputLen , write = FInputLen, default =0; _property int RThreshold = read = FRThreshold ,write = SetRThreshold, default =0 ; _property bool CDHolding = read = ReadCDHolding; _property bool DSRHolding = read = ReadDSRHolding; _property bool RIHolding = read = ReadRIHolding; _property bool CTSHolding = read = ReadCTSHolding; /事件 _property TReceiveDataEvent OnReceiveData = read = FOnReceiveData , write = FOnReceiveData; _property TReceiveErrorEvent OnReceiveError = read = FOnReceiveError , write = FOnReceiveError; _property TModemStateChangeEvent OnModemStateChange = read = FOnModemStateChange , write = FOnModemStateChange;/-#endifComm.cpp /主文件內(nèi)容/-#include #pragma hdrstop#include Comm.h#pragma package(smart_init)/-/ ValidCtrCheck is used to assure that the components created do not have/ any pure virtual functions./static inline void ValidCtrCheck(TComm *) new TComm(NULL);/-_fastcall TComm:TComm(TComponent* Owner) : TComponent(Owner) CommTimer = new TTimer(this); CommTimer-Enabled = false; CommTimer-Interval = 5; CommTimer-OnTimer = ProcTimer; hComm = 0; /通信端口Handle先清空 FPortOpen = False; FCommPort = pnCOM1; /默認COM1 FBaudRate = br9600; /9600bps FHwHandShaking = hhNone; /不啟動硬件流量控制 FSwHandShaking = shNone; /不啟動軟件流量控制 FDataBits = DB8; /數(shù)據(jù)位數(shù)=8 FParity = None; /不作同位檢查 FStopBits = SB1; /停止位數(shù)=1 FInputLen = 0; /默認是一次指令全部讀取 FDTR = FRTS = false;/-namespace Comm void _fastcall PACKAGE Register() TComponentClass classes1 = _classid(TComm); RegisterComponents(System, classes, 0); /-void TComm:OpenComm(void) HANDLE hNewCommFile; String ComStr; char *COMNo; ComStr = COM + IntToStr(1+FCommPort); hNewCommFile = CreateFile( ComStr.c_str(), GENERIC_READ | GENERIC_WRITE, 0, /not shared NULL, /no security OPEN_EXISTING, 0, /No Overlapped NULL /template ); if (hNewCommFile = INVALID_HANDLE_VALUE) throw 打開串口錯誤!; /ShowMessage(Error opening serial port); /raise Exception.Create( Error opening serial port ); if (!SetupComm(hNewCommFile, INPUTBUFFERSIZE, INPUTBUFFERSIZE) CloseHandle(hComm); throw 不能設(shè)置串口緩沖區(qū)!; /ShowMessage(Cannot setup comm buffer); / It is ok to continue. hComm = hNewCommFile; / 清除湲沖區(qū) PurgeComm( hComm, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ; / 通信端口配置 _SetCommState(); FDTR = true; SetDTRStatus(FDTR); SetRTSStatus(FRTS); FPortOpen = True; CommTimer-Enabled = true;/-void TComm:_SetCommState(void) DCB dcb; DWORD tmpValue; /取得串行端口設(shè)置 GetCommState(hComm, &dcb); /更改傳輸速率 dcb.BaudRate = FBaudRate; dcb.fBinary = 1; /必須指定為1 dcb.Parity = (unsigned char)FParity; /Parity的指定 FParityCheck = false; if (FParity != 0) FParityCheck = true; if (FParityCheck) dcb.fParity = (unsigned long)dcb_ParityCheck; / Enable parity check / 設(shè)置硬件流量控制 switch (FHwHandShaking) case hhNone: dcb.fRtsControl=0; break; case hhNoneRTSON: dcb.fRtsControl = RTS_CONTROL_ENABLE; /dcb_RtsControlEnable; break; case hhRTSCTS: dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; /dcb_RtsControlHandShake; dcb.fOutxCtsFlow = 1; /dcb_OutxCtsFlow; break; /設(shè)置軟件流量控制 if (FSwHandShaking!=shNone) dcb.fOutX = 1; /dcb_OutX; dcb.fInX = 1; /dcb_InX; /設(shè)置數(shù)據(jù)位數(shù) dcb.ByteSize = FDataBits + 5; /設(shè)置停止位數(shù) dcb.StopBits = FStopBits ; /將設(shè)置寫入 SetCommState(hComm, &dcb);/- 組件用的定時器事件 -/void _fastcall TComm:ProcTimer(TObject *Sender) DWORD tmpValue; DWORD dwCommError; COMSTAT CS; if (hComm = 0) return; /若設(shè)置讀取的字符數(shù),檢查并引發(fā)事件 ClearCommError(hComm,&dwCommError,&CS); /取得狀態(tài) FCommError = dwCommError; /錯誤數(shù)值 if (FRThreshold 0) if (CS.cbInQue = (DWORD)FRThreshold) ReceiveData(); GetModemState(); / EventProcess(); Application-ProcessMessages(); /看有無其他的指令需執(zhí)行,以免鎖住 /檢查線路狀態(tài)是否發(fā)生改變,若改變則引發(fā)事件 tmpValue = FCommEvent; if (tmpValue != 0) ModemStateChange(tmpValue); Application-ProcessMessages(); /看有無其他的指令需執(zhí)行,以免鎖住 /若發(fā)生錯誤,則引發(fā)錯誤 tmpValue = FCommError; if (tmpValue != 0) ReceiveError(tmpValue); Application-ProcessMessages(); /看有無其他的指令需執(zhí)行,以免鎖住/- 線路狀態(tài)檢測函數(shù) -/void _fastcall TComm:GetModemState(void) DWORD dwModemState;if (hComm=0) ShowMessage(串口打開錯誤!); if (GetCommModemStatus(hComm, &dwModemState ) if (dwModemState & MS_RLSD_ON) if (!FCDHolding) FCommEvent=FCommEvent+EV_RLSD; FCDHolding=true; else if (FCDHolding) FCommEvent=FCommEvent-EV_RLSD; FCDHolding=false; if (dwModemState & MS_DSR_ON) if (!FDSRHolding) FCommEvent=FCommEvent+EV_DSR; FDSRHolding=true; else if (FDSRHolding) FCommEvent=FCommEvent-EV_DSR; FDSRHolding=false; if (dwModemState & MS_RING_ON) if (!FRIHolding) FCommEvent=FCommEvent+EV_RING; FRIHolding=true; else if (FRIHolding) FCommEvent=FCommEvent-EV_RING; FRIHolding=false; if (dwModemState & MS_CTS_ON) if (!FCTSHolding) FCommEvent=FCommEvent+EV_CTS; FCTSHolding=true; else if (FCTSHolding) FCommEvent=FCommEvent-EV_CTS; FCTSHolding=false; / GetCommModem Loop/- Receive Data -/void _fastcall TComm:ReceiveData(void) if (FOnReceiveData) FOnReceiveData(this);/- Modem State Change -/void _fastcall TComm:ModemStateChange(DWORD ModemEvent) if (FOnModemStateChange) FOnModemStateChange( this, ModemEvent );/- Receive Error -/void _fastcall TComm:ReceiveError(DWORD EvtMask) if (FOnReceiveError) FOnReceiveError( this, EvtMask );/- Change Port -/void _fastcall TComm:SetCommPort(TComPortNumber Port) if (Port=FCommPort) return; FCommPort = Port;/- 設(shè)置速率 -/void _fastcall TComm:SetBaudRate(TBaudRate Rate) if (Rate = FBaudRate) return; FBaudRate = Rate; if (hComm != 0) _SetCommState();/- 硬件交握 -/THwHandShaking _fastcall TComm:GetHwHandShaking(void) return(FHwHandShaking);void _fastcall TComm:SetHwHandShaking(THwHandShaking c ) if (c = FHwHandShaking) return; FHwHandShaking = c; if (hComm != 0) _SetCommState();/- 軟件交握 -/TSwHandShaking _fastcall TComm:GetSwHandShaking(void) return(FSwHandShaking);void _fastcall TComm:SetSwHandShaking(TSwHandShaking c ) if (c = FSwHandShaking) return; FSwHandShaking = c; if (hComm != 0) _SetCommState();/- 數(shù)據(jù)位數(shù) -/void _fastcall TComm:SetDataBits(TDataBits Size ) if (Size = FDataBits) return; FDataBits = Size; if (hComm != 0) _SetCommState();/- 奇偶校驗 -/void _fastcall TComm:SetParity(TParity p ) if (p = FParity) return; FParity = p; if (hComm != 0) _SetCommState();/- 停止位 -/void _fastcall TComm:SetStopBits(TStopBits Bits ) if (Bits = FStopBits) return; FStopBits = Bits; if (hComm != 0) _SetCommState();/- 用來清除Buffer -/void _fastcall TComm:SetInDataCount(DWORD StrNo) if (StrNo!=0) return; PurgeComm(hComm, PURGE_RXCLEAR); / 清除COM 資料/- 接收閾值 -/void _fastcall TComm:SetRThreshold(int RTNo) FRThreshold=RTNo;/- 開始通信端口 -/void _fastcall TComm:SetPortOpen(bool b) CommTimer-Enabled=false; if (b) /若指定打開通信端口,則 if (FPortOpen) ShowMessage(串口已經(jīng)打開!); return; /FportOpen loop OpenComm(); /打開通信端口 return; /b loop CloseComm();/- DTR狀態(tài) -/bool _fastcall TComm:GetDTRStatus() return(FDTR);void _fastcall TComm:SetDTRStatus(bool b) FDTR=b; if (hComm=0) return; if (b) EscapeCommFunction(hComm,SETDTR); /將DTR升至高電位 else EscapeCommFunction(hComm,CLRDTR);/將DTR降至低電位/- RTS狀態(tài) -/bool _fastcall TComm:GetRTSStatus() return(FRTS);void _fast

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論