Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Public Class frmMain
Dim myPort As Array
Delegate Sub SetTextCallback(ByVal [text] As String)
'Serial Port Receiving Code Starts Here ....
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
'Serial Port Receiving Code Ends Here ....
'Serial Port Receiving Code(Invoke) Starts Here ....
Private Sub ReceivedText(ByVal [text] As String)
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text]
Dim fine As String = Mid([text], 7, 3)
Dim list As Integer = ListBox3.Items.Add(fine)
End If
End Sub
End Class
ASKER
ASKER
ASKER
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class Form1
'------------------------------------------------
Dim myPort As Array
Dim buffer As String
Delegate Sub SetTextCallback(ByVal [ADatagram] As String) 'Added to prevent threading errors during receiveing of data
'------------------------------------------------
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.AddRange(myPort)
Button2.Enabled = False
End Sub
'------------------------------------------------
Private Sub ComboBox1_Click(sender As System.Object, e As System.EventArgs) Handles ComboBox1.Click
End Sub
'------------------------------------------------
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
SerialPort1.PortName = ComboBox1.Text
SerialPort1.BaudRate = ComboBox2.Text
SerialPort1.Open()
End Sub
'------------------------------------------------
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
SerialPort1.Close()
Button1.Enabled = True
Button2.Enabled = False
Button4.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim datagram As String
buffer = buffer & SerialPort1.ReadExisting()
datagram = DequeueDatagram()
Do While Not datagram Is Nothing
HandleDatagram(datagram)
datagram = DequeueDatagram()
Loop
End Sub
Private Function DequeueDatagram() As String
Const CARET As String = Chr(63)
Const CARRIAGE_RETURN As String = Chr(10)
Dim caretPosition As Long
Dim carriageReturnPosition As Long
Dim result As String = Nothing
carriageReturnPosition = buffer.IndexOf(CARRIAGE_RETURN)
If (carriageReturnPosition > -1) Then
result = buffer.Substring(0, carriageReturnPosition)
buffer = buffer.Substring(carriageReturnPosition + 1)
caretPosition = result.IndexOf(CARET)
If (caretPosition > -1) Then
' Only return an entire bE datagram.
result = result.Substring(caretPosition)
Else
' Without caret it is never a valid bE formatted message.
' Thus we drop it here.
' TODO: Log invalid datagrams as they may indicate device or transmission errors.
result = Nothing
End If
End If
Return result
End Function
Private Sub HandleDatagram([ADatagram] As String)
If Me.RichTextBox2.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf HandleDatagram)
Me.Invoke(x, New Object() {(ADatagram)})
Else
RichTextBox2.Text &= ADatagram
Dim fine As String = Mid(ADatagram, 7, 3)
Dim list As Integer = ListBox1.Items.Add(fine)
End If
End Sub
End Sub
End Class
ASKER
ASKER
Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,
TRUSTED BY
Then much more important: A serial device must be always threated as a streaming device. Thus you need to read data into a buffer and examine its content. And only process entire messages. E.g.
Open in new window