手机通过蓝牙串口与arduino通信

前段时间把蓝牙透传模块在arduino上的使用弄好了,今天拿我的手机来测试一下。

我手机是多普达S1,因为是行货,没有wifi,想控制arduino只能通过蓝牙了。

还好wm6操作系统能运行.net2.0开发的移动软件,用serialport控件来作为串口通信的主要工具。

首先把arduino接上蓝牙透传模块,方法在前面的文章提到了,这里就不再叙述。

然后开启手机的蓝牙,搜索蓝牙设备,能找到模块名称,选择之后建立com口,我这里建立的是com6

然后就是在手机上弄个小软件来通信了。

用serialport控件,设置好基本的属性,主要是和arduino的波特率要一致,串口号是com6,基本就没什么问题,我这里很快就能正常通信了。

我把我测试的代码发来让大家了解一下

手机通过蓝牙串口与arduino通信_第1张图片

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的数据会发送的很频繁,比如时刻反馈温度传感器发送的温度数据、光线传感器发送的光线数据等,使得该软件在接收数据的时候无法操作其他的功能,甚至会假死,也许通过建立线程能解决这个问题,以后再弄哈

你可能感兴趣的:(exception,object,Integer,手机,button,textbox)