手机通过蓝牙串口与arduino通信
2011-01-17 21:51
344 查看
前段时间把蓝牙透传模块在arduino上的使用弄好了,今天拿我的手机来测试一下。
我手机是多普达S1,因为是行货,没有wifi,想控制arduino只能通过蓝牙了。
还好wm6操作系统能运行.net2.0开发的移动软件,用serialport控件来作为串口通信的主要工具。
首先把arduino接上蓝牙透传模块,方法在前面的文章提到了,这里就不再叙述。
然后开启手机的蓝牙,搜索蓝牙设备,能找到模块名称,选择之后建立com口,我这里建立的是com6
然后就是在手机上弄个小软件来通信了。
用serialport控件,设置好基本的属性,主要是和arduino的波特率要一致,串口号是com6,基本就没什么问题,我这里很快就能正常通信了。
我把我测试的代码发来让大家了解一下
Imports System.Text
Imports System.IO.Ports
Imports System.Threading
Imports Microsoft.WindowsMobile.Forms
Imports System.Runtime.InteropServices
Public Class Form1
Dim receivedData As String
Private Delegate Sub settexts()
Private Sub sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
Try
Dim bytesRead As Integer = sp.BytesToRead
Dim bytes As Byte() = New Byte(bytesRead + 1) {}
sp.Read(bytes, 0, bytes.Length)
receivedData = System.Text.Encoding.ASCII.GetString(bytes, 0, bytes.Length - 1)
sp.DiscardOutBuffer()
sp.DiscardInBuffer()
Invoke(New settexts(AddressOf settext))
Catch ex As Exception
MessageBox.Show(ex.Message)
sp.Write("sync")
End Try
End Sub
Private Sub settext()
TextBox2.Text = TextBox2.Text & receivedData
TextBox2.ScrollToCaret()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox2.Text = ""
Try
If sp.IsOpen = False Then
TextBox2.Text = "串口关闭"
sp.Open()
End If
sp.Write(Encoding.ASCII.GetBytes(TextBox1.Text & vbCrLf), 0, Encoding.ASCII.GetBytes(TextBox1.Text).Length)
sp.WriteLine("")
'sp.WriteLine(TextBox1.Text)
Catch ex As Exception
TextBox2.Text = ex.Message
sp.Close()
sp.Dispose()
End Try
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
sp.Close()
sp.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connport()
End Sub
Private Sub portname_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles portname.SelectedIndexChanged
connport()
End Sub
Private Sub connport()
Try
sp = New SerialPort(portname.SelectedItem, 9600, Parity.None, 8, StopBits.One)
sp.RtsEnable = True
sp.DtrEnable = True
sp.ReadTimeout = 1000
sp.Open()
If sp.IsOpen Then
TextBox2.Text = "串口连接"
End If
Catch ex As Exception
TextBox2.Text = ex.Message
End Try
End Sub
End Class
这个代码基本上能实现发送数据和同步接收数据并很好的显示出来。但是因为arduino的数据会发送的很频繁,比如时刻反馈温度传感器发送的温度数据、光线传感器发送的光线数据等,使得该软件在接收数据的时候无法操作其他的功能,甚至会假死,也许通过建立线程能解决这个问题,以后再弄哈
我手机是多普达S1,因为是行货,没有wifi,想控制arduino只能通过蓝牙了。
还好wm6操作系统能运行.net2.0开发的移动软件,用serialport控件来作为串口通信的主要工具。
首先把arduino接上蓝牙透传模块,方法在前面的文章提到了,这里就不再叙述。
然后开启手机的蓝牙,搜索蓝牙设备,能找到模块名称,选择之后建立com口,我这里建立的是com6
然后就是在手机上弄个小软件来通信了。
用serialport控件,设置好基本的属性,主要是和arduino的波特率要一致,串口号是com6,基本就没什么问题,我这里很快就能正常通信了。
我把我测试的代码发来让大家了解一下
Imports System.Text
Imports System.IO.Ports
Imports System.Threading
Imports Microsoft.WindowsMobile.Forms
Imports System.Runtime.InteropServices
Public Class Form1
Dim receivedData As String
Private Delegate Sub settexts()
Private Sub sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
Try
Dim bytesRead As Integer = sp.BytesToRead
Dim bytes As Byte() = New Byte(bytesRead + 1) {}
sp.Read(bytes, 0, bytes.Length)
receivedData = System.Text.Encoding.ASCII.GetString(bytes, 0, bytes.Length - 1)
sp.DiscardOutBuffer()
sp.DiscardInBuffer()
Invoke(New settexts(AddressOf settext))
Catch ex As Exception
MessageBox.Show(ex.Message)
sp.Write("sync")
End Try
End Sub
Private Sub settext()
TextBox2.Text = TextBox2.Text & receivedData
TextBox2.ScrollToCaret()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox2.Text = ""
Try
If sp.IsOpen = False Then
TextBox2.Text = "串口关闭"
sp.Open()
End If
sp.Write(Encoding.ASCII.GetBytes(TextBox1.Text & vbCrLf), 0, Encoding.ASCII.GetBytes(TextBox1.Text).Length)
sp.WriteLine("")
'sp.WriteLine(TextBox1.Text)
Catch ex As Exception
TextBox2.Text = ex.Message
sp.Close()
sp.Dispose()
End Try
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
sp.Close()
sp.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connport()
End Sub
Private Sub portname_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles portname.SelectedIndexChanged
connport()
End Sub
Private Sub connport()
Try
sp = New SerialPort(portname.SelectedItem, 9600, Parity.None, 8, StopBits.One)
sp.RtsEnable = True
sp.DtrEnable = True
sp.ReadTimeout = 1000
sp.Open()
If sp.IsOpen Then
TextBox2.Text = "串口连接"
End If
Catch ex As Exception
TextBox2.Text = ex.Message
End Try
End Sub
End Class
这个代码基本上能实现发送数据和同步接收数据并很好的显示出来。但是因为arduino的数据会发送的很频繁,比如时刻反馈温度传感器发送的温度数据、光线传感器发送的光线数据等,使得该软件在接收数据的时候无法操作其他的功能,甚至会假死,也许通过建立线程能解决这个问题,以后再弄哈
相关文章推荐
- 手机通过蓝牙串口与arduino通信
- 用Android手机通过蓝牙模块HC-06连接Arduino串口输出
- BlackBerry 10 , BlackBerry OS 7.1 手机通过蓝牙串口读取Arduino 蓝牙传过来的温度 湿度信息
- android手机通过串口蓝牙透传模块与AVR单片机通信实例。。。蓝牙服务程序案例
- 用Thinkpad T400笔记本计算机和 Nokia 6120C手机通过蓝牙连接上网。
- 树莓派2model B 通过蓝牙实现A2DP协议连接手机播放音乐
- 某些车载蓝牙无法通过PBAP获取手机SIM卡联系人
- 蓝牙转串口的硬件和Android手机的APK设计
- Mac: 通过蓝牙用安卓手机向Mac发送文件
- Uart2Socket:将PC的串口信息通过WiFi网络传输至安卓Android手机上
- UBUTUN 通过蓝牙连接Hoary和诺基亚手机
- 通过串口发送手机短信(C#版)
- 树莓派2model B 通过蓝牙实现A2DP协议连接手机播放音乐
- cc2640 基于官方从机修改的通过手机实现蓝牙点灯例程
- Ubuntu通过蓝牙连接塞班手机的调制解调器接入Internet
- 手机和蓝牙串口的传输方法
- 电脑通过蓝牙适配器连接手机与蓝牙耳机之经验
- STC89C52单片机通过HC-06蓝牙模块与Android手机通信
- Fedora 17如何通过命令行连接蓝牙手机(Nokia E71)
- 教你如何用电脑通过蓝牙连接手机上网