上位机MFC同步异步串口通讯源代码
例程是通过纯函数编写的串口通讯类:
如下
- #include <windows.h>
- #ifndef ZCOMM_CALSS
- #define ZCOMM_CALSS
- #define COMSUCCESS 1
- #define FAILURE 0
- #define MAXTIMEOUT 2000
- class ZComm {
- public:
- HANDLE idComDev;
- HANDLE receive;
- HANDLE began_receive;
- int fConnected;
- int fCOMMOpened;
-
- protected:
- OVERLAPPED WRITE_OS,READ_OS;
- BOOL olap; // overlapped 方式
- HANDLE hCommWatchThread;
- DWORD dwThreadID ;
- public:
- ZComm();
- ~ZComm();
- public:
- int open(char *szPort,int Baud_rate,BOOL overlapped); //打开串口,初始化串口
- int Close();//关闭串口
- int set_baudrate(int baud_rate);
- int Read(BYTE *buf,int nMaxLength);
- int ReadString(void* pData, int nLength);
- int Write(BYTE *buf,int buflen);
- int sendcommand(BYTE *buf,int buflen,char * reply,int *len_reply);
- BOOL WaitEventChar();
- public:
- BOOL SetRTS(int set);
- BOOL SetTimeOut(DWORD ReadIntervalTimeout,
- DWORD ReadTotalTimeoutMultiplier,
- DWORD ReadTotalTimeoutConstant);
- void crc_16(BYTE *data, int length, BYTE *reply);
- BOOL FlashCommMask();
- BOOL SetEventChar(char EventChar);
- BOOL PurgeIn();
- BOOL Purge();
- int CheckICC (int cmd,BYTE *buf);
- BYTE CalculateICC (int cmd,BYTE *buf);
- DWORD ReadStream(BYTE * buf,int Length);
- DWORD ReadByte(BYTE * buf);
- bool SendStrCommand(CString CommandWord);
- CString ReceiveATInfo();
- };
- #endif
复制代码
|