BOOL DestroyWindow HWND hWnd);
/*说明:采用WIN32函数销毁控件*/
/*hWnd:EasyComm控件句柄*/
/*举例:可以选择在客户窗体销毁前销毁EasyComm控件*/
void CCustomerWnd::OnDestroy )
{
if ::IsWindow m_hCom))
::DestroyWindow m_hCom);
CWnd::OnDestroy );
}
4)、EasyComm控件消息:
COM_SETPORT:设置端口名称
wParam: 0
lParam: 端口号字符串指针
举例:SendMessage hCom,COM_SETPORT,0, LPARAM)_T “COM1”));
COM_GETPORT:取端口号
wParam: 缓冲区大小
lParam:缓冲区指针
举例:TCHAR buf[10];
SendMessage hCom,COM_GETPORT,9, LPARAM)buf);
COM_SETBAUDRATE:设置端口波特率
wParam:波特率长整数值
lParam:0
举例:SendMessage hCom,COM_SETBAUDRATE,4800,0);
COM_GETBAUDRATE:取端口波特率
wParam: 0
lParam: 0
举例:long nBuad = long)SendMessage hCom,COM_GETBAUDRATE,0,0);
COM_SETBYTESIZE:设置端口位长
wParam: 位长短整数值
lParam: 0
举例:SendMessage hCom,COM_SETBYTESIZE,8,0);
COM_GETBYTESIZE: 取端口位长
wParam: 0
lParam: 0
举例:short nByteSize = short)SendMessage hCom,COM_GETBYTESIZE,0,0);
COM_SETSTOPBITS: 设置端口停止位
wParam: 停止位短整数值
lParam: 0
#define STOPBITS_ONE 1 /*1位停止位*/
#define STOPBITS_TWO 2 /*2位停止位*/
#define STOPBITS_ONEHALF 3/*1.5停止位*/
举例:SendMessage hCom,COM_SETSTOPBITS,1,0);
COM_GETSTOPBITS: 取端口停止位
wParam: 0
lParam: 0
举例:short nStopBits = short)SendMessage hCom,COM_GETSTOPBITS,0,0);
COM_SETPARITY : 设置端口校验
wParam: 校验短整数值
lParam: 0
#define PY_NONE 0 /*无奇偶校验*/
#define PY_EVEN 1 /*偶校验*/
#define PY_ODD 2 /*奇校验*/
举例:SendMessage hCom,COM_SETPARITY,0,0);
COM_GETPARITY: 取端口校验
wParam: 0
lParam: 0
举例:short nParity = short)SendMessage hCom,COM_GETPARITY,0,0);
COM_SETFCTRL: 设置端口流控制
wParam: 流模式短整数值
lParam: 0
#define FLOWCTRL_NONE 0 /*无流控制*/
#define FLOWCTRL_HARD 1 /*硬件控制*/
#define FLOWCTRL_SOFT 2 /*软件控制*/
举例:SendMessage hCom,COM_SETCTRL,0,0);
COM_GETFCTRL: 取端口流控制
wParam: 0
lParam: 0
举例:short nCtrl = short)SendMessage hCom,COM_GETCTRL,0,0);
COM_SETTIMEOUTS: 设置读写每字节超时毫秒数
wParam: LOWORD为读超时,HIWORD为写超时
lParam: 0
举例:SendMessage hCom,COM_SETTIMEOUTS,MAKEWPARAM 1,1),0);
COM_GETTIMEOUTS: 取读写每字节超时毫秒数
wParam: 0
lparam: 0
举例:DWORD dw = DWORD)SendMessage hCom,COM_GETTIMEOUTS,0,0);
short nReadPerByte = LOWORD dw); short nWritePerByte = HIWORD dw);
COM_SETACKCHAR: 设置端口回应字符
wParam: 回应字符
lParam: 0
举例:SendMessage hCom,COM_SETACKCHAR, WPARAM)0x06,0);
COM_GETACKCHAR: 取端口回应字符
wParam: 0
lParam: 0
举例: char chAck = char)SendMessage hCom,COM_GETACKCHAR,0,0);
COM_SETLISTIMER: 设置端口侦听超时毫秒数
wParam: 短整数毫秒
lParam: 0
举例:SendMessage hCom,COM_SETLISTIMER,100,0);
COM_GETLISTIMER: 取端口侦听超时毫秒数
wParam: 0
lParam: 0
举例:short nListemTimer = short)SendMessage hCom,COM_GETLISTIMER,0,0);
COM_SETQUEUE: 设置端口内部读写缓冲区大小
wParam: 读缓冲区长整数值
lParam: 写缓冲区长整数值
举例:SendMessage hCom,COM_SETQUEUE,1024,1024)
COM_SETACTIVE: 设置端口状态,打开或关闭
wParam: TRUE则打开端口,FALSE则关闭端口
lParam: 0
举例:SendMesage hCom,COM_SETACTIVE, WPARAM)TRUE,0);
SendMessage hCom,COM_SETACTIVE, WPARAM)FALSE,0);
COM_GETACTIVE: 取端口状态
wParam: 0
lParam: 0
举例:BOOL bActive = SendMessage hCom,COM_GETACTIVE,0,0);
COM_SETDATA: 向端口写数据
wParam: 长整数值的数据大小
lParam: 数据指针
返回:已写的字节数
举例:long nWrited = SendMessage hCom,COM_SETDATA, WPARAM)size, LPARAM)data);
COM_GETDATA: 从端口读数据
wParam: 长整数值的请求字节数
lParam: 缓冲区指针
返回:已读的字节数
举例:long nReaded = SendMessage hCom,COM_GETDATA, WPARAM)size, LPARAM)buf);
5)、EasyComm的通知消息:控件通过WM_NOTIFY通知父客户窗体,该消息体的结构为:
struct {
HWND hwndFrom; /*控件的窗体句柄*/
UINT idFrom; /*控件的ID*/
UINT code; /*消息标识*/
WPARAM wParam; /*根据消息标识设置*/
LPARAM lParam; /*根据消息标识设置*/
}NMHDR_COMM;
用于通知端口状态的结构:
struct {
BOOL bRLSDHold; /*非零则端口等待RLSD信号,输出阻塞*/
BOOL bCTSHold; /*非零则端口等待CTS信号,输出阻塞*/
BOOL bDSRHold; /*非零则端口等待DSR信号,输出阻塞*/
BOOL bXOFFHold; /*非零则收到XOFF字符,输出阻塞*/
BOOL bXOFFSent; /*非零则送出XOFF字符,输出阻塞*/
int nInQueue; /*端口输入缓冲区字符数*/
int nOutQueue; /*端口输出缓冲区字符数*/
}CommMonitor;
|
具体消息标识(code)分述如下:
NC_COMMBREAK:端口中断通知
wParam: 忽略
lParam: 忽略
NC_COMMERROR:端口错误通知
wParam: 错误代码
lParam: 错误文本指针
NC_COMMMONITOR:端口状态通知
wParam: 忽略
lParam: 端口状态结构CommMonitor指针
NC_COMMACK:端口收到回应字符
wParam: 输入缓冲区可读的字符数
lParam: 忽略
NC_COMMRECIVE:端口收到字符
wParam: 输入缓冲区可读的字符数
lParam: 忽略
举例:在客户窗体中响应控件通知
BOOL CCustomerWnd::OnNotify WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
NMHDR* phdr = NMHDR*)lParam;
if phdr->idFrom == IDC_COM)
{
NMHDR_COMM* pnc = NMHDR_COMM*)lParam;
if pnc->code == NC_COMMBREAK)
{
AfxMessageBox _T "Comm Breaked"));
}else if pnc->code == NC_COMMERROR)
{
AfxMessageBox TCHAR*)pnc->lParam);
}else if pnc->code == NC_COMMMONITOR)
{
CommMonitor* pcm = CommMonitor*)pnc->lParam;
…
}else if pnc->code == NC_COMMRECIVE)
{
if pnc->wParam)
{
TCHAR *tmp = TCHAR*)calloc long)pnc->wParam + 1,sizeof TCHAR));
::SendMessage hCom,COM_GETDATA, WPARAM)pnc->wParam, LPARAM)tmp);
…
free tmp);
}
}
}
return CWnd::OnNotify wParam, lParam, pResult);
}
|
|