?? 串口助手程序編程過程2.htm
字號:
if((hexdata==16)||(lowhexdata==16))<br>
break;<br>
else <br>
hexdata=hexdata*16+lowhexdata;<br>
i++;<br>
senddata[hexdatalen]=(char)hexdata;<br>
hexdatalen++;<br>
}<br>
senddata.SetSize(hexdatalen);<br>
return hexdatalen;<br>
}<br>
<br>
//這是一個將字符轉換為相應的十六進制值的函數<br>
//好多C語言書上都可以找到<br>
//功能:若是在0-F之間的字符,則轉換為相應的十六進制字符,否則返回-1<br>
char CSCommTestDlg::ConvertHexChar(char ch) <br>
{<br>
if((ch>='0')&&(ch<='9'))<br>
return ch-0x30;<br>
else if((ch>='A')&&(ch<='F'))<br>
return ch-'A'+10;<br>
else if((ch>='a')&&(ch<='f'))<br>
return ch-'a'+10;<br>
else return (-1);<br>
}</font></p>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000"> </font></p>
</blockquote>
</blockquote>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000"> 再將CSCommTestDlg::OnButtonManualsend()修改成以下形式:</font></p>
<blockquote>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">void CSCommTestDlg::OnButtonManualsend() <br>
{<br>
// TODO: Add your control notification handler code here<br>
UpdateData(TRUE); //讀取編輯框內容<br>
if(m_ctrlHexSend.GetCheck())<br>
{<br>
CByteArray hexdata;<br>
int len=String2Hex(m_strTXData,hexdata); //此處返回的len可以用于計算發送了多少個十六進制數<br>
m_ctrlComm.SetOutput(COleVariant(hexdata)); //發送十六進制數據<br>
}<br>
else <br>
m_ctrlComm.SetOutput(COleVariant(m_strTXData));//發送ASCII字符數據<br>
<br>
}<br>
</font>
</p>
</blockquote>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">現在,你先將串口線接好并打開串口調試助手V2.1,選上以十六制顯示,設置好相應串口,然后運行我們這個程序,在發送框中輸入00
01 02 03 A1 CC等十六進制字符,并選上以十六進制發送,單擊手動發送,在串口調試助手的接收框中應該可以看到00
01 02 03 A1 CC了。</font></p>
<p align="left" style="margin-top: 3; margin-bottom: 3"> </p>
<p align="left" style="margin-top: 3; margin-bottom: 3"><a name="9.在接收框中以十六進制顯示"><b><font color="#000000">9.在接收框中以十六進制顯示</font></b></a></p>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">
這就容易多了: 在主對話框中加入一個復選接鈕,IDC_CHECK_HEXDISPLAY Caption:
十六進制顯示,再利用ClassWizard為其添加控制變量:m_ctrlHexDiaplay。
然后修改CSCommTestDlg::OnComm()函數:</font></p>
<blockquote>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">void CSCommTestDlg::OnComm() <br>
{<br>
// TODO: Add your control notification handler code here<br>
VARIANT variant_inp;<br>
COleSafeArray safearray_inp;<br>
LONG len,k;<br>
BYTE rxdata[2048]; //設置BYTE數組 An 8-bit integerthat is not signed.<br>
CString strtemp;<br>
if(m_ctrlComm.GetCommEvent()==2) //事件值為2表示接收緩沖區內有字符<br>
{<br>
variant_inp=m_ctrlComm.GetInput(); //讀緩沖區<br>
safearray_inp=variant_inp; //VARIANT型變量轉換為ColeSafeArray型變量<br>
len=safearray_inp.GetOneDimSize(); //得到有效數據長度<br>
for(k=0;k<len;k++)<br>
safearray_inp.GetElement(&k,rxdata+k);//轉換為BYTE型數組<br>
for(k=0;k<len;k++) //將數組轉換為Cstring型變量<br>
{<br>
BYTE bt=*(char*)(rxdata+k); //字符型<br>
if(m_ctrlHexDisplay.GetCheck())<br>
strtemp.Format("%02X ",bt); //將字符以十六進制方式送入臨時變量strtemp存放,注意這里加入一個空隔<br>
else <br>
strtemp.Format("%c",bt); //將字符送入臨時變量strtemp存放<br>
<br>
m_strRXData+=strtemp; //加入接收編輯框對應字符串 <br>
}<br>
}<br>
UpdateData(FALSE); //更新編輯框內容<br>
}</font></p>
</blockquote>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">測試:在串口調試助手發送框中輸入00
01 02 03 A1 CC等十六進制字符,并選上以十六進制發送,單擊手動發送,在本程序運行后選上以十六進制顯示,在串口調試助手中單擊手動發送或自動發送,則在本程序的接收框中應該可以看到00
01 02 03 A1 CC了。</font></p>
<p align="left" style="margin-top: 3; margin-bottom: 3"> </p>
<p align="left" style="margin-top: 3; margin-bottom: 3"><a name="10.如何設置自動發送"><b><font color="#000000">10.如何設置自動發送</font></b></a></p>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">
最簡單的設定自動發送周期是用SetTimer()函數,這在數據采集中很有用,在控制中指令的傳送也可能用到定時發送。</font></p>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">
方法是:在ClassWizard中選上MessageMap卡,然后在Objects IDs選中CSCommTestDlg類,再在Messages框中選上WM_TIMER消息,單擊ADD_FUNCTION加入void CSCommTestDlg::OnTimer(UINT nIDEvent)
函數,這個函數是放入“時間到”后要處理的代碼:</font></p>
<blockquote>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">void CSCommTestDlg::OnTimer(UINT nIDEvent) <br>
{<br>
// TODO: Add your message handler code here and/or call default<br>
OnButtonManualsend();<br>
CDialog::OnTimer(nIDEvent);<br>
}<br>
</font></p>
</blockquote>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">再在在主對話框中加入一個復選接鈕,ID為IDC_CHECK_AUTOSEND Caption:
自動發送(周期1秒),再利用ClassWizard為其添加BN_CLICK消息處理函數void
CSCommTestDlg::OnCheckAutosend():</font></p>
<blockquote>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">void
CSCommTestDlg::OnCheckAutosend() <br>
{<br>
// TODO: Add your control notification handler code here<br>
m_bAutoSend=!m_bAutoSend;<br>
if(m_bAutoSend)<br>
{<br>
SetTimer(1,1000,NULL);//時間為1000毫秒<br>
}<br>
else<br>
{<br>
KillTimer(1); //取消定時<br>
}<br>
}</font></p>
</blockquote>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">其中:m_bAutoSend為BOOL型變量,在CLASSVIEW中為CSCommTestDlg類加入,并在構造函數中初始化:</font></p>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">
m_bAutoSen=FALSE;<br>
現在可以運行程序測試了。<br>
</font>
</p>
<p align="left" style="margin-top: 3; margin-bottom: 3"> </p>
<p align="left" style="margin-top: 3; margin-bottom: 3"><b><a name="11.什么是VARIANT數據類型?如何使用VARIANT數據類型?"><font color="#000000">11.什么是VARIANT數據類型?如何使用VARIANT數據類型?</font></a></b></p>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">
不知如何使用VARIANT數據類型, 有不少朋友對VARIANT這個新的數據類型大感頭疼。SetOutput()函數中
需要的VARIANT參數還可以使用COleVariant類的構造函數簡單生成,現在GetInput()函數的返回值也成了VARIANT類型,那么如何從返回的值中提取有用的內容。
VARIANT及由之而派生出的COleVariant類主要用于在OLE自動化中傳遞數據。實際上VARIANT也只不過是一個新定義的結構罷了,它的主要成員包括一個聯合體及一個變量。該聯合體由各種類型的數據成員構成,
而該變量則用來指明聯合體中目前起作用的數據類型。我們所關心的接收到的數據就存儲在該聯合體的某個數據成員中。
該聯合體中包含的數據類型很多,從一些簡單的變量到非常復雜的數組和指針。由于通過串口接收到的內容常常是一個字節串,我們將使用其中的某個數組或指針來訪問接收到的數據。這里推薦給大家的是指向一個SAFEARRAY(COleSafeArray)類型變量。新的數據類型SAFEARRAY正如其名字一樣,是一個“安全數組”,它能根據系統環境自動調整其16位或32
位的定義,并且不會被OLE改變(某些類型如BSTR在16位或32位應用程序間傳遞時會被OLE翻譯從而破壞其中的二進制數據)。大家無須了解SAFEARRAY的具體定義,只要知道它是另外一個結構,其中包含一個
(void *)類型的指針pvData,其指向的內存就是存放有用數據的地方。
簡而言之,從GetInput()函數返回的VARIANT類型變量中,找出parray
指針,再從該指針指向的SAFEARRAY變量中找出pvData指針,就可以向訪問數組一樣取得所接收到的數據了。具體應用請參見void
CSCommTestDlg::OnComm()函數。</font></p>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">
大概我現在也說不清這個問題,我自己從第一次接觸這個東西,到現在還是給別人講不清。</font></p>
<p align="left" style="margin-top: 3; margin-bottom: 3"> </p>
<p align="left" style="margin-top: 3; margin-bottom: 3"><font color="#000000">另:二進制收發設置請參考<a href="../scomm/scmscomm.htm">MSComm控件說明</a>。</font></p>
<p align="left" style="margin-top: 3; margin-bottom: 3"> </p>
<p align="left" style="margin-top: 3; margin-bottom: 3"> </p>
<p align="center" style="margin-top: 3; margin-bottom: 3"><a href="#串口調試助手源程序"><font color="#000000">返回頂部</font></a></p>
<p> </TD>
</tr>
<tr>
<TD width=100% valign="top">
</TD>
</tr>
</TBODY></TABLE>
<TABLE border=0 cellPadding=1 cellSpacing=1 width=550>
<TBODY>
<TR>
<TD>
</TD></TR>
<TR>
<TD vAlign=top width=424>
<p align="center"> </TD>
</TR>
<TR>
<TD>
</TD></TR>
<TR>
<TD vAlign=top width=424></TD>
</TR>
</TBODY></TABLE> <BR></TD>
<TD align=right background=../picindex/zhe.gif height=443 vAlign=top
width=10 rowspan="2"> </TD>
<TD align=middle bgColor=#cbe4e4 vAlign=top width=150>
</TD></TR>
<tr>
<TD align=middle bgColor=#cbe4e4 vAlign=top width=150 height="18">
</TD>
</tr>
</TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=758 height="28">
<TBODY>
<TR>
<TD height="16" valign="middle" background="../picindex/looker.gif">
<p align="right">
<FONT
color=#cc3300> </FONT>
</TD></TR>
<tr>
<TD height="45">
<p align="center">轉載本站原版內容,請注明作者,并說明來自http://www.gjwtech.com
</TD>
</tr>
</TBODY></TABLE>
<p align="center">
</p>
</BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -