⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_41906.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:41906 发表者:小缪 发表日期:2003-05-30 10:33:03
<br>主题:请教大虾!!
<br>内容:请问串口在没有线连接时能不能对他进行配置啊?<BR>如果能,我这段程序为什么不行啊?每次都出现“打开串口失败”的报告啊!!怎么就配置不成功了??望大虾指教!!<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HANDLE hCom;<BR>&nbsp;&nbsp;&nbsp;&nbsp;DWORD dwError;<BR>&nbsp;&nbsp;&nbsp;&nbsp;DCB&nbsp;&nbsp; dcb={0};<BR>&nbsp;&nbsp;&nbsp;&nbsp;char buffer[1024];<BR>&nbsp;&nbsp;&nbsp;&nbsp;int READ_BUF_SIZE=32;<BR>&nbsp;&nbsp;&nbsp;&nbsp;hCom=CreateFile("COM1",<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GENERIC_READ|GENERIC_WRITE,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dcb.BaudRate=CBR_4800;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dcb.fBinary=TRUE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dcb.fParity=FALSE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dcb.ByteSize=8;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dcb.StopBits=1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dcb.fDtrControl=DTR_CONTROL_DISABLE; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!SetCommState(hCom,&amp;dcb)) AfxMessageBox("打开串口失败!",MB_OK);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:michael 回复日期:2003-05-30 13:12:08
<br>内容:1)打开一个串口需使用:<BR><BR>CFile file;<BR><BR>CFileException e;<BR><BR>file.Open (<BR><BR>portName, //example "COM1","COM2"<BR><BR>CFile::modeReadWrite,<BR><BR>&amp;e);<BR><BR>2)关闭一个串口需使用:<BR><BR>file.Close();<BR><BR>3)从端口进行读操作,需使用:<BR><BR>char m_ReadBuff[UINT n];<BR><BR>UINT nByte=file.Read (<BR><BR>&amp;m_ReadBuff, //buffer to store byte<BR><BR>UINT nCount //number of bytes to read<BR><BR>);<BR><BR>4)从端口进行写操作,需使用:<BR><BR>char m_WriteBuff[UINT n];<BR><BR>file.Write (<BR><BR>&amp;m_WriteBuff, //buffer to store byte<BR><BR>UINT nCount //number of bytes to write<BR><BR>);<BR><BR>5)配置串口<BR>串行端口创建时,必须对其进行设置以匹配与其对话的设备。虽然可以通过操作系统设置这些参数,但也可以用Windows API 中的SetCommState()函数来设置它们。一般地,可用如下程序设置它们:<BR>DCB dcb;<BR><BR>::GetCommState( (HANDLE)file.m_hFile, &amp;dcb );<BR><BR>dcb.BaudRate = 1200,…;<BR><BR>dcb.ByteSize = 7 or 8;<BR><BR>dcb.StopBits = 0,1,2=0,1.5,2;<BR><BR>dcb.Parity = 0-4=no,odd,even,mark,space;<BR><BR>::SetCommState((HANDLE)file.m_hFile, &amp;dcb );<BR><BR>为了更好地控制端口可以利用SetCommTimeouts()函数打开或关闭超时功能,具体程序如下:<BR><BR>COMMTIMEOUTS cto;<BR><BR>::GetCommTimeouts((HANDLE)file.m_hFile , &amp;cto );<BR><BR>cto.ReadIntervalTimeout =0;<BR><BR>cto.ReadTotalTimeoutMultiplier =0;<BR><BR>cto.ReadTotalTimeoutConstant =0;<BR><BR>cto.WriteTotalTimeoutMultiplier=0;<BR><BR>cto.WriteTotalTimeoutConstant =0;<BR><BR>::SetCommTimeouts((HANDLE)file.m_hFile , &amp;cto );
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:小缪 回复日期:2003-05-30 18:33:20
<br>内容:我看到的打开串口都是用CreatFile()函数的,且我的串口打开已经成功了啊,就是在进行配置的时候出现了问题啊<BR>即SetCommState(hCom,&amp;dcb) 函数返回的是FALSE<BR>为什么配置不成功啊?<BR>我想是dcb有问题,可是我觉得没有问题啊,michael,您能够帮我看一下吗?谢谢!!<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

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