我正在使用AirCable Serial5蓝牙适配器进行RS232到蓝牙传输。我需要将Baudrate从9600更改为115200.我与AirCable供应商谈过,他说,为此,我必须将Baud更改Break命令(0x85)发送到串行COM端口。我不知道如何使用VB.NET中的波特率更改HEX代码发送break命令
Imports System
Imports System.IO.Ports
Imports System.Text
Imports System.Threading
Public Class Form1
Private Sp As New SerialPort
Private comBuffer As Byte()
Private Delegate Sub UpdateFormDelegate()
Private UpdateFormDelegate1 As UpdateFormDelegate
Dim m As Integer
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With Sp
.PortName = "COM8"
.BaudRate = 9600
.DataBits = 8
.Parity = Parity.None
.StopBits = StopBits.One
.Handshake = Handshake.None
End With
Try
Sp.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
AddHandler Sp.DataReceived, AddressOf Sp_DataReceived
Sp.Write("s r0x90 115200" + vbCr)
Thread.Sleep(100)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Sp.Write("s r0x90 9600" + vbCr)
Thread.Sleep(100)
End Sub
Private Sub getBD_Click(sender As System.Object, e As System.EventArgs) Handles getBD.Click
Sp.Write("g r0x90" + vbCr)
Thread.Sleep(100)
End Sub
Private Sub Sp_DataReceived(ByVal Sender As Object, ByVal e As SerialDataReceivedEventArgs)
'Handles serial port data received events
UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
Dim n As Integer = Sp.BytesToRead 'find number of bytes in buffer
m = n
comBuffer = New Byte(n - 1) {} 're dimension storage buffer
Sp.Read(comBuffer, 0, n) 'read data from the buffer
Me.Invoke(UpdateFormDelegate1) 'call the delegate
End Sub
Private Sub UpdateDisplay()
' updates label text
'Label1.Text = ""
Dim b As Integer = 0
Do Until b = m
Dim array() As Byte = {CStr(comBuffer(b))}
Dim value As String = System.Text.ASCIIEncoding.ASCII.GetString(array)
Label1.Text += value
b += 1
Loop
End Sub
Private Sub snd_Click(sender As System.Object, e As System.EventArgs) Handles snd.Click
Dim x As String
x = cmdBox.Text
Sp.Write(x + vbCr)
Thread.Sleep(100)
End Sub
End Class
如果您有任何疑问,请与我们联系。