📄 subject_58752.htm
字号:
<p>
序号:58752 发表者:狂战士 发表日期:2003-11-03 15:27:30
<br>主题:怎样把从串口接收来的数据放入全局unsigned char[] 中!?
<br>内容:我只能把接收来的数据放入全局CString str_receive中<BR> CString str_receive=_T("");<BR><BR>LRESULT CXXXXXDlg:: OnCommNotify(WPARAM wParam,LPARAM lParam)<BR>{<BR> m_pOutbuffer=new char[4096];<BR> memset(m_pOutbuffer, 0, 4096);<BR> if((wParam&EV_RXCHAR)!=EV_RXCHAR)<BR> {<BR> SetEvent(m_hPostMsgEvent);<BR> return 0L;<BR> }<BR> else<BR> {<BR> int i;<BR> DWORD number=ReadComm(m_pOutbuffer,4096);<BR> CString string_receive=_T("");<BR> for( i=0;i<(int)number;i++)<BR> {<BR> string_receive=string_receive+m_pOutbuffer[i];<BR> str_receive+=string_receive;<BR> string_receive=_T("");<BR> }<BR> SetEvent(m_hPostMsgEvent);<BR> }<BR> delete []m_pOutbuffer;<BR> return 0L;<BR>}<BR>如何能把从串口接收来的数据放入全局unsigned char[] 中呢?<BR>因为把数据放入CString变量中会导致丢失数据的!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:CDMA2000 回复日期:2003-11-03 15:33:02
<br>内容:int nBuflen=str_receive.GetLength();<BR>unsigned char* pDataBuf=new unsigned char[nBuflen}<BR>memcpy(pDataBuf,str_receive,nBuflen);
<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-11-03 15:37:22
<br>内容:TO:CDMA2000<BR>每当设备发过来一个数据,我就把它放入unsigned char[]也可以吗?<BR>你所给的代码好象是把CString 转化成为unsigned char[]吧?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:CDMA2000 回复日期:2003-11-03 15:42:32
<br>内容:当然可以直接放入啊啊,
<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-11-03 15:55:26
<br>内容:TO:CDMA2000,这样写对吗?<BR> CString str_receive=_T("");unsigned char* pDataBuf;<BR>LRESULT CXXXXXDlg:: OnCommNotify(WPARAM wParam,LPARAM lParam)<BR>{<BR> m_pOutbuffer=new char[4096];<BR> memset(m_pOutbuffer, 0, 4096);<BR> if((wParam&EV_RXCHAR)!=EV_RXCHAR)<BR> {<BR> SetEvent(m_hPostMsgEvent);<BR> return 0L;<BR> }<BR> else<BR> {<BR> int i;<BR> DWORD number=ReadComm(m_pOutbuffer,4096);<BR> CString string_receive=_T("");<BR> for( i=0;i<(int)number;i++)<BR> {<BR> string_receive=string_receive+m_pOutbuffer[i];<BR> str_receive+=string_receive;<BR> int nBuflen=str_receive.GetLength();<BR> pDataBuf=new unsigned char[nBuflen];<BR> memcpy(pDataBuf,str_receive,nBuflen);<BR> string_receive=_T("");<BR> }<BR> SetEvent(m_hPostMsgEvent);<BR> }<BR> delete []m_pOutbuffer;<BR> return 0L;<BR>}<BR>
<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-11-03 17:22:01
<br>内容:建议你在接受到数据后使用循环队列缓存,防止在数据速率高的情况下数据丢失
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:红苹果 回复日期:2003-11-05 00:57:35
<br>内容:这样:<BR>unsigned char* pDataBuf=new unsigned char[4096];// 用一个合适大小的全局数组<BR>int DataCount=0;// 接受数据的字节数<BR>LRESULT CXXXXXDlg:: OnCommNotify(WPARAM wParam,LPARAM lParam)<BR>{<BR> if((wParam&EV_RXCHAR)!=EV_RXCHAR)<BR> {<BR> SetEvent(m_hPostMsgEvent);<BR> return 0L;<BR> }<BR> else<BR> {<BR> DataCount=ReadComm(pDataBuf,4096);<BR> SetEvent(m_hPostMsgEvent);<BR> }<BR> return 0L;<BR>}<BR>
<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-11-05 08:51:43
<br>内容:TO:红苹果<BR>我用的是DWORD CXXXXXDlg::ReadComm(char *buf, DWORD dwLength)函数,是从网上下的啊!<BR>'ReadComm' : cannot convert parameter 1 from 'unsigned char *' to 'char *'
<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-11-05 08:57:34
<br>内容:你可以转换一下呀,DataCount=ReadComm((char*)pDataBuf,4096);
<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-11-05 17:29:35
<br>内容:串口通信用控件是比较方便而且简单的,如果你对串口的实时性要求比较简单的话,用控件是最简单、而且快速的方法。也可以用winAPI函数,如果用辅助线程就稍微麻烦一点。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:kevin 回复日期:2003-11-05 17:36:28
<br>内容:串口读进来的数据本来就是字节流,怎么还转来转去的呢??!!<BR>晕啊:)
<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-11-05 17:40:57
<br>内容: 你用数组多麻烦啊,你为什么不用CByteArray呢?很方便的,来一个数据方一个进去,而且可以有很多的方法对其进行操作,比数组方便多了。还可以不受限制的加入任意个数的数据。 <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 + -