Android藍芽串口程式開發,android程式開發

來源:互聯網
上載者:User

Android藍芽串口程式開發,android程式開發



本文主要介紹了針對android的藍芽串口上位機開發。

一、幀定義

android用戶端按照一定的資料幀格式通過藍芽串口發送資料到串連到MCU的藍芽從機,MCU接收到資料後,按照幀格式的定義,接收資料幀,並解析資料幀,得到需要的資料。

android用戶端按照以下幀格式來發送和接收資料。




1.1用戶端發送的資料幀內容

發送頻率:10Hz



幀校正的和是從幀長開始到幀校正前所有資料的和對256取餘。即sum%256。

pitch roll yaw資料各16位,由高低8位組成。MCU接收資料時需要將其再複合成一個16位元據。

0x5A:幀頭

14:針長度

0xA1:幀功能,標明這幀資料是pitch yaw roll內容。

pitch_set_H:pitch高8位

pitch_set_L:pitch低8位

...

...

幀校正:(幀長+幀功能+資料)%256

0xA5:幀尾。

(註:幀是這麼定義的,但實際上編寫接收程式的時候,我將幀尾0xA5當作了一幀資料的幀頭。這樣幀頭就變成了 0xA5 0x5A。此時就不存在幀尾了)

下面是android用戶端發送位元組資訊的程式:

<span style="white-space:pre"></span>b[0] = (byte)0XA5;  //幀頭         b[1] = (byte)14;     //幀長         b[2] = (byte)0xA1;   //幀功能                 b[3] = (byte)(pitchSet>>8);     //資料         b[4] = (byte)(pitchSet);         b[5] = (byte)(rollSet>>8);         b[6] = (byte)(rollSet);         b[7] = (byte)(yawSet>>8);         b[8] = (byte)(yawSet);         b[9] = (byte)0;         b[10] = (byte)0;         b[11] = (byte)0;         b[12] = (byte)0;                 int temp = 0;         for(int j=1;j<13;j++)         {         temp = temp+b[j];         }         b[13] = (byte)(temp%256);  //校正             b[14] = (byte)0x5A;     //幀結束

1.2MCU接收幀資料

由於串口是按照一個一個位元組的格式發送資料,所以MCU接收資料時,要按照用戶端發送的幀格式的定義,找到一幀資料的開始與結束,確認一幀資料接收無誤後,再將其中的資料解析出來。使用一個為來標誌(狀態機器)接收程式對一陣資料的接收狀態。

編寫的DSP2808接收程式如下:

定義資料類型

typedef struct{int status;   //data statusint Rx_Data; //the receivced dataint data[15];//int rx_buf[13];int buf_index;int sum;int yawSet;int pitchSet;int rollSet;}Sci_Data;Sci_Data sci_data;

interrupt void SCIB_RX_ISR()          //以中斷的方式接收資料{//接收程式sci_data.Rx_Data = ScibRegs.SCIRXBUF.all;        ScibRegs.SCIFFRX.bit.RXFFOVRCLR = 1;  //clear overflow flagScibRegs.SCIFFRX.bit.RXFFINTCLR = 1;   //clear the interrupt flagPieCtrlRegs.PIEACK.all |= PIEACK_GROUP9;  //enable more interrupt from PIE group 9Decode_Frame(sci_data.Rx_Data);          //資料幀解碼}void Decode_Frame(int rx_data){if(sci_data.status==0 && rx_data == 0x5A)   //判斷幀尾 如果找到幀尾,status=1{sci_data.status = 1;}else if(sci_data.status == 1 &&rx_data == 0xA5)      //幀尾下一個字元是幀頭,status=2{sci_data.status = 2;}else if(sci_data.status ==2) //幀頭後是13個位元組資料 從幀長到幀校正{if(sci_data.buf_index<13){sci_data.rx_buf[sci_data.buf_index] = rx_data;        //  0-12 to save data length function data... sumif(sci_data.buf_index<12) //求取校正和{sci_data.sum += sci_data.rx_buf[sci_data.buf_index];}sci_data.buf_index++;}if(sci_data.buf_index >= 13){if(sci_data.rx_buf[12] == sci_data.sum%256){int j=0;for(j=0;j<10;j++){sci_data.data[j] = sci_data.rx_buf[j+2];    //save data to the data buf}sci_data.pitchSet = sci_data.data[0]<<8|sci_data.data[1];}sci_data.buf_index = 0;sci_data.status = 0;sci_data.sum = 0;}}else {sci_data.buf_index = 0;sci_data.status = 0;sci_data.sum = 0;}}

這樣DSP2808就接收到了從用戶端發送來的資料。儲存在sci_data.pitchSet、sci_data.rollSet、sci_data.yawSet三個變數中。 


二、android用戶端開發流程

問題:檢測藍芽的串連狀態

android藍芽串口程式狀態機器

在程式中,通過broadcast來檢測藍芽的串連狀態,根絕藍芽的串連狀態來決定操作。

開啟藍芽---掃描裝置---選擇裝置---串連裝置---串連正常---發送資料---






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.