在VC中使用SerialPort類實現串口通訊

來源:互聯網
上載者:User

在VC中實現串口通訊有3中方法,mscomm 、api 和串口類。

但在實際的編程中MSComm控制項不好用,api則比較麻煩。建議使用串口類SerialPort(請到網路中搜尋下載)
串口類SerialPort的用法如下:
在主對話方塊的'.h'檔案添加以下內容:
#include "SerialPort.h"
CSerialPort m_ComPort;    //串口類對象
afx_msg void OnCommunication(WPARAM ch, LPARAM port);

在主對話方塊的''.cpp'檔案添加以下內容:
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)

//開啟串口
if (m_ComPort.InitPort(this,1,9600,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
m_ComPort.StartMonitoring();  //啟動串口監視線程
else
AfxMessageBox("沒有發現,或被其它裝置佔用");
return TRUE;  // return TRUE  unless you set the focus to a control

//資料發送
void C XX Dlg::OnBuSend()
{
char str[128]="4646xfgsdgsgs";
str[2]='/xaf';              //16進位資料的發送。
m_ComPort.WriteToPort(str);
}

//資料接收
void C XX Dlg::OnCommunication(WPARAM ch, LPARAM port)
{
m_rece+=ch;
UpdateData(false);    //更新資料
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.