I have an external ham radio that is controlled through serial string commands that return a data string of varying length but usually < 20 characters. Any terminal program like PuTTY can be used. I want to write a master-slave type of program that does not have to be event driven. I have tried several approached in VB NET (VS 2019) and although I can send string commands, the Read routines never work, the routines fail with timeouts. The input buffer remains untouched because when I close the debug VB NET program and open PuTTY, it immediately reads the input buffer showing all the replies to the VB NET Write string commands. My code for test purposed defines the COM Port for my specific setup (COM5, 9600, 8,1, no flow). I have tried different SerialPort Readxxxx string routines such as ReadExisting, Readline, ReadTo but the routines do not even see any bytes in the input buffer… it always shows 0 bytes. Any advice would be greatly appreciated.
VB.NET:
Imports System
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim WithEvents MyPort As SerialPort = New System.IO.Ports.SerialPort("COM5", 9600, Parity.None, 8, StopBits.One)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bufsize1 As Integer = 99 'used as value other than zero
Dim readstring1 As String = ""
If Not MyPort.IsOpen Then
MyPort.Open()
End If
Try
MyPort.Write("DATE" + Chr(13))
Thread.Sleep(200)
MyPort.ReadTimeout = 1000
bufsize1 = MyPort.BytesToRead
TextBox1.Text = CStr(bufsize1)
readstring1 = MyPort.ReadExisting
TextBox2.Text = readstring1
Catch ex As TimeoutException
TextBox1.Text = "NoData"
MsgBox(ex.Message)
End Try
End Sub
End Class