[转帖]Mscomm32 串口无效(Invalid port number),导致窗口初始化异常_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1944 | 回复: 0   主题: [转帖]Mscomm32 串口无效(Invalid port number),导致窗口初始化异常        上一篇   下一篇 
lplwx123
注册用户
等级:新兵
经验:72
发帖:5
精华:0
注册:2017-3-9
状态:离线
发送短消息息给lplwx123 加好友    发送短消息息给lplwx123 发消息
发表于: IP:您无权察看 2019-9-18 10:02:27 | [全部帖] [楼主帖] 楼主

使用Microsoft Communications Control, version 6.0 控件,在一个窗口的 OnInitDialog()中初始化控件并调用 if(!pMsComm->get_PortOpen() )//打开串口 语句,其中pMsComm为Microsoft Communications Control, version 6.0 控件关联变量。该语句在失败时会抛出一个异常,该异常在与被如下代码捕获,

RESULT AFXAPI AfxCallWndProc(CWnd* pWnd, HWND hWnd, UINT nMsg,
WPARAM wParam = 0, LPARAM lParam = 0)
_AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData();
MSG oldState = pThreadState->m_lastSentMsg; // save for nesting
pThreadState->m_lastSentMsg.hwnd = hWnd;
pThreadState->m_lastSentMsg.message = nMsg;
pThreadState->m_lastSentMsg.wParam = wParam;
pThreadState->m_lastSentMsg.lParam = lParam;
#ifdef _DEBUG
_AfxTraceMsg(_T("WndProc"), &pThreadState->m_lastSentMsg);
#endif
// Catch exceptions thrown outside the scope of a callback
// in debug builds and warn the user.
LRESULT lResult;
#ifndef _AFX_NO_OCC_SUPPORT
// special case for WM_DESTROY
if ((nMsg == WM_DESTROY) && (pWnd->m_pCtrlCont != NULL))
pWnd->m_pCtrlCont->OnUIActivate(NULL);
#endif
// special case for WM_INITDIALOG
CRect rectOld;
DWORD dwStyle = 0;
if (nMsg == WM_INITDIALOG)
_AfxPreInitDialog(pWnd, &rectOld, &dwStyle);
// delegate to object's WindowProc
lResult = pWnd->WindowProc(nMsg, wParam, lParam);
// more special case for WM_INITDIALOG
if (nMsg == WM_INITDIALOG)
_AfxPostInitDialog(pWnd, rectOld, dwStyle);
CATCH_ALL(e)
lResult = AfxProcessWndProcException(e, &pThreadState->m_lastSentMsg);
TRACE(traceAppMsg, 0, "Warning: Uncaught exception in WindowProc (returning %ld).\n",
lResult);
DELETE_EXCEPTION(e);
END_CATCH_ALL
pThreadState->m_lastSentMsg = oldState;
return lResult;


致OnInitDialog()在串口初始化之后的代码都没有执行。使得依赖这些代码的代码出现以未初始化为主要特征的异常。一个改进的方式是事先检测串口有没有效果。

     因此在网上查找了一些资料,总结出在串口初始化之前可以使用如下代码:

HANDLE m_hCom;
CString com;
com.Format("\\\\.\\COM%d",(int)(pMsComm->get__CommPort()));
m_hCom = CreateFile(com, 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); // 这里的CreateFile函数起了很大的作用,可以用来创建系统设备文件,如果该设备不存在或者被占用,则会返回一个错误,即下面的 INVALID_HANDLE_VALUE ,据此可以判断可使用性。详细参见MSDN中的介绍。
if(m_hCom == INVALID_HANDLE_VALUE) // 如果没有该设备,或者被其他应用程序在用 *******************
int errornum=GetLastError();
if(errornum==2)
com.Format("端口%d 不存在",(int)(pMsComm->get__CommPort()));
else if(errornum==5)
com.Format("端口%d被占用",(int)(pMsComm->get__CommPort()));
AfxMessageBox(com);
CloseHandle(m_hCom); // 关闭文件句柄,后面我们采用控件,不用API
return ;//这是因为串口初始化封装在另一个函数里面在OnInitDialog调用。
CloseHandle(m_hCom); // 关闭文件句柄,后面我们采用控件,不用API


使用该代码获取串口是否可用。如果必须要开启串口,可以单独写个 线程,在线程里面循环检测该代码。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论