⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcomm32.h

📁 网吧电量采集、温湿度采集串口通信实例程序
💻 H
字号:
#ifndef _TCOMM32_H_YBCHEN_
#define _TCOMM32_H_YBCHEN_

/***************************************************************************\
*                                                                           *
*                  TComm32.h - 串行口通讯程序, 版本 1.3.3.5                 *
*                              Bulid: 09-05-2004                            *
*                    Copyright (C) 1997-2004, Victor Chen                   *
*                      Homepage: http://www.cppfans.com                     *
*                          Email: victor@cppfans.com                        *
*                                                                           *
\***************************************************************************/

#include <VCL.h>
#include <process.h>
#include <stdio.h>
#include <Controls.hpp>
//---------------------------------------------------------------------------
namespace VictorComm32 {
//---------------------------------------------------------------------------

class PACKAGE TCommQueue
 {
   public:
     TCommQueue(long lSize=8192);
     virtual ~TCommQueue();

     long In(const char far *s);         //RetVal: num of chars pushed in
     long In(const char far *s, long n); //RetVal: num of chars pushed in
     long Out(char far *s);              //RetVal: num of chars poped out
     long Out(char far *s, long n);      //RetVal: num of chars poped out
     inline void Clear(void) { _Head = _Tail = 0; }

     __property long Count  = { read = fGetCount  }; //readonly property
     __property long QSize  = { read = _BufSize, write = fSetBufSize };
     __property bool Valid  = { read = fIsValid   }; //readonly property
     __property long Remain = { read = fGetRemain }; //readonly property

   protected:
     inline bool fIsValid(void) { return _Buffer != 0; }
     inline long fGetCount(void) { return (_Tail-_Head+_BufSize)%_BufSize; }
     inline long fGetRemain(void) { return (_Head-_Tail+_BufSize-1)%_BufSize; }
     void fSetBufSize(long n);
     long _BufSize, _Head, _Tail;
     char far *_Buffer;
 };
//---------------------------------------------------------------------------

class PACKAGE TCommSerialPortInfo
 {
   public:
     __property TStringList *PortList = { read = _PortList };
     void __fastcall Refresh(void);

     __fastcall TCommSerialPortInfo();
     virtual __fastcall ~TCommSerialPortInfo();

     __property bool IsFromSystem = { read = _bFromSysDrv };
     static int __fastcall PortNo(AnsiString s);
     static AnsiString __fastcall PortName(int iPortNo);

   private:
     static int __fastcall PortListSortCompare(TStringList* lpList, int Index1, int Index2);

     TStringList *_PortList;
     bool _bFromSysDrv;
     void __fastcall ListFromSystem(void);
 };
//---------------------------------------------------------------------------

class PACKAGE TComm32:public TObject
 {
   public:
     enum TFlowControl { fcNone = 0, fcRtsCts = 3, fcXonXoff = 4, fcTranXonXoff = 5, fcRtsCtsXonXoff = 6 }; //AT&Kn

   public:
     __fastcall TComm32();
     virtual __fastcall ~TComm32();

     __property int    PortNo = { read = _PortNo, write = fSetPort   };
     __property HANDLE Handle = { read = _Handle, write = fSetHandle };
     __property bool   Active = { read = _Opened, write = fSetActive  };

     __property DWORD  Baud     = { read = fGetBaud    , write = fSetBaud    };
     __property BYTE   Parity   = { read = fGetParity  , write = fSetParity  };
     __property BYTE   ByteSize = { read = fGetByteSize, write = fSetByteSize};
     __property BYTE   StopBits = { read = fGetStopBits, write = fSetStopBits};

     __property long InBufSize  = { read = fGetInBufSize , write = fSetInBufSize };
     __property long OutBufSize = { read = fGetOutBufSize, write = fSetOutBufSize};

     __property long HwInSize   = { read = _HwInSize , write = fSetHwInSize  };
     __property long HwOutSize  = { read = _HwOutSize, write = fSetHwOutSize };

     virtual long __fastcall Read(void far *s, long n);
     virtual long __fastcall Write(const void far *s, long n);
     virtual long __fastcall Command(const char far *s);

     virtual void __fastcall PurgeRead(bool bAbort=false);
     virtual void __fastcall PurgeWrite(bool bAbort=false);

     __property TFlowControl  FlowControl = { read = fGetFCtrl, write = fSetFCtrl };
     __property unsigned char AutoAnswer  = { read = _AutoAns , write = fSetAAns  };
     __property DWORD         ModemStatus = { read = _ModemStatus }; //MS_CTS_ON | MS_DSR_ON | MS_RING_ON | MS_RLSD_ON

     __property bool DTR  = { read = fGetDTR , write = fSetDTR  }; //Computer status, read/write
     __property bool RTS  = { read = fGetRTS , write = fSetRTS  }; //Computer status, read/write
     __property bool CTS  = { read = fGetCTS  }; //Modem status, read-only
     __property bool DSR  = { read = fGetDSR  }; //Modem status, read-only
     __property bool RING = { read = fGetRING }; //Modem status, read-only
     __property bool RLSD = { read = fGetRLSD }; //Modem status, read-only

     virtual void __fastcall ResetModem(void); //reset modem to power-on status
     virtual void __fastcall InitModem(void); //init modem using TComm32 properties
     virtual BOOL __fastcall EscapeCommFunction(DWORD dwFunc); //dwFunc: one of the following values: CLRDTR,CLRRTS,SETDTR,SETRTS,SETXOFF,SETXON,SETBREAK,CLRBREAK

     __property bool  FromHandle   = { read = _bFromHandle };
     __property bool  DisableWrite = { read = _DisableWrite, write = _DisableWrite};

     __property TCommQueue  *InQueue  = { read = _InQueue    };
     __property TCommQueue  *OutQueue = { read = _OutQueue   };

     //可以产生 OnCommNotify 的事件: EV_RXCHAR|EV_TXEMPTY|EV_CTS|EV_DSR|EV_RLSD|EV_RING|EV_RXFLAG|EV_RX80FULL|EV_ERR
     __property void __fastcall (__closure *OnCommNotify) (TObject *Sender, int NotifyType) = { read = lpCommNotify, write=lpCommNotify };
     __property void __fastcall (__closure *AfterOpen) (TObject *Sender) = { read = lpAfterOpen, write = lpAfterOpen };
     __property void __fastcall (__closure *BeforeClose) (TObject *Sender) = { read = lpBeforeClose, write = lpBeforeClose };

   protected:
     virtual void __fastcall Open(void);
     virtual void __fastcall Close(void);

     virtual void __fastcall CommNotify(int NotifyType); //EV_RXCHAR or EV_TXEMPTY
     virtual void __fastcall CommAfterOpen(void);
     virtual void __fastcall CommBeforeClose(void);
     virtual void __fastcall fmsgCommTimer(void);

   private:
     inline void __fastcall fSetPort(int COMn)       { Close(); _PortNo = COMn; sprintf(_PortName, Fmt_COMn, _PortNo); _bFromHandle = false; }
     inline void __fastcall fSetHandle(HANDLE hFile) { Close(); _PortNo = 0; _PortName[0] = 0; _Handle = hFile; _bFromHandle = true; }
     inline void __fastcall fSetActive(bool bActive) { if(bActive) Open(); else Close(); }

     inline DWORD __fastcall fGetBaud    (void) { return _dcb.BaudRate; }
     inline BYTE  __fastcall fGetParity  (void) { return _dcb.Parity  ; }
     inline BYTE  __fastcall fGetByteSize(void) { return _dcb.ByteSize; }
     inline BYTE  __fastcall fGetStopBits(void) { return _dcb.StopBits; }

     inline void __fastcall fSetBaud    (DWORD NewBaud  ) { Close(); _dcb.BaudRate = NewBaud  ; }
     inline void __fastcall fSetParity  (BYTE  NewParity) { Close(); _dcb.Parity   = NewParity; }
     inline void __fastcall fSetByteSize(BYTE  NewBSize ) { Close(); _dcb.ByteSize = NewBSize ; }
     inline void __fastcall fSetStopBits(BYTE  NewSBits ) { Close(); _dcb.StopBits = NewSBits ; }

     inline long __fastcall fGetInBufSize (void) { return _InQueue ->QSize; }
     inline long __fastcall fGetOutBufSize(void) { return _OutQueue->QSize; }
     inline void __fastcall fSetInBufSize (long n)  { _InQueue ->QSize = n; }
     inline void __fastcall fSetOutBufSize(long n)  { _OutQueue->QSize = n; }

     inline void __fastcall fSetHwBufSize(long HwIn, long HwOut) { Close(); _HwInSize=HwIn; _HwOutSize=HwOut; }
     inline void __fastcall fSetHwInSize (long NewSize) { fSetHwBufSize(NewSize, _HwOutSize); }
     inline void __fastcall fSetHwOutSize(long NewSize) { fSetHwBufSize(_HwInSize,  NewSize); }

     TFlowControl __fastcall fGetFCtrl(void);
     void         __fastcall fSetFCtrl(TFlowControl);
     inline void  __fastcall fSetAAns(unsigned char aans) { _AutoAns = aans; }

     bool __fastcall fGetDTR(void); //Computer status, read/write
     bool __fastcall fGetRTS(void); //Computer status, read/write
     void __fastcall fSetDTR(bool); //Computer status, read/write
     void __fastcall fSetRTS(bool); //Computer status, read/write

     bool __fastcall fGetCTS (void) { return (_ModemStatus & MS_CTS_ON ) != 0; } //Modem status, read-only
     bool __fastcall fGetDSR (void) { return (_ModemStatus & MS_DSR_ON ) != 0; } //Modem status, read-only
     bool __fastcall fGetRING(void) { return (_ModemStatus & MS_RING_ON) != 0; } //Modem status, read-only
     bool __fastcall fGetRLSD(void) { return (_ModemStatus & MS_RLSD_ON) != 0; } //Modem status, read-only

   private:
     HANDLE _Handle;    //INVALID_HANDLE_VALUE
     DCB    _dcb;
     bool   _bFromHandle, _Opened, _DisableWrite;
     bool   _bDTR, _bRTS;

     int    _PortNo;
     char   _PortName[32];
     long   _HwInSize, _HwOutSize;
     unsigned char _AutoAns;

     TCommQueue  *_InQueue;
     TCommQueue  *_OutQueue;

     static const char Fmt_COMn[];       //"COM%d"
     static const char Fmt_InitModem[];  //"ATE0&K%dS0=%d\r"
     static const char Str_ResetModem[]; //"ATZ\r"

     void __fastcall (__closure *lpCommNotify) (TObject *Sender, int NotifyType);
     void __fastcall (__closure *lpAfterOpen) (TObject *Sender);
     void __fastcall (__closure *lpBeforeClose) (TObject *Sender);

     static void _ReadThread(void *Param);
     static void _WriteThread(void *Param);
     static void _TimerThread(void *Param);

   private:
     volatile long _ReadThreadId;       //-1
     volatile bool _RunReadThread;      // 0
     volatile bool _ReadThreadRunning;  // 0

     volatile long _WriteThreadId;      //-1
     volatile bool _RunWriteThread;     // 0
     volatile bool _WriteThreadRunning; // 0

     volatile long _TimerThreadId;      //-1
     volatile bool _RunTimerThread;     // 0
     volatile bool _TimerThreadRunning; // 0

     OVERLAPPED    _ReadOS;             //
     OVERLAPPED    _WriteOS;            //write overlapped structure

     HANDLE        _hKillRead;          //kill read thread //NULL
     HANDLE        _hKillWrite;         //kill write thread //NULL
     HANDLE        _hSyncWrite;         //write synchronize event //NULL
     HANDLE        _hKillTimer;         //kill timer thread //NULL

     DWORD _ModemStatus;    //MS_CTS_ON | MS_DSR_ON | MS_RING_ON | MS_RLSD_ON
     DWORD _TimerInterval;  //Detect Ring Timer
     DWORD _DetectRingTime, _dwDetectingRing;
 };

//---------------------------------------------------------------------------

class PACKAGE EComm32Error:public Exception
{
public:
  enum TErrorCode
   {
     COMM_NOERROR   =  0, //没有错误
     COMM_NOMEMORY  =  1, //内存不够
     COMM_INITRDBUF =  2, //不能初始化读缓存
     COMM_INITWRBUF =  3, //不能初始化写缓存
     COMM_OPENPORT  =  4, //不能打开端口
     COMM_SETMASK   =  5, //不能设置端口事件MASK
     COMM_BUFSIZE   =  6, //不能设置端口缓存
     COMM_GETSTATE  =  7, //不能得到端口参数
     COMM_SETSTATE  =  8, //不能设置端口参数
     COMM_CRRDEVENT =  9, //不能创建端口异步读事件
     COMM_CRWREVENT = 10, //不能创建端口异步写事件
     COMM_CRRDTHREAD= 11, //不能创建端口读线程
     COMM_CRWRTHREAD= 12, //不能创建端口写线程
     COMM_CLOSERDT  = 13, //不能关闭端口读线程
     COMM_CLOSEWRT  = 14, //不能关闭端口写线程
   };

  __property TErrorCode ErrorCode = { read = _ErrorCode };
  __fastcall EComm32Error(TErrorCode ErrCode);

protected:
  TErrorCode _ErrorCode;

private:
  class MessageStrings
   {
     public:
       typedef struct {TErrorCode Code ; char *Msg ; } ERRMSG;

       __property AnsiString ErrMsg[TErrorCode] = { read = fGetErrMsg };
       __property AnsiString Unknown            = { read = fGetUnknown};

       __fastcall MessageStrings();

     private:
       AnsiString __fastcall fGetErrMsg(TErrorCode ecCode);
       AnsiString __fastcall fGetUnknown(void);

       ERRMSG     *Err_Messages;
       char       *Err_Unknown;

       static ERRMSG Err_Messages_Chs[], Err_Messages_Cht[], Err_Messages_Enu[];
       static char   Err_Unknown_Chs [], Err_Unknown_Cht [], Err_Unknown_Enu [];
   };
};
//---------------------------------------------------------------------------
} //namespace VictorComm32
//---------------------------------------------------------------------------
using namespace VictorComm32;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -