VB6 is obsolete and yes, it used the MSCOMM control.
You can, however, easily use Visual Basic .Net and even get it for free included in Visual Studio 20xx Comunity Edition.
Here, you create a for example a Form1, a Button1 and a SerialPort1.
To send "PING" and hopefully receive a "PONG", you would code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SerialPort1.BaudRate = "115200"
SerialPort1.DataBits = "8"
SerialPort1.Handshake = "N"
SerialPort1.StopBits = "1"
SerialPort1.PortName = "COM1"
SerialPort1.Open()
SerialPort1.Write("PING")
Dim received As String
received = SerialPort1.ReadLine
SerialPort1.Close()
End Sub
End Class
Note that the tricky part of RS232 communication is knowing when to start listening and knowing when the transmission ended.
Often, some kind of protocol is used, where you know that you received everything: either you know it, because the reply to a given command has so many Bytes, or the transmission is ended with a special character (normally LF and/or CR). Alternatively, you could use a protocol where the first Byte tells you how many Bytes will follow.
My example above is the most simple one: it assumes that after sending a string, you will get a string ("line") back, which terminates with LFCR.
This has a big problem: if your device decides to not return the expected answer, your VB .Net app will wait forever for the incoming reply and basically get stuck. To handle this, you can use a TRY/CATCH exception and a TIME OUT. If the reply does not arrive in so many milliseconds, an error is produced and handled by TRY/CATCH.
This won't work well if you are expecting n Bytes but only received n-x Bytes. In this case your exception will kick in, but the device might be stuck trying to send the remaining Bytes.
As you see, implementing RS232 communication can be very easy, but when shit hits the fan, you start having all sorts of problems. For example, the SerialPort control is an asynchronous process. If you are filling for instance an array with the incoming Bytes, you cannot just use that array in another SUB, as it might be still used by the SerialPort... This is why I prefer to send/receive data in the same subroutine, instead of setting up a sub that handles the event of incoming Bytes.
Here is the mandatory example produced by ChatGPT, which I do recommend for your purpose (ChatGPT - not necessarily the example)! It is much better than browsing for examples on Google, because you can request exactly what you need.
Imports System.IO.Ports
Public Class Form1
Private serialPort As New SerialPort()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Set up serial port properties
serialPort.PortName = "COM1" ' Change to your COM port number
serialPort.BaudRate = 9600 ' Change to your baud rate
serialPort.DataBits = 8
serialPort.Parity = Parity.None
serialPort.StopBits = StopBits.One
serialPort.Handshake = Handshake.None
' Open the serial port
Try
serialPort.Open()
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
Dim dataToSend As String = txtSend.Text
If serialPort.IsOpen Then
' Send the string to the serial port
serialPort.Write(dataToSend)
' Clear the send textbox
txtSend.Text = ""
Else
MessageBox.Show("Serial port is not open")
End If
End Sub
Private Sub serialPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles serialPort.DataReceived
' Read the data from the serial port
Dim dataReceived As String = serialPort.ReadExisting()
' Append the received data to the receive textbox
txtReceive.AppendText(dataReceived)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
' Close the serial port
If serialPort.IsOpen Then
serialPort.Close()
End If
End Sub
End Class
Kind regards,
Vitor