Imports System.Data
Imports System.Data.SqlClient
Imports VB = Microsoft.VisualBasic
Imports System.IO.Ports
Imports System.Threading
Imports System.ComponentModel
Imports System
Public Class Form1
Dim myPort As Array
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = ""
txtSN.Enabled = False
btSend.Enabled = False
myPort = IO.Ports.SerialPort.GetPortNames()
cmbBaud.Items.Add(9600)
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)
For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0) 'Set cmbPort text to the first COM port detected
cmbBaud.Text = cmbBaud.Items.Item(4) 'Set cmbBaud text to the first Baud rate on the list
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
If SerialPort1.IsOpen = True Then
SerialPort1.Close()
btnConnect.Text = "connect"
txtSN.Enabled = False
btSend.Enabled = False
Else
SerialPort1.PortName = cmbPort.Text 'Set SerialPort1 to the selected COM port at startup
SerialPort1.BaudRate = cmbBaud.Text 'Set Baud rate to the selected value on
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8 'Open our serial port
SerialPort1.Open()
btnConnect.Text = "disconnect"
txtSN.Enabled = True
btSend.Enabled = True
End If
If SerialPort1.IsOpen = True Then
TextBox1.Text = TextBox1.Text & vbCrLf & "Process" & " : " & "connect success with " & SerialPort1.PortName & " / " & SerialPort1.BaudRate
Else
TextBox1.Text = TextBox1.Text & vbCrLf & "process" & " : " & "connect loss"
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.txtOutput.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.txtOutput.Text &= [text]
End If
''compares the ID of the creating Thread to the ID of the calling Thread
'If Me.txtSN.InvokeRequired Then
' Dim x As New SetTextCallback(AddressOf ReceivedText)
' Me.Invoke(x, New Object() {(text)})
'Else
' Me.txtSN.Text &= [text]
'End If
End Sub
Private Sub send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSend.Click
'SerialPort1.WriteLine(txtSN.Text.Trim)
SerialPort1.Write(txtSN.Text & vbCrLf)
TextBox1.Text = TextBox1.Text & vbCrLf & " send command : " & txtSN.Text
txtSN.Text = ""
End Sub
End Class
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.txtOutput.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.txtOutput.Text &= [text]
End If
''compares the ID of the creating Thread to the ID of the calling Thread
'If Me.txtSN.InvokeRequired Then
' Dim x As New SetTextCallback(AddressOf ReceivedText)
' Me.Invoke(x, New Object() {(text)})
'Else
' Me.txtSN.Text &= [text]
'End If
End Sub