I have received a few requests for an example of CI-V programming using
Visual Basic .Net. While my VB is pretty rusty, I decided to try and
convert one of my examples from C# to VB.Net. The conversion was very
straightforward. As with the C# example this has minimal error checking
and would need better error checking, collision detection, and retries for
failed commands in a true production quality implementation
Here is what I ended up with.
Imports System.IO.Ports
Imports System.Text.RegularExpressions
Module Module1
Public Class ICOM7300
Private ReadOnly port As String
Private ReadOnly baudRate As Long
Private serialPort As SerialPort
Public Sub New(portValue As String, baudRateValue As Long)
port = portValue
baudRate = baudRateValue
serialPort = Nothing
End Sub
Public Sub Open()
serialPort = New SerialPort(port, baudRate, Parity.None, 8, StopBits.One)
' Throws an exception if the serial port fails to open
serialPort.Open()
serialPort.ReadTimeout = 500
serialPort.WriteTimeout = 500
End Sub
Public Sub Close()
If serialPort IsNot Nothing Then
If (serialPort.IsOpen) Then
serialPort.Close()
End If
serialPort.Dispose()
End If
serialPort = Nothing
End Sub
Public Function OperatingFrequency() As Integer
If serialPort Is Nothing Or serialPort.IsOpen = False Then
Throw New Exception("Serial port is not open to get OperatingFrequency")
End If
Dim request() As Byte = New Byte() {&HFE, &HFE, &H94, &HE0, &H3, &HFD}
serialPort.Write(request, 0, request.Length)
Dim response As List(Of Byte)
response = GetResponse()
Dim frequency As Long
frequency = (response(5) And &HF) + (((response(5) >> 4) And &HF) * 10) +
((response(6) And &HF) * 100) + (((response(6) >> 4) And &HF) * 1000) +
((response(7) And &HF) * 10000) + (((response(7) >> 4) And &HF) * 100000) +
((response(8) And &HF) * 1000000) + (((response(8) >> 4) And &HF) * 10000000) +
((response(9) And &HF) * 100000000) + (((response(9) >> 4) And &HF) * 1000000000)
Return frequency
End Function
Private Function GetResponse() As List(Of Byte)
If serialPort Is Nothing Then Return Nothing
Dim response As List(Of Byte)
response = FillBuffer()
While response(2) <> &HE0 Or response(3) <> &H94 or response(4) <> &H3
response = FillBuffer()
End While
Return response
End Function
Private Function FillBuffer() As List(Of Byte)
Dim response As List(Of Byte) = New List(Of Byte)()
Dim inByte As Byte
inByte = serialPort.ReadByte() And &HFF
While (inByte <> &HFD)
response.Add(inByte)
inByte = serialPort.ReadByte() And &HFF
End While
response.Add(inByte)
Return response
End Function
End Class
Function ValidateComPort(port As String) As Boolean
Dim ports As String()
ports = SerialPort.GetPortNames()
For Each comPort As String In ports
If (comPort.Equals(port, StringComparison.OrdinalIgnoreCase)) Then
Return True
End If
Next
Return False
End Function
Sub Main(args As String())
Dim port As String = ""
Dim baudRate As String = "115200"
Dim ic7300 As ICOM7300
For Each arg As String In args
If ValidateComPort(arg) Then
port = arg
ElseIf Regex.IsMatch(arg, "^\d+$") Then
baudRate = arg
Else
' show usage And return
Console.WriteLine("usage: Set-7300Clock comport <baudrate>")
Return
End If
Next
' We must have a port. Everything else can be default
If port.Equals("") Then
' show usage And return
Console.WriteLine("usage: Set-7300Clock comport <baudrate>")
Return
End If
ic7300 = New ICOM7300(port, Long.Parse(baudRate))
Try
ic7300.Open()
Dim frequency As Long = ic7300.OperatingFrequency
Console.WriteLine("Operating frequency is {0}", frequency)
Catch ex As Exception
Console.WriteLine("Error getting operating frequency")
Console.WriteLine(ex.Message)
Console.WriteLine("Make sure that {0} is the port the IC-7300 is connected to and not in use by any other software.", port)
Finally
ic7300.Close()
End Try
End Sub
End Module