portobj.h

来自「button of Xp style,provide some infterfa」· C头文件 代码 · 共 194 行

H
194 行
字号
// PortObj.h: interface for the CPortObj class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_PORTOBJ_H__5462C18E_7D34_412A_8DB5_75914376C7B4__INCLUDED_)
#define AFX_PORTOBJ_H__5462C18E_7D34_412A_8DB5_75914376C7B4__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define FC_DTRDSR       0x01
#define FC_RTSCTS       0x02
#define FC_XONXOFF      0x04
#define ASCII_BEL       0x07
#define ASCII_BS        0x08
#define ASCII_LF        0x0A
#define ASCII_CR        0x0D
#define ASCII_XON       0x11
#define ASCII_XOFF      0x13

/////////////////////////////////////////////////////////////////////////////////////////
class CPortObj  
{
	
public:
	CPortObj();
	virtual ~CPortObj();

protected:
	BOOL m_bOpened;					//端口打开标志
	HANDLE m_hIDComDev;				//端口操作句柄

	OVERLAPPED m_OverlappedRead;	//读串口的重叠结构
	OVERLAPPED m_OverlappedWrite;	//写串口的重叠结构

	int m_nErrCode;					//错误代码
	LPVOID m_lpMsgBuf;				//错误信息	
	
protected:
	BOOL WriteCommByte( unsigned char ucByte );	//发送一个字节
	void GetAndFormatLastErr();					//取最后一次错误,并翻译
	
public:

	virtual BOOL Open( int nPort = 1, 
			   int nBaud = 4800,
			   int nParity = NOPARITY,
			   int nByteSize = 8, 
			   int nStopBits = ONESTOPBIT);							//打开端口

	virtual BOOL Close();											//关闭端口
	
	virtual int ReadData( void *pDataBuf, int nBufSize );			//读接收缓冲区数据
	virtual int SendData( const char *pDataBuf, int nSendLen );		//发送数据
	virtual BOOL ChangeParity( int nParity );						//改变奇偶校验

	virtual int GetPortBufDataLen( int *pnParityErrCode = NULL );	//取串口缓冲区字节长度
	virtual BOOL IsOpened(){ return( m_bOpened ); }					//查询是否已经打开串口

	virtual void GetLastErrInfo( int& nErrCode,					
								 LPSTR *lpszErrInfo, 
								 int nSize);						//取最后错误信息
	
};

/////////////////////////////////////////////////////////////////////////////////////////
#define BYTE_INTERVAL_CONST		10	//字符间隔常数

//继承CPortObj对象
class CFFPortObj:CPortObj
{
public:
	CFFPortObj();
	virtual ~CFFPortObj();

public:
	
	BOOL m_bSharePort;		//端口共享标志,如果允许共享,则多次打开时,进行引用计数
	BOOL m_bDataFrameAddr;	//数据帧带有地址
	int  m_nAddrByteLen;	//地址的长度
	
protected:
	int m_nPort;
	int m_nBaud;
	int m_nParity;
	int m_nByteSize;
	int m_nStopBits;

	char m_pRxBuf[5000];	//接收缓冲区指针
	int m_nRxBufSize;		//接收缓冲区长度
	int m_nRxDataLen;		//接收缓冲区字节长度

	int m_nPortUserTotal;	//端口引用计数
	int m_nByteInterval;	//接收字节间隔,单位为毫秒
protected:
	void CalByteIntervalByBaud();	//根据波特率计算两个字节中间允许的时间间隔

public:
	virtual BOOL SendAndWaitReply(const char *pWriteBuf,	//发送缓冲区
								  int nWriteLen,			//发送长度
								  char *pReadBuf,			//接收缓冲区
								  int nReadBufSize,			//接收缓冲区大小
								  int& nReturnDataLen,		//返回数据长度
								  int nMaxWaitFirstByteTime);//最大延时时间

	virtual void ClearRxBuf();								//清除接收缓冲区
	virtual int GetRxBufDataLen();							//取接收缓冲区数据长度		

	virtual void SetRxBufSize( int nSize );
	virtual int GetRxBufSize(){ return m_nRxBufSize; }
	virtual void SetByteInterval( int nTime );				//设置接收字节间隔
	virtual int GetByteInterval(){ return m_nByteInterval; };
	virtual int GetPortID(){ return m_nPort;}
	virtual BOOL SetShareMode( BOOL bSharePort );

//overrides
public:

	virtual BOOL Open( int nPort = 1, 
		int nBaud = 4800,
		int nParity = NOPARITY,
		int nByteSize = 8, 
		int nStopBits = ONESTOPBIT);						//打开端口
	
	virtual BOOL Close();									//关闭端口
	
	virtual int ReadData( void *pDataBuf, int nBufSize );	//读接收缓冲区数据
	virtual int SendData( const char *pDataBuf, int nSendLen );		//发送数据
	virtual BOOL ChangeParity( int nParity );						//改变奇偶校验
	
	virtual int GetPortBufDataLen( int *pnParityErrCode = NULL );	//取串口缓冲区字节长度
	virtual BOOL IsOpened(){ return CPortObj::IsOpened();}			//查询是否已经打开串口
	
	virtual void GetLastErrInfo( int& nErrCode,					
								LPSTR *lpszErrInfo, 
								int nSize);							//取最后错误信息
	
};


/////////////////////////////////////////////////////////////////////////////////////////
//计时函数
#ifndef FASM_CALDELAYTIME
#define FASM_CALDELAYTIME

typedef struct
{
public:
	LONGLONG BeginTime; //开始时间
	double dfFreq;		//时钟频率
	
public:
	
	//计算从上次复位以来到现在已经过去的时间(单位:毫秒)
	int GetTotalMs()
	{
		LARGE_INTEGER litmp; 
		LONGLONG CulTime; 
		
		QueryPerformanceCounter(&litmp); 
		CulTime = litmp.QuadPart; 
		
		return (long)((double)((CulTime-BeginTime)*1.0/ (dfFreq*1.0))*1000); 
	}

	void Start()	//启动计时
	{
		LARGE_INTEGER litmp; 
		// 获得计数器的时钟频率 
		QueryPerformanceFrequency(&litmp); 
		dfFreq = (double)litmp.QuadPart; 
		
		// 获得初始值 
		QueryPerformanceCounter(&litmp); 
		BeginTime = litmp.QuadPart; 
		
	}
	
	BOOL IsTimePass( long nDelayTime )	//比较从开始计时以来的时间是否超过制定时间
	{
		int a = GetTotalMs();
		if( GetTotalMs() > nDelayTime ) 
			return TRUE; //延时已到
		
		return FALSE;
	}

}FASM_CLOCK;
#endif


#endif // !defined(AFX_PORTOBJ_H__5462C18E_7D34_412A_8DB5_75914376C7B4__INCLUDED_)

⌨️ 快捷键说明

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