串列埠傳送資料

2021-07-12 01:29:46 字數 2453 閱讀 6967

s  串列埠程式設計 詳解4 串列埠傳送資料

串列埠傳送資料報括:

十六進製制傳送資料

手動傳送資料

自動傳送資料

手動傳送資料的**如下:

[cpp]view plain

copy

//手動傳送

long

tx_count=0;  

void

cscommdlg::onbuttonmanualsend()   

else

else

//傳送ascii文字

cstring strtemp;  

strtemp.format("tx: %d"

,tx_count);  

m_ctrltxcount.setwindowtext(strtemp);//顯示計數     

//  wparam ch='a';       //測試用

//  oncommunication(ch,1);  

}     

cfile m_rfile;  

//lpcstr是win32和vc++所使用的一種字串資料型別。lpcstr被定義成是乙個指向以null(『\0』)結尾的常量字元的指標。

lpcstr

lpszpath=m_strcurpath;

// "c:\\comdata"

setcurrentdirectory(lpszpath);            

}  

**分析:

傳送資料是很簡單的,只用了m_port.writetoport(data,len);就夠啦。

這裡也涉及到是否是十六進製制傳送資料,如果是 則先把十六進製制轉換為字元再進行傳送,轉換得呼叫str2hex( )函式。 其實這個函式名為hextostr( )更合適

[cpp]view plain

copy

//將乙個字串作為十六進製制串轉化為乙個位元組陣列,位元組間可用空格分隔

//返回轉換後的位元組陣列長度,同時位元組陣列長度自動設定

//將 一串十六進製制數轉換為字串(原理:兩個十六進製制數轉化為乙個字元  )

intcscommdlg::str2hex(cstring str, 

char

*data)  

i++;  

if(i>=len)  

break

;  l=str[i];//低位

t=hexchar(h);  

t1=hexchar(l);  

if((t==16)||(t1==16))  

break

;  else

t=t*16+t1;  //把兩位的十六進製制數轉換為乙個字元

i++;  

data[rlen]=(char

)t;  

rlen++;  

}  return

rlen;  

}  

[cpp]view plain

copy

char

cscommdlg::hexchar(

char

c)    

自動傳送處理

如果選擇了自動傳送,則應該使用定時器來實現自動周期性地傳送的功能,**如下

選擇自動傳送按鈕的**,為idc_check_autosend新增處理函式如下

[cpp]view plain

copy

void

cscommdlg::oncheckautosend()   

else

settimer(1,m_ncycletime,null);  

}  else

}  定時器**

[cpp]view plain

copy

void

cscommdlg::ontimer(

uint

nidevent)   

cdialog::ontimer(nidevent);  

}  

當然也要考慮,當傳送輸入的編輯框中的內容改變時,應及時將串列埠傳送的內容改變,因此應為編輯框idc_edit_send新增en_chance相應函式。這個我是想不全的。

[cpp]view plain

copy

void

cscommdlg::onchangeeditsend()     

與此同時,如果傳送週期改變時,應該重新用設定定時時間

[cpp]view plain

copy

void

cscommdlg::onchangeeditcycletime()     

以上的**理解和使用應該都不是些什麼問題

串列埠向電腦傳送資料

串列埠向電腦傳送資料 include include delay.h 呼叫延時函式 void uartinit void 以上部分為使用燒錄軟體得來編碼 void uart init init 初始化 void uart sendbyte unsigned char byte void main 串...

ARMlinux串列埠傳送

首先選擇串列埠 位置方便,不和串列埠終端重合,定義char 為串列埠節點目錄 dev tty ttysac3 char uart3 dev ttysac3 然後定義乙個傳送資料buffer char buffer hello world n 開啟串列埠,呼叫配置函式set opt 自己定義的 if ...

串列埠傳送接收浮點型資料

最近正在除錯nrf24l01無線通行模組,由於要fpga與pc機相互通訊,pc機沒spi,所以要個開發板 資料傳輸流程如下 上位機 串列埠 arm開發板 spi nrf24l01 另乙個nrf24l01 spi fpga開發板。其中遇到乙個頭疼的問題是傳輸的都是浮點型數,但傳送接受的是unsigne...