Public Class Form1
Public WithEvents SerialPort As New SerialPort Private Sub Form1_Load (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles MyBase. Load
Dim ports As String ()=SerialPort. GetPortNames () Dim port As String
For Each port In ports ComboBox1. Items. The Add (port)
'If Then' test process returns bolean ' 'here is the final port', 'added to the list the available serial port name' End If
Next port 'ComboBox1. SelectedIndex=0 End Sub serial port configuration
Private Sub Serial_Port1 () '. Setting up the serial port parameters SerialPort1 BaudRate=57600 baud rate SerialPort1. DataBits=8 'data bit SerialPort1. StopBits=IO. Ports. StopBits. One' stop bit SerialPort1.. Parity=I/o Ports. The Parity. None 'check digit
End Sub 'close a serial port connection
Private Sub SerialPort_Close ()
Try
SerialPort1. Close Close a serial port
() 'Label3. Text=SerialPort1. IsOpen
If SerialPort1. IsOpen=False Then Label2. Text="is a serial port not connected" Label2. ForeColor=Color. Red
End the If
Catch the ex As Exception MessageBox. Show (ex. Message)
End the Try
End Sub 'open serial port connection
Private Sub SerialPort1_Open ()
Try
SerialPort1. The Open () 'Open the serial port
If SerialPort1. IsOpen=True Then Label2. Text="is a serial port has been opened" Label2. ForeColor=Color. Green
End the If
Catch the ex As Exception MessageBox. Show (ex. Message)
End the Try End Sub
'receiving data
Private Sub Sp_DataReceived ()
Me. Invoke (New EventHandler (AddressOf Sp_Receiving)) receiving data function called '
End Sub Private Sub Sp_Receiving (ByVal sender As Object, ByVal As EventArgs e)
Dim strIncoming As String
Try
TextBox2. Text=Str (Val (TextBox2. Text) + SerialPort1. BytesToRead) If SerialPort1. BytesToRead & gt; 0 Then Threading. Thread. Sleep (100), 'added delay strIncoming=SerialPort1. ReadExisting. ToString' data in a read buffer SerialPort1. DiscardInBuffer ()
TextBox2. Text=strIncoming
End the If
Catch the ex As Exception MessageBox. Show (ex. Message)
End the Try End Sub 'serial search
Private Sub Button1_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles for. Click
Serial_Port1 ()
SerialPort1_Open ()
End Sub
Private Sub BtnSend_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles BtnSend. Click SerialPort1. Write (TextBox1. Text) Sp_DataReceived ()
End Sub
End the Class
CodePudding user response:
The building Lord, after the program runs in the ComboBox1 have content? Add at the interface between the control SerialPort1?CodePudding user response:
No com can choose, serialport is added in the interfaceCodePudding user response:
CodePudding user response:
Tried didn't find the shrimpsCodePudding user response: