MFC單文件之串列埠通訊實現16進製制資料的傳送和接收

2021-07-30 12:44:50 字數 3827 閱讀 9942

void cdlg_ci::onbnclickedstart()

void cdlg_ci::onbnclickedloaddata3a()

m_mscomm_ci.put_commport(flag); //選擇com

m_mscomm_ci.put_inbuffersize(1024); //接收緩衝區

m_mscomm_ci.put_outbuffersize(1024);//傳送緩衝區

m_mscomm_ci.put_inputlen(0);//設定當前接收區資料長度為0,表示全部讀取

m_mscomm_ci.put_inputmode(1);//以二進位制方式讀寫資料

m_mscomm_ci.put_rthreshold(1);//接收緩衝區有1個及1個以上字元時,將引發接收資料的oncomm

m_mscomm_ci.put_settings(_t("9600,n,8,1"));//波特率9600無檢驗位,8個資料位,1個停止位

if (!m_mscomm_ci.get_portopen())//如果串列埠沒有開啟則開啟

else

}else

}

這裡要注意的地方是編輯框中的內容是可以直接獲取的,不用新增變數也行,非常方便。

void cdlg::oncommmscomm1()

}buffer.trimleft();

buffer.trimright();

cbytearray receivedata;

int receivedatalen;

receivedatalen = str2hex(buffer, receivedata);

int data36 = receivedata[0] + receivedata[1] + receivedata[2] + receivedata[3] + receivedata[4] + receivedata[5] + receivedata[6] + receivedata[7] + receivedata[8] + receivedata[9]

+ receivedata[10] + receivedata[11] + receivedata[12] + receivedata[13] + receivedata[14] + receivedata[15] + receivedata[16] + receivedata[17] + receivedata[18] + receivedata[19]

+ receivedata[20] + receivedata[21] + receivedata[22] + receivedata[23] + receivedata[24] + receivedata[25] + receivedata[26] + receivedata[27] + receivedata[28] + receivedata[29]

+ receivedata[30] + receivedata[31] + receivedata[32] + receivedata[33] + receivedata[34],

data37 = (data36 & 0xff00) >> 8, data38 = (data36 & 0xff);

if((receivedata[35] == data37) && (receivedata[36] == data38))

if (receivedata[3] == 0x01)

if (receivedata[3] == 0x02)

if (receivedata[3] == 0x03)

}}else

}

這裡加了乙個buffer和strtemp可以實現buffer區域的快取足夠,不然會有buffer too small 的bug。同時用到了兩個函式,把收到的資料轉成16進製制數儲存。

//將字元轉換十六進製制

char cdlg::hexchar(char c)

//將字串轉換成十六進製制

int cdlg::str2hex(cstring str, cbytearray &senddata)

i++;

if (i >= len)

break;

lstr = str[i];

hexdata = hexchar(hstr); //高位轉換

lowhexdata = hexchar(lstr); //低位轉換

if((hexdata==16)||(lowhexdata==16))

break;

else

hexdata = hexdata * 16 + lowhexdata;

i++;

senddata[hexdatalen] = (char)hexdata;

hexdatalen++;

}return hexdatalen;

}

函式定義部分:

放在標頭檔案的public部分:

char cdlg::hexchar(char c);

int cdlg::str2hex(cstring str, cbytearray& senddata);

這裡要把一串資料中的其中兩個拼接起來,本來是想著把得到的16進製制陣列中的元素再轉化為字串的,後來發現這樣做倒不如直接在開始就提取出來,因為想要的資料位置知道。

如下:

void ccardnum::oncommmscommcardnum()

strcard.format("%s%02x", strcard, *(rxdata + 3));

strcard.format("%s%02x", strcard, *(rxdata + 4));

}buffer.trimleft();

buffer.trimright();

cbytearray receivedata;

int receivedatalen;

receivedatalen = str2hex(buffer, receivedata);

int data36 = receivedata[0] + receivedata[1] + receivedata[2] + receivedata[3] + receivedata[4] + receivedata[5] + receivedata[6] + receivedata[7] + receivedata[8] + receivedata[9]

+ receivedata[10] + receivedata[11] + receivedata[12] + receivedata[13] + receivedata[14] + receivedata[15] + receivedata[16] + receivedata[17] + receivedata[18] + receivedata[19]

+ receivedata[20] + receivedata[21] + receivedata[22] + receivedata[23] + receivedata[24] + receivedata[25] + receivedata[26] + receivedata[27] + receivedata[28] + receivedata[29]

+ receivedata[30] + receivedata[31] + receivedata[32] + receivedata[33] + receivedata[34],

data37 = (data36 & 0xff00) >> 8, data38 = (data36 & 0xff);

if ((receivedata[35] == data37) && (receivedata[36] == data38))

}else

}

串列埠通訊實現

前段時間編寫了串列埠通訊的 用的是rs 232資料線 include 標準輸入輸出定義 include 標準函式庫定義 include unix 標準函式定義 include include include include include include define dev dev ttys1 d...

用MFC編寫串列埠通訊,實現資料採集功能

硬體連線 工控機 rs232線 力感測器 pt600顯示儀表 rs232線將顯示儀表和工控機相連 程式設計預定功能 實現mfc所寫軟體介面中編輯框能夠實時顯示儀表所顯示的數值 3.第一步 將儀表的各項引數設定好,包括資料位 波特率 校驗位 停止位等,然後利用串列埠除錯助手,測試所連線的硬體通訊是否暢...

VC 串列埠通訊實現方式

利用師姐qt2440開發板控制機械臂,串列埠通訊出現問題,找到usb轉串列埠後,任然找不到相應的驅動。初步打算用微控制器實現。1.基於active控制項的方式 mscomm 優點是 直接利用控制項,在串列埠有資料到達時,會促發相應的事件響應函式,然後你可以在實踐響應函式裡,進行資料的讀取。缺點是 資...