m_SerialPort.InitPort()串口初始化时没有找到串口 直接返回0了

Lowerbyte 2013-09-10 11:45:07
OpenPort:
int nPort=m_ctrlComPort.GetCurSel()+1;
if (m_SerialPort.InitPort(this,nPort,9600,'N',8,1,EV_RXFLAG|EV_RXCHAR,512))
{
m_SerialPort.StartMonitoring();
m_bPortOpen=true;
}
else
{
AfxMessageBox(_T("not found the com"));
m_bPortOpen=false;
}
InitPort():部分代码
EnterCriticalSection(&m_csCommunicationSync);
//if the port is already opened: close it
if(m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}
// prepare port strings
sprintf(szPort,"COM%d",portnr);
sprintf(szBand,"baud=%d parity=%c data=%d stop=%d",baud,parity,databits,stopbits);
//get a handle to the port
m_hComm=CreateFile((LPCWSTR)szPort, //communication port string (COMX)
GENERIC_READ|GENERIC_WRITE, //read/write types
0, //comm devices must be opened exclusive access
NULL, //no security attributes
OPEN_EXISTING, //comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, //Asyns I/O
0); //template must be 0 for comm devices
if (m_hComm==INVALID_HANDLE_VALUE)
{
//NOT FOUND PORT
DWORD dwError=GetLastError();
return FALSE;
}
...全文
484 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lowerbyte 2013-09-11
  • 打赏
  • 举报
回复
引用 4 楼 fronz 的回复:
你确定你设备管理器上有显示能用的串口吧。
查出来了: sprintf(szPort,"COM%d",portnr); sprintf(szBand,"baud=%d parity=%c data=%d stop=%d",baud,parity,databits,stopbits); 数据类型转换的问题,我该用CString str; str.Format();后就可以调用了。 还没搞明白为何这样?
Lowerbyte 2013-09-10
  • 打赏
  • 举报
回复
nPort 值是1。默认COM1
  • 打赏
  • 举报
回复
把nPort的值打印出来
fronz 2013-09-10
  • 打赏
  • 举报
回复
你确定你设备管理器上有显示能用的串口吧。
Lowerbyte 2013-09-10
  • 打赏
  • 举报
回复
。。。顶一下

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧