找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 13732|回復(fù): 10
打印 上一主題 下一主題
收起左側(cè)

安卓通過(guò)USBHost和stm32通信源代碼,包括安卓端和stm32端

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
以前的學(xué)習(xí)結(jié)果,為了f429,現(xiàn)貢獻(xiàn)給大家,大家可以直接用我這程序當(dāng)做安卓的串口調(diào)試助手,望大家喜歡,幫我加油頂貼!
給大家一個(gè)提示,這個(gè)安卓程序要求你的安卓設(shè)備支持Host功能,并且root,打開(kāi)了usbhost權(quán)限哈。




  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  MCD Application Team
  5.   * @version V4.0.0
  6.   * @date    21-January-2013
  7.   * @brief   Virtual Com Port Demo main file
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * [b]© COPYRIGHT 2013 STMicroelectronics[/b]
  12.   *
  13.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14.   * You may not use this file except in compliance with the License.
  15.   * You may obtain a copy of the License at:
  16.   *
  17.   *        [url]http://www.st.com/software_license_agreement_liberty_v2[/url]
  18.   *
  19.   * Unless required by applicable law or agreed to in writing, software
  20.   * distributed under the License is distributed on an "AS IS" BASIS,
  21.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22.   * See the License for the specific language governing permissions and
  23.   * limitations under the License.
  24.   *
  25.   ******************************************************************************
  26.   */


  27. /* Includes ------------------------------------------------------------------*/
  28. #include "hw_config.h"
  29. #include "usb_lib.h"
  30. #include "usb_desc.h"
  31. #include "usb_pwr.h"

  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. /* Private macro -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Extern variables ----------------------------------------------------------*/
  37. /* Private function prototypes -----------------------------------------------*/
  38. /* Private functions ---------------------------------------------------------*/

  39. /*******************************************************************************
  40. * Function Name  : main.
  41. * Description    : Main routine.
  42. * Input          : None.
  43. * Output         : None.
  44. * Return         : None.
  45. *******************************************************************************/
  46. void delay_ms(unsigned int m)
  47. {
  48.         while(m--);
  49. }
  50. /*******************************************************************************
  51. * Function Name  : USB_To_USART_Send_Data.
  52. * Description    : send the received data from USB to the UART 0.
  53. * Input          : data_buffer: data address.
  54.                    Nb_bytes: number of bytes to send.
  55. * Return         : none.
  56. *******************************************************************************/
  57. void USB_To_USART_Send_Data(uint8_t* data_buffer, uint8_t Nb_bytes)
  58. {
  59. //在此處處理用戶數(shù)據(jù)接收。
  60. }
  61. int main(void)
  62. {
  63.   Set_System();
  64.   Set_USBClock();
  65.   USB_Interrupts_Config();
  66.   USB_Init();
  67.   while (1)
  68.   {
  69.                 delay_ms(50);
  70.                 USART_To_USB_Send_Data((unsigned char *)"Hello",6);
  71.   }
  72. }
  73. #ifdef USE_FULL_ASSERT
  74. /*******************************************************************************
  75. * Function Name  : assert_failed
  76. * Description    : Reports the name of the source file and the source line number
  77. *                  where the assert_param error has occurred.
  78. * Input          : - file: pointer to the source file name
  79. *                  - line: assert_param error line source number
  80. * Output         : None
  81. * Return         : None
  82. *******************************************************************************/
  83. void assert_failed(uint8_t* file, uint32_t line)
  84. {
  85.   /* User can add his own implementation to report the file name and line number,
  86.      ex: printf("Wrong parameters value: file %s on line %d", file, line) */

  87.   /* Infinite loop */
  88.   while (1)
  89.   {}
  90. }
  91. #endif

  92. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
復(fù)制代碼

stm32f103虛擬串口程序.zip (368.2 KB, 下載次數(shù): 73)

安卓端Host程序.zip (1.33 MB, 下載次數(shù): 62)





分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:127084 發(fā)表于 2016-6-17 15:32 | 只看該作者
回復(fù)
求解,怎么實(shí)現(xiàn)的。從前看到有人用藍(lán)牙當(dāng)串口就感覺(jué)很高端~

隨著USB的普及,串口的局限性,串口在產(chǎn)品設(shè)計(jì)中使用的越來(lái)越少,甚至在近幾年的一部分臺(tái)式機(jī)和絕大多數(shù)筆記本電腦都不帶串口接口了。不過(guò)老一代的設(shè)備還沒(méi)有淘汰,比如串口打印機(jī),還有各種工業(yè)的控制器等等,它們還是用的串口接口,那么就導(dǎo)致了老設(shè)備在新計(jì)算機(jī)上面無(wú)法使用,為了解決這一尷尬情況,USB協(xié)會(huì)制定了CDC標(biāo)準(zhǔn),即:用USB通信來(lái)模擬串口通信,在計(jì)算機(jī)端就會(huì)直接識(shí)別為串口,原來(lái)開(kāi)發(fā)的軟件不用做任何修改就可以繼續(xù)使用。
回復(fù)

使用道具 舉報(bào)

板凳
ID:130178 發(fā)表于 2016-7-12 15:16 | 只看該作者
您好,這個(gè)很棒 謝謝
回復(fù)

使用道具 舉報(bào)

地板
ID:123924 發(fā)表于 2018-1-10 11:38 | 只看該作者
如何收藏??只好回復(fù)保存
回復(fù)

使用道具 舉報(bào)

5#
ID:282409 發(fā)表于 2018-2-5 17:30 | 只看該作者
51hei大小 發(fā)表于 2016-6-17 15:32
回復(fù)
求解,怎么實(shí)現(xiàn)的。從前看到有人用藍(lán)牙當(dāng)串口就感覺(jué)很高端~

可以直接使用USB轉(zhuǎn)串口芯片,比如CH340來(lái)實(shí)現(xiàn),CH340也是支持安卓系統(tǒng)的,接上就可以直接使用,給安卓手機(jī)擴(kuò)展出一個(gè)串口來(lái)。
還有一種應(yīng)用方式是安卓手機(jī)作為設(shè)備端,需要使用到USB 安卓HOST芯片,可以參考CH559這類單片機(jī)來(lái)做,也就是單片機(jī)的USB做主機(jī),安卓的USB做設(shè)備,這樣通信。
回復(fù)

使用道具 舉報(bào)

6#
ID:135990 發(fā)表于 2018-9-11 17:25 | 只看該作者
需要使用otg線嗎
回復(fù)

使用道具 舉報(bào)

7#
ID:184314 發(fā)表于 2019-6-11 15:18 | 只看該作者
支持一下,剛好再看這方面的
回復(fù)

使用道具 舉報(bào)

8#
ID:434018 發(fā)表于 2019-6-17 12:58 | 只看該作者
It is very good!Thanks!
回復(fù)

使用道具 舉報(bào)

9#
ID:434018 發(fā)表于 2019-6-17 16:50 | 只看該作者
It is a good reference.Thanks!
回復(fù)

使用道具 舉報(bào)

10#
ID:734898 發(fā)表于 2020-4-22 15:46 來(lái)自手機(jī) | 只看該作者
在小米5 MIUI8 安卓6.0上,以及紅米k20pro MIUI11 安卓10上,都是打開(kāi)之后直接閃退,提示拔出了usb設(shè)備。。。連接的時(shí)候也的確是彈出了應(yīng)用請(qǐng)求的,不知道是我單片機(jī)的問(wèn)題還是安卓版本不兼容
回復(fù)

使用道具 舉報(bào)

11#
ID:1014633 發(fā)表于 2022-3-31 16:22 | 只看該作者
學(xué)藝一下 難得看待
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表