我按照CH573的BLE UART例程,的確能夠?qū)崿F(xiàn)與手機(jī)收發(fā)。
但是找了半天沒找到串口3打印的接收數(shù)據(jù)是在哪個(gè)變量?
發(fā)送數(shù)據(jù)又在哪個(gè)變量?
是在app_uart_process()里面嗎? 具體在哪?
我按照CH573的BLE UART例程,的確能夠?qū)崿F(xiàn)與手機(jī)收發(fā)。
但是找了半天沒找到串口3打印的接收數(shù)據(jù)是在哪個(gè)變量?
發(fā)送數(shù)據(jù)又在哪個(gè)變量?
是在app_uart_process()里面嗎? 具體在哪?
您好,主循環(huán)里的app_uart_process()一直在查詢uart_rx_flag,uart_rx_flag為1時(shí)會(huì)調(diào)用tmos_start_task(Peripheral_TaskID, UART_TO_BLE_SEND_EVT, 2);任務(wù)將串口接收到的數(shù)據(jù)通過藍(lán)牙發(fā)送出去,uart_rx_flag在串口中斷中置1。
串口接收的數(shù)據(jù)會(huì)放到app_uart_rx_fifo,
最終的藍(lán)牙發(fā)送函數(shù)是下面這段:
?noti.len?=?read_length;??//發(fā)送長(zhǎng)度 ?noti.pValue?=?GATT_bm_alloc(peripheralConnList.connHandle,?ATT_HANDLE_VALUE_NOTI,?noti.len,?NULL,?0);//申請(qǐng)內(nèi)存 ?if(noti.pValue?!=?NULL) ?{ ?????tmos_memcpy(noti.pValue,?to_test_buffer,?noti.len);//賦值數(shù)據(jù) ?????result?=?ble_uart_notify(peripheralConnList.connHandle,?¬i,?0);//發(fā)送數(shù)據(jù) ?????if(result?!=?SUCCESS)//發(fā)送失敗 ?????{ ??????PRINT("R1:%02x\r\n",?result); ??????send_to_ble_state?=?SEND_TO_BLE_SEND_FAILED; ??????GATT_bm_free((gattMsg_t?*)¬i,?ATT_HANDLE_VALUE_NOTI);?//發(fā)送失敗需釋放內(nèi)存,成功會(huì)自動(dòng)釋放 ??????tmos_start_task(Peripheral_TaskID,?UART_TO_BLE_SEND_EVT,?2); ??????} ???????else??//發(fā)送成功 ??????????{ ????????????send_to_ble_state?=?SEND_TO_BLE_TO_SEND; ????????????//app_fifo_write(&app_uart_tx_fifo,to_test_buffer,&read_length); ????????????//app_drv_fifo_write(&app_uart_tx_fifo,to_test_buffer,&read_length); ?????????????read_length?=?0; ????????????tmos_start_task(Peripheral_TaskID,?UART_TO_BLE_SEND_EVT,?2); ???????????} ?}
十分感謝二樓詳細(xì)的回答。我還有這些疑問:
疑問1、我用BLE UART發(fā)現(xiàn)了一個(gè)問題:我先用手機(jī)跟573連接,然后手機(jī)往573發(fā)了6個(gè)hi,然后,當(dāng)然573的串口3也接收到了。接著我從573的串口3往手機(jī)發(fā)了3次123,手機(jī)收到的內(nèi)容卻是這樣的“hihihihihihi123123123”,就是手機(jī)收到的數(shù)據(jù)不是3個(gè)123,而是包含之前發(fā)出去的6個(gè)hi,這是為什么呢? 是因?yàn)槭瞻l(fā)之前沒有清空FIFO? 還是說收發(fā)都是共用一個(gè)FIFO?
疑問2、我嘗試將從手機(jī)發(fā)到串口3的hi在串口1打印出來。
//ble?uart?service?callback?handler void?on_bleuartServiceEvt(uint16_t?connection_handle,?ble_uart_evt_t?*p_evt) { ????switch(p_evt->type) ????{ ????????case?BLE_UART_EVT_TX_NOTI_DISABLED: ????????????PRINT("%02x:bleuart_EVT_TX_NOTI_DISABLED\r\n",?connection_handle); ????????????break; ????????case?BLE_UART_EVT_TX_NOTI_ENABLED: ????????????PRINT("%02x:bleuart_EVT_TX_NOTI_ENABLED\r\n",?connection_handle); ????????????break; ????????case?BLE_UART_EVT_BLE_DATA_RECIEVED: ????????????PRINT("BLE?RX?DATA?len:%d\r\n",?p_evt->data.length); ????????????//for?notify?back?test ????????????//to?ble ????????????uint16_t?to_write_length?=?p_evt->data.length; ????????????app_drv_fifo_write(&app_uart_rx_fifo,?(uint8_t?*)p_evt->data.p_data,?&to_write_length);//串口接收的數(shù)據(jù)會(huì)放到app_uart_rx_fifo ????????????tmos_start_task(Peripheral_TaskID,?UART_TO_BLE_SEND_EVT,?2); ????????????//end?of?nofify?back?test ????????????//ble?to?uart ????????????app_uart_tx_data((uint8_t?*)p_evt->data.p_data,?p_evt->data.length); ????????????PRINT("%s\r\n",?p_evt->data.p_data); ????????????break; ????????default: ????????????break; ????} }
以上在原例程中,我多加了一句話,
PRINT("%s\r\n", p_evt->data.p_data);
將從手機(jī)發(fā)到串口3的hi在串口1打印出來
但是打印出來的卻是hi7,如圖
多了一個(gè)字符7,
如果我發(fā)的是hello的話,后面還多了幾個(gè)字符,亂碼
這是為啥?怎么才能抓取手機(jī)某次發(fā)過來的數(shù)據(jù)? 接收的數(shù)據(jù)只打印當(dāng)前的消息內(nèi)容,不打印之前已經(jīng)收到的
上面的程序不是單一一個(gè)例程,而是我把BLE UART跟Centperi這兩個(gè)例程合起來了,所以串口1還有其他的打印內(nèi)容
問題1:
默認(rèn)程序收到手機(jī)發(fā)送數(shù)據(jù)后會(huì)通過藍(lán)牙回給app,如果不需要可以屏蔽下面這段代碼:
??uint16_t?to_write_length?=?p_evt->data.length; ????????????app_drv_fifo_write(&app_uart_rx_fifo,?(uint8_t?*)p_evt->data.p_data,?&to_write_length); ????????????tmos_start_task(Peripheral_TaskID,?UART_TO_BLE_SEND_EVT,?2);
問題2:不要打印輸出字符串,遇0才停止,你并不知道0在什么位置,如果要打印字符,可以根據(jù)長(zhǎng)度寫for循環(huán)打印字符,
好的 謝謝。
另外,現(xiàn)在有2個(gè)開發(fā)板,分別是573和582,
573下載了57x例程的CentPeri和BLE UART融合的工程代碼這里稱之為CentPeri_UART工程
582下載了58x例程里面的BLE UART的工程代碼
用手機(jī)分別對(duì)這2個(gè)開發(fā)板都能實(shí)現(xiàn)收發(fā),UART3能看見收發(fā)的數(shù)據(jù)。
我在CentPeri_UART工程里面將582板子的藍(lán)牙地址設(shè)為了白名單,2個(gè)板上電后,能連接上對(duì)方,如下圖UART1口能顯示RSSI
當(dāng)我把串口線都接到板子各自的UART3口,從573發(fā)1234出去,并沒有在582的串口3收到1234,從582發(fā)abcd出去,也沒有在573的串口3收到abcd這是為什么呢? 是我漏改什么了嗎?
您好,默認(rèn)主機(jī)例程是和Peripheral例程通信的,無法與BLE UART例程直接通訊,枚舉服務(wù)需要做出修改,可以發(fā)送郵件至郵箱hy@wch.cn,可以給你發(fā)送一個(gè)在ch582上已經(jīng)修改好主機(jī)的例程參考。
郵件已發(fā),期待你的回復(fù)。謝謝!
您好,郵件已回復(fù),注意查收。
? ? ? 我想問一下#2那里,那段代碼的作用是芯片從串口接收到數(shù)據(jù),然后通過藍(lán)牙發(fā)送到手機(jī)APP嗎?
? ? ??
? ? ?我看這個(gè)進(jìn)入的條件 是說BLE發(fā)送失敗了才會(huì)執(zhí)行下面的代碼,請(qǐng)問為什么是這樣的呢
是的,不過與你截圖中的case不一樣,將串口收到的數(shù)據(jù)通過藍(lán)牙發(fā)送到app是在SEND_TO_BLE_TO_SEND中,如果發(fā)送失敗了,才會(huì)進(jìn)入SEND_TO_BLE_SEND_FAILED.
? ? ?好的謝謝 我還想問一下就是下面這個(gè)代碼? ?具體是哪一行通過藍(lán)牙將數(shù)據(jù)發(fā)送給手機(jī)APP呢?
? ??
?
可以看下二樓的截圖注釋,先申請(qǐng)內(nèi)存,再賦值數(shù)據(jù),最后發(fā)送數(shù)據(jù),
??result?=?ble_uart_notify(peripheralConnList.connHandle,?¬i,?0);
? ? ? 這個(gè)我知道 但是你剛剛不是說? 將串口收到的數(shù)據(jù)通過藍(lán)牙發(fā)送到app是在SEND_TO_BLE_TO_SEND中,如果發(fā)送失敗了,才會(huì)進(jìn)入SEND_TO_BLE_SEND_FAILED.
? ? ? 但是我先進(jìn)入的這個(gè)SEND_TO_BLE_TO_SEND沒有找到其是如何發(fā)送數(shù)據(jù)的,只有在SEND_TO_BLE_SEND_FAILED中才能找到你說的這一條語句
?result?=?ble_uart_notify(peripheralConnList.connHandle,?¬i,?0);
? ? ??
? ? ? ?如果我一開始就從SEND_TO_BLE_TO_SEND中發(fā)送數(shù)據(jù)成功了 那我就不會(huì)進(jìn)入到SEND_TO_BLE_SEND_FAILED中了呀
但是現(xiàn)在問題就是我在SEND_TO_BLE_TO_SEND沒有找到其發(fā)送數(shù)據(jù)語句
?case?SEND_TO_BLE_TO_SEND: ????????????????//notify?is?not?enabled ????????????????if(!ble_uart_notify_is_ready(peripheralConnList.connHandle)) ????????????????{ ????????????????????if(peripheralConnList.connHandle?==?GAP_CONNHANDLE_INIT) ????????????????????{ ????????????????????????//connection?lost,?flush?rx?fifo?here ????????????????????????app_drv_fifo_flush(&app_uart_rx_fifo); ????????????????????} ????????????????????break; ????????????????} ????????????????read_length?=?ATT_GetMTU(peripheralConnList.connHandle)?-?3; ????????????????if(app_drv_fifo_length(&app_uart_rx_fifo)?>=?read_length) ????????????????{ ????????????????????PRINT("FIFO_LEN:%d\r\n",?app_drv_fifo_length(&app_uart_rx_fifo)); ????????????????????result?=?app_drv_fifo_read(&app_uart_rx_fifo,?to_test_buffer,?&read_length); ????????????????????uart_to_ble_send_evt_cnt?=?0; ????????????????} ????????????????else ????????????????{ ????????????????????if(uart_to_ble_send_evt_cnt?>?10) ????????????????????{ ????????????????????????result?=?app_drv_fifo_read(&app_uart_rx_fifo,?to_test_buffer,?&read_length); ????????????????????????uart_to_ble_send_evt_cnt?=?0; ????????????????????} ????????????????????else ????????????????????{ ????????????????????????tmos_start_task(Peripheral_TaskID,?UART_TO_BLE_SEND_EVT,?4); ????????????????????????uart_to_ble_send_evt_cnt++; ????????????????????????PRINT("NO?TIME?OUT\r\n"); ????????????????????} ????????????????} ????????????????if(APP_DRV_FIFO_RESULT_SUCCESS?==?result) ????????????????{ ????????????????????noti.len?=?read_length; ????????????????????noti.pValue?=?GATT_bm_alloc(peripheralConnList.connHandle,?ATT_HANDLE_VALUE_NOTI,?noti.len,?NULL,?0); ????????????????????if(noti.pValue?!=?NULL) ????????????????????{ ????????????????????????tmos_memcpy(noti.pValue,?to_test_buffer,?noti.len); ????????????????????????result?=?ble_uart_notify(peripheralConnList.connHandle,?¬i,?0);??//數(shù)據(jù)發(fā)送 ????????????????????????if(result?!=?SUCCESS) ????????????????????????{ ????????????????????????????PRINT("R1:%02x\r\n",?result); ????????????????????????????send_to_ble_state?=?SEND_TO_BLE_SEND_FAILED; ????????????????????????????GATT_bm_free((gattMsg_t?*)¬i,?ATT_HANDLE_VALUE_NOTI); ????????????????????????????tmos_start_task(Peripheral_TaskID,?UART_TO_BLE_SEND_EVT,?2); ????????????????????????} ????????????????????????else ????????????????????????{ ????????????????????????????send_to_ble_state?=?SEND_TO_BLE_TO_SEND; ????????????????????????????//app_fifo_write(&app_uart_tx_fifo,to_test_buffer,&read_length); ????????????????????????????//app_drv_fifo_write(&app_uart_tx_fifo,to_test_buffer,&read_length); ????????????????????????????read_length?=?0; ????????????????????????????tmos_start_task(Peripheral_TaskID,?UART_TO_BLE_SEND_EVT,?2); ????????????????????????} ????????????????????} ????????????????????else ????????????????????{ ????????????????????????send_to_ble_state?=?SEND_TO_BLE_ALLOC_FAILED; ????????????????????????tmos_start_task(Peripheral_TaskID,?UART_TO_BLE_SEND_EVT,?2); ????????????????????} ????????????????} ????????????????else ????????????????{ ????????????????????//send_to_ble_state?=?SEND_TO_BLE_FIFO_EMPTY; ????????????????} ????????????????break;
? ? ? 好的感謝?。?!
我想問一下#2藍(lán)牙接收是通過哪里實(shí)現(xiàn)的?藍(lán)牙接收后是通過write寫入tx_fifo,然后readtosameaddr讀取tx_fifo,再通過串口發(fā)送出去嗎?