Public Class Main
    '*----------------------------------------------------------------------
    '* (c) 2008 Microstrain, Inc.
    '*----------------------------------------------------------------------
    '* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING 
    '* CUSTOMERS WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER 
    '* FOR THEM TO SAVE TIME. AS A RESULT, MICROSTRAIN SHALL NOT BE HELD LIABLE 
    '* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY 
    '* CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY 
    '* CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH 
    '* THEIR PRODUCTS.
    '*----------------------------------------------------------------------
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'quit app
        End
    End Sub

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'populate comm port combo box
        Dim X As Integer
        For X = 1 To 32
            cmbCommPort.Items.Add(Trim(Str(X)))
        Next
        'set numeric up/down box at default
        NumUpDown.Value = 100
        'Sets a value indicating whether to catch calls on the wrong thread that access a control's Handle property 
        cmbCommPort.CheckForIllegalCrossThreadCalls = False
    End Sub

    Private Sub NumUpDown_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumUpDown.ValueChanged
        'get value to write to eeprom
        MyDataRateValue = Math.Round(51200 / NumUpDown.Value)
        'display data rate value
        txtDataRateValue.Text = MyDataRateValue
    End Sub

    Private Function ReportEvent(ByVal MyEvent As String) As Boolean
        'add event to text
        If txtEvent.Text = "" Then
            txtEvent.Text = MyEvent
        Else
            txtEvent.Text = txtEvent.Text + Chr(13) + Chr(10) + MyEvent
        End If
    End Function

    Private Sub SetDataRateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetDataRateToolStripMenuItem.Click
        'set error handler
        On Error GoTo ErrorHandler

        'test for comm port selection
        If cmbCommPort.Text = "" Then
            'event
            Call ReportEvent("Comm Port not selected")
            'exit
            Exit Sub
        End If

        If SetDataRateToolStripMenuItem.Checked = False Then
            'event
            Call ReportEvent("Set Data Rate attempted")
            'set menu check
            SetDataRateToolStripMenuItem.Checked = True
            'disable combo
            cmbCommPort.Enabled = False
            'set serial port number
            SerialPort1.PortName() = "COM" + Trim(cmbCommPort.Text)
            'set receive threshold
            SerialPort1.ReceivedBytesThreshold = 5
            'open serial port
            SerialPort1.Open()
            'populate byte array with EEPROM Write command; EEPROM location 0xFCA2
            ReDim arrBytes(0 To 7)
            arrBytes(0) = 228
            arrBytes(1) = 193
            arrBytes(2) = 41
            arrBytes(3) = 0
            arrBytes(4) = 64674 \ 256
            arrBytes(5) = 64674 Mod 256
            arrBytes(6) = CDbl(txtDataRateValue.Text) \ 256
            arrBytes(7) = CDbl(txtDataRateValue.Text) Mod 256
            'send command
            SerialPort1.Write(arrBytes, 0, 8)
        ElseIf SetDataRateToolStripMenuItem.Checked = True Then
            'open serial port
            SerialPort1.Close()
            'event
            Call ReportEvent("Set Data Rate stopped")
            'set menu check
            SetDataRateToolStripMenuItem.Checked = False
            'enable combo
            cmbCommPort.Enabled = True
        End If

        'exit
        Exit Sub
ErrorHandler:
        'set menu check
        SetDataRateToolStripMenuItem.Checked = False
        'close comm port
        If SerialPort1.IsOpen = True Then SerialPort1.Close()
        'enable combo
        cmbCommPort.Enabled = True
        'event
        Call ReportEvent("Set Data Rate errored: " + Err.Description)
        'exit sub
        Exit Sub
    End Sub

    Private Function CheckSum() As Boolean
        'set error handler
        On Error GoTo ErrorHandler

        '-----calc TheirCheckSum
        'dim
        Dim X As Double
        'zero variable
        TheirCheckSum = 0
        'calc TheirCheckSum
        For X = 0 To 2
            TheirCheckSum = TheirCheckSum + CDbl(arrBytes(X))
        Next X

        'handle CheckSum rollover
        TheirCheckSum = TheirCheckSum Mod 65536

        'calc MyCheckSum
        MyCheckSum = (CDbl(arrBytes(3)) * 256) + arrBytes(4)

        'compare checksums
        If MyCheckSum = TheirCheckSum Then
            ''set checksum return
            CheckSum = True
        Else
            'set checksum return
            CheckSum = False
        End If

        'exit
        Exit Function
ErrorHandler:
        'set checksum return
        CheckSum = False
        'exit
        Exit Function
    End Function

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        'set error handler
        On Error GoTo ErrorHandler

        'set variable
        UpperLimitOfArray = SerialPort1.BytesToRead - 1
        'prep array
        ReDim arrBytes(0 To UpperLimitOfArray)
        'get comm buffer
        SerialPort1.Read(arrBytes, 0, SerialPort1.BytesToRead)

        'test for no byte return
        If UBound(arrBytes) = -1 Then
            'set menu check
            SetDataRateToolStripMenuItem.Checked = False
            'close comm port
            If SerialPort1.IsOpen = True Then SerialPort1.Close()
            'enable combo
            cmbCommPort.Enabled = True
            'event
            Call ReportEvent("Set Data Rate failed")
            'exit
            Exit Sub
        End If

        'test 5 byte return and header
        If Not UBound(arrBytes) = 4 And Not arrBytes(0) = 228 Then
            'set menu check
            SetDataRateToolStripMenuItem.Checked = False
            'close comm port
            If SerialPort1.IsOpen = True Then SerialPort1.Close()
            'enable combo
            cmbCommPort.Enabled = True
            'event
            Call ReportEvent("Set Data Rate failed")
            'exit
            Exit Sub
        End If

        'test checksum
        If CheckSum() = False Then
            'set menu check
            SetDataRateToolStripMenuItem.Checked = False
            'close comm port
            If SerialPort1.IsOpen = True Then SerialPort1.Close()
            'enable combo
            cmbCommPort.Enabled = True
            'event
            Call ReportEvent("Set Data Rate failed")
            'exit
            Exit Sub
        End If

        '-----------------success
        'set menu check
        SetDataRateToolStripMenuItem.Checked = False
        'close comm port
        If SerialPort1.IsOpen = True Then SerialPort1.Close()
        'enable combo
        cmbCommPort.Enabled = True
        'event
        Call ReportEvent("Set Data Rate succeeded")

        Exit Sub
ErrorHandler:
        'set menu check
        SetDataRateToolStripMenuItem.Checked = False
        'close comm port
        If SerialPort1.IsOpen = True Then SerialPort1.Close()
        'enable combo
        cmbCommPort.Enabled = True
        'event
        Call ReportEvent("Set Data Rate errored")
        'exit
        Exit Sub
    End Sub
End Class
