ComControl.h
上传用户:asikq0571
上传日期:2014-07-12
资源大小:528k
文件大小:2k
- // ComControl.h: interface for the CComControl class.
- //
- //////////////////////////////////////////////////////////////////////
- #if !defined(AFX_COMCONTROL_H__ED00AC9E_EFAB_46E7_BDD3_18951E15DABF__INCLUDED_)
- #define AFX_COMCONTROL_H__ED00AC9E_EFAB_46E7_BDD3_18951E15DABF__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- //串口监测线程
- UINT ComThreadProc(LPVOID para);
- class CComControl : public CObject
- {
- friend UINT ComThreadProc(LPVOID para);
- public:
- CComControl();
- virtual ~CComControl();
- public:
- virtual void Serialize(CArchive &ar);
- int m_nStopBits; //停止位
- int m_nDataBits; //数据位
- CString m_szPortName; //串口名
- int m_nBaudRate; //波特率
- int m_nParity; //校验方式
- HWND m_hOwner; //所有者窗口句柄,串口事件将通知此窗口
- HANDLE ComHandle() //串口句柄,通过该句柄,用户可以以自己的方式工作
- {
- return m_hCom;
- }
- DWORD BytesInQue(); //返回输入队列中字节数
- DWORD BytesOutQue(); //返回输出队列中字节数
- bool PortOpened() //返回端口是否是打开的
- {
- return m_hCom != INVALID_HANDLE_VALUE;
- }
- //打开关闭串口
- bool OpenPort(); //打开串口,一般先调用SetParam
- bool OpenPort(int nPortNo, DCB& dcb, COMMTIMEOUTS& timeouts); //直接用给定参数打开串口
- void ClosePort(); //关闭串口
- //设置参数
- void SetParam(); //通过对话框设置串口参数
- void SetParam(LPCTSTR szPortName, int nBaudRate, int nParity);
- //读写函数,他们将阻塞调用线程,直到成功或超时或发生错误
- BOOL Send(BYTE* pBuff, DWORD dwBytesToSend); //发送数据
- BOOL Send(BYTE* pBuff, DWORD dwBytesToSend, DWORD& dwBytesSended);
- BOOL Receive(BYTE* pBuff, DWORD dwBytesToRead); //接收数据
- BOOL Receive(BYTE* pBuff, DWORD dwBytesToRead, DWORD& dwBytesReaded);
- //清除串口缓冲区和错误标志
- void ClearPort();
- void ClearErr();
- private:
- HANDLE m_hCom; //端口句柄
- BOOL volatile m_bExit;
- CWinThread* m_pThread; //串口线程
- OVERLAPPED m_ovWrite; //用于发送的数据OVERLAPPED结构
- OVERLAPPED m_ovRead; //用于接收数据的OVERLAPPED结构
- //private function
- //init com object
- void InitCom();
- };
- #endif // !defined(AFX_COMCONTROL_H__ED00AC9E_EFAB_46E7_BDD3_18951E15DABF__INCLUDED_)