Home > Net >  Why run to find a serial port when the prompt says com1 port was not found, also, this code has a pr
Why run to find a serial port when the prompt says com1 port was not found, also, this code has a pr

Time:09-23

Imports System. IO. Ports
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 interface

CodePudding user response:

CodePudding user response:

Tried didn't find the shrimps

CodePudding user response:

refer to the second floor weixin_46142047 response:
no com can choose, serialport is added in the interface

Not com can choose system used in the building Lord, there is no serial port; The original poster to find a computer with a serial port or install a virtual serial port,
Serialport1 default serial number is a serial port 1, so the execution Serial_Port1 () will be an error after com1 port was not found,

CodePudding user response:

Open the words can show com1, because the initialization, added to the combobox, but when sending data suggest com1 serial port is not available, that s the point of this program

CodePudding user response:

refer to 6th floor weixin_46142047 response:
open words can really show the com1, because the initialization, added to the combobox, but when sending data suggest com1 serial port is not available, then this program and what's the meaning of it

A serial port com1, not with a serial port resources are occupied by the other application, the building Lord open device manager check the port state,

CodePudding user response:

No serial port, manually add

You machine, there is no serial port, please use the "my computer" - right, "management", "equipment management" view the presence of a serial port

If there is no serial port to debug, please use VSPD virtual serial port simulation