Home

Site Map

Contact RAC

Join RAC

Search

Français


  Log Into Members' Section  
Register Mbr. No.: Password: Forgot your password?

Elecraft Power Meter Code
RAC
Imports System.IO.Ports
Imports System
Imports System.Math

Public Class Form1
    Dim WithEvents myComPort As New IO.Ports.SerialPort
    Dim ForwardPower As String
    Dim ReflectedPower As String
    Dim ReturnLoss As String
    Dim Swr As String
    Dim x As String  'dummy variable


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        ' Loads On Start Up
        'Searches for COM Ports And Adds them to ComboBox

        For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
            cbbComPorts.Items.Add(My.Computer.Ports.SerialPortNames(i))
        Next
    End Sub

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Label5.Click

    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnConnect.Click
        'Open and configure Port
        If myComPort.IsOpen Then
            myComPort.Close()
        End If

        With myComPort
            .ReadTimeout = 3000
            .PortName = cbbComPorts.Text()
            .BaudRate = 9600  ' fixed by the Elecraft software
            .Parity = Parity.None
            .StopBits = StopBits.One
            .Handshake = Handshake.None
            .DataBits = 8
        End With
        'Open Port
        myComPort.Open()
    End Sub

    Private Sub btnMeasure_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMeasure.Click
        If myComPort.IsOpen Then

            'Forward Power
            x = myComPort.ReadExisting  'empty comport buffer
            myComPort.Write("F")
            System.Threading.Thread.Sleep(100)  'wait for a response
            ForwardPower = myComPort.ReadExisting
            ForwardPower = ForwardPower.Substring(1, 4)

            ' calibrate here (Now just a trial)
            ForwardPower = ForwardPower + 1   'add 1 watt
            txtbPf.Text = ForwardPower

            'Reflected Power
            x = myComPort.ReadExisting  'empty comport buffer
            myComPort.Write("R")
            System.Threading.Thread.Sleep(100)  'wait for a response
            ReflectedPower = myComPort.ReadExisting
            ReflectedPower = ReflectedPower.Substring(1, 4)
            txtbPr.Text = ReflectedPower

            'SWR/Return Loss calculation
            Swr = (1 + Math.Sqrt(ReflectedPower) / Math.Sqrt(ForwardPower)) / (1 - Math.Sqrt(ReflectedPower) / Math.Sqrt(ForwardPower))

            txtbSwr.Text = Swr
            ReturnLoss = (Swr - 1) / (Swr + 1)
            ReturnLoss = -20.0 * Math.Log10(ReturnLoss)
            txtbRl.Text = ReturnLoss
        Else
            MsgBox(" Please Open a ComPort")
        End If
    End Sub

    Private Sub btnToggleLeds_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToggleLeds.Click
        If myComPort.IsOpen Then
            myComPort.Write("L")

        Else
            MsgBox(" Please Open a Comport")
        End If
    End Sub



    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If myComPort.IsOpen Then

            Select Case ComboBox1.SelectedItem
                Case "B: Forward Power Bar Graph"
                    x = myComPort.ReadExisting
                    myComPort.Write("B")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "C: Reverse Power Bar Graph"
                    x = myComPort.ReadExisting
                    myComPort.Write("C")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "D: SWR Bar Graph"
                    x = myComPort.ReadExisting
                    myComPort.Write("D")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "F: Forward Power"
                    x = myComPort.ReadExisting
                    myComPort.Write("F")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "L: Toggle LED's ON/OFF"
                    x = myComPort.ReadExisting
                    myComPort.Write("L")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "M: Toggle Average/PEP LED's"
                    x = myComPort.ReadExisting
                    myComPort.Write("M")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "N : Toggle Serial Data,Average/PEP"
                    x = myComPort.ReadExisting
                    myComPort.Write("N")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "P: Toggle LED Bar Graph"
                    x = myComPort.ReadExisting
                    myComPort.Write("P")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "R: Reverse Power"
                    x = myComPort.ReadExisting
                    myComPort.Write("R")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "S: SWR"
                    x = myComPort.ReadExisting
                    myComPort.Write("S")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting


                Case "U: Request EEPROM Settings"
                    x = myComPort.ReadExisting
                    myComPort.Write("U")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "V: FirmWare Version"
                    x = myComPort.ReadExisting
                    myComPort.Write("V")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "W: Write To EEPROM"
                    x = myComPort.ReadExisting
                    myComPort.Write("W")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "X: Request Current User Settings in RAM"
                    myComPort.Write("X")
                    x = myComPort.ReadExisting
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting

                Case "Z: Streaming"
                    x = myComPort.ReadExisting
                    myComPort.Write("Z")
                    System.Threading.Thread.Sleep(100)
                    rtbCommandResponse.Text = myComPort.ReadExisting
            End Select
        Else
            MsgBox(" Please Open ComPort")
        End If

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
        myComPort.Close()
        Me.Close()
    End Sub
End Class