求大神指教关于arduino蓝牙通信问题
用的是arduinounor3的板子,想做一个蓝牙与android手机通信,让手机实时监控arduino串口的检测信息(如红外、温湿度等),蓝牙用的HC-06,代码如下I...
用的是arduino uno r3的板子,想做一个蓝牙与android手机通信,让手机实时监控arduino串口的检测信息(如红外、温湿度等),蓝牙用的HC-06,代码如下
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
但是运行一直有错,错误显示:
sketch_may03b.ino:1:1: error: 'Imports' does not name a type
编译有误。
求大神帮帮忙,谢谢! 展开
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
但是运行一直有错,错误显示:
sketch_may03b.ino:1:1: error: 'Imports' does not name a type
编译有误。
求大神帮帮忙,谢谢! 展开
1个回答
leipole
2024-11-29 广告
2024-11-29 广告
作为上海雷普电气有限公司的一员,我们深知温度控制在工业领域的重要性。Arduino温度控制系统凭借其灵活性和易用性,成为我们温控方案中的重要组成部分。该系统通过Arduino控制器与温度传感器相连,实时监测并调整温度,实现精确的温度控制。无...
点击进入详情页
本回答由leipole提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询