到目前为止,我将发布我的代码。
我正在向微控制器发送数据,但现在我要检查其他方式并希望接收数据。
我编写的代码本身就是UART
。我正在使用readexisting并且能够读取一些不重要的字符,但是我无法读取所需的数据。
Imports System.IO.Ports
Public Class Settings
Dim myComPort As New SerialPort
' Call a routine to write a command to turn on an LED and read the response.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SendCommand("7")
End Sub
' Call a routint write a command to turn on an LED and read the response.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SendCommand("A")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SendCommand("C")
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
myComPort.BaudRate = CInt(cmbBitRate.SelectedItem)
TextBox4.Text = myComPort.BaudRate
End Sub
' If myComPort is open, finish transmitting.
' Exiting the Using block closes the port and releases its resources.
Sub CloseComPort()
Try
Using myComPort
If (Not (myComPort Is Nothing)) Then
' The COM port exists.
If myComPort.IsOpen Then
' Wait for the transmit buffer to empty.
Do While (myComPort.BytesToWrite > 0)
Loop
End If
End If
End Using
Catch ex As UnauthorizedAccessException
' The port may have been removed. Ignore.
End Try
End Sub
' Set the BaudRate property of myComPort to match the bit rate selected in the combo box.
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBitRate.SelectedIndexChanged
myComPort.BaudRate = CInt(cmbBitRate.SelectedItem)
'TextBox2.Text = ToString(myComPort.BaudRate)
End Sub
' If the previously selected COM port is open, close it.
' Set the PortName property of myComPort to match the port selected in the combo box.
' Call a routine to open the port.
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPorts.SelectedIndexChanged
CloseComPort()
myComPort.BaudRate = CInt(cmbBitRate.SelectedItem)
' myComPort.BaudRate = 500
'TextBox2.Text = myComPort.BaudRate
OpenComPort()
End Sub
' Call a routine to close the COM port.
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
CloseComPort()
End Sub
' Call routines to initalize the form and open the selected COM port.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
InitializeForm()
OpenComPort()
AddHandler myComPort.DataReceived, AddressOf DataReceivedeventHandler
End Sub
' Set up the form and select a default port and bit rate.
Sub InitializeForm()
Dim bitRates(9) As Integer
Dim nameArray() As String
' Find the COM ports on the system.
nameArray = SerialPort.GetPortNames
Array.Sort(nameArray)
' Fill a combo box with the port names.
cmbPorts.DataSource = nameArray
cmbPorts.DropDownStyle = ComboBoxStyle.DropDownList
' Select a default port.
'cmbPorts.SelectedIndex = 1
'Bit rates to select from.
bitRates(0) = 300
bitRates(1) = 600
bitRates(2) = 1200
bitRates(3) = 2400
bitRates(4) = 9600
bitRates(5) = 14400
bitRates(6) = 19200
bitRates(7) = 38400
bitRates(8) = 57600
bitRates(9) = 115200
'Place the bit rates in a combo box.
cmbBitRate.DataSource = bitRates
cmbBitRate.DropDownStyle = ComboBoxStyle.DropDownList
' Select a default bit rate.
' If (Not (cmbBitRate Is Nothing)) Then
'CloseComPort()
'End If
End Sub
' Set port parameters and open the COM port
' associated with the SerialPort object myComPort.
Sub OpenComPort()
Try
' Get the selected COM port's name from the combo box.
If Not myComPort.IsOpen Then
myComPort.PortName = cmbPorts.SelectedItem.ToString
' Get the selected bit rate from the combo box.
If cmbBitRate.SelectedIndex > 0 Then
myComPort.BaudRate = CInt(cmbBitRate.SelectedItem)
TextBox4.Text = myComPort.BaudRate
End If
'myComPort.BaudRate = CInt(cmbBitRate.SelectedItem)
' Set other port parameters.
myComPort.Parity = Parity.None
myComPort.DataBits = 8
myComPort.StopBits = StopBits.One
myComPort.Handshake = Handshake.None
myComPort.ReadTimeout = 3000
myComPort.WriteTimeout = 5000
' Open the port.
myComPort.Open()
End If
Catch ex As InvalidOperationException
MessageBox.Show(ex.Message)
Catch ex As UnauthorizedAccessException
MessageBox.Show(ex.Message)
Catch ex As System.IO.IOException
MessageBox.Show(ex.Message)
End Try
End Sub
' Write a command to the SerialPort object and read the response.
''' <param name= "command"> The command to send. </param>
Private Sub SendCommand(ByVal command As String)
' Dim response As String
Try
'TextBox1.BackColor = Color.MediumPurple
myComPort.Write(command)
'myComPort.ReadTimeout = 1000
' While myComPort.ReadLine = 0
' End While
'If myComPort.IsOpen Then
'myComPort.ReadLine()
'End If
'TextBox6.Text = myComPort.ReadLine
Select Case command
Case "A"
' TextBox6.Text = myComPort.
TextBox1.Text = "Green LED is on"
TextBox1.BackColor = Color.LightGreen
Case "7"
TextBox1.Text = " Red LED is ON"
TextBox1.BackColor = Color.Red
'TextBox6.Text = myComPort.ReadLine
Case "C"
TextBox1.Text = " Both LED's are ON"
TextBox1.BackColor = Color.RoyalBlue
'TextBox6.Text = myComPort.ReadLine
Case Else
End Select
Catch ex As TimeoutException
MessageBox.Show(ex.Message)
Catch ex As InvalidOperationException
MessageBox.Show(ex.Message)
Catch ex As UnauthorizedAccessException
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub DataReceivedeventHandler(
ByVal sender As Object,
ByVal e As SerialDataReceivedEventArgs)
Dim sp As SerialPort = CType(sender, SerialPort)
Dim indata As String = sp.ReadExisting()
' Dim jndata As String = sp.ReadChar
MsgBox(indata)
'TextBox6.Text = CStr(vbMsgBoxRtlReading)
' MsgBox(jndata)
' Console.WriteLine("Data Received:")
Console.Write(indata)
' ArgumentException.Equals(indata)
End Sub
Public Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
End Sub
'Private Sub DataReceivedEventHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
' Throw New NotImplementedException
'End Sub
End Class