找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 10244|回复: 10
打印 上一主题 下一主题
收起左侧

安卓通过USBHost和stm32通信源代码,包括安卓端和stm32端

  [复制链接]
跳转到指定楼层
楼主
以前的学习结果,为了f429,现贡献给大家,大家可以直接用我这程序当做安卓的串口调试助手,望大家喜欢,帮我加油顶贴啊!
给大家一个提示,这个安卓程序要求你的安卓设备支持Host功能,并且root,打开了usbhost权限哈。




  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. //在此处处理用户数据接收。
  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****/
复制代码

stm32f103虚拟串口程序.zip (368.2 KB, 下载次数: 73)

安卓端Host程序.zip (1.33 MB, 下载次数: 62)





分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:127084 发表于 2016-6-17 15:32 | 只看该作者
回复
求解,怎么实现的。从前看到有人用蓝牙当串口就感觉很高端~

随着USB的普及,串口的局限性,串口在产品设计中使用的越来越少,甚至在近几年的一部分台式机和绝大多数笔记本电脑都不带串口接口了。不过老一代的设备还没有淘汰,比如串口打印机,还有各种工业的控制器等等,它们还是用的串口接口,那么就导致了老设备在新计算机上面无法使用,为了解决这一尴尬情况,USB协会制定了CDC标准,即:用USB通信来模拟串口通信,在计算机端就会直接识别为串口,原来开发的软件不用做任何修改就可以继续使用。
回复

使用道具 举报

板凳
ID:130178 发表于 2016-7-12 15:16 | 只看该作者
您好,这个很棒 谢谢
回复

使用道具 举报

地板
ID:123924 发表于 2018-1-10 11:38 | 只看该作者
如何收藏??只好回复保存
回复

使用道具 举报

5#
ID:282409 发表于 2018-2-5 17:30 | 只看该作者
51hei大小 发表于 2016-6-17 15:32
回复
求解,怎么实现的。从前看到有人用蓝牙当串口就感觉很高端~

可以直接使用USB转串口芯片,比如CH340来实现,CH340也是支持安卓系统的,接上就可以直接使用,给安卓手机扩展出一个串口来。
还有一种应用方式是安卓手机作为设备端,需要使用到USB 安卓HOST芯片,可以参考CH559这类单片机来做,也就是单片机的USB做主机,安卓的USB做设备,这样通信。
回复

使用道具 举报

6#
ID:135990 发表于 2018-9-11 17:25 | 只看该作者
需要使用otg线吗
回复

使用道具 举报

7#
ID:184314 发表于 2019-6-11 15:18 | 只看该作者
支持一下,刚好再看这方面的
回复

使用道具 举报

8#
ID:434018 发表于 2019-6-17 12:58 | 只看该作者
It is very good!Thanks!
回复

使用道具 举报

9#
ID:434018 发表于 2019-6-17 16:50 | 只看该作者
It is a good reference.Thanks!
回复

使用道具 举报

10#
ID:734898 发表于 2020-4-22 15:46 来自手机 | 只看该作者
在小米5 MIUI8 安卓6.0上,以及红米k20pro MIUI11 安卓10上,都是打开之后直接闪退,提示拔出了usb设备。。。连接的时候也的确是弹出了应用请求的,不知道是我单片机的问题还是安卓版本不兼容
回复

使用道具 举报

11#
ID:1014633 发表于 2022-3-31 16:22 | 只看该作者
学艺一下 难得看待
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表