手機通過藍牙串口與arduino通信

前段時間把藍牙透傳模塊在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的數據會發送的很頻繁,比如時刻反饋溫度傳感器發送的溫度數據、光線傳感器發送的光線數據等,使得該軟件在接收數據的時候無法操作其他的功能,甚至會假死,也許通過建立線程能解決這個問題,以後再弄哈

發佈了31 篇原創文章 · 獲贊 77 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章