Option Explicit On   ' Each variable must be declared well

Imports Quicker


Public Class Form1

    Dim is_try_ok As Byte   ' try ... catch state, 1: Ok, 0: error
    Dim temp_int16 As Int16  ' temporary used int16 value
    Dim temp_int32 As Int32  ' temporary used int32 value
    Dim temp_single As Single  ' temporary used 32-bit float value


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' enable timer1
        Timer1.Interval = 250  ' 250 ms
        Timer1.Enabled = True

    End Sub


    ' convert 8017H's value (-32768 ~ +32767) to float value (-10.0 ~ +10.0)
    Private Function convert_8017(ByVal val As Int16) As Single
        If (val >= 0) Then
            convert_8017 = Convert.ToSingle(val) * 10.0F / 32767.0F
        Else
            convert_8017 = Convert.ToSingle(val) * 10.0F / 32768.0F
        End If
    End Function


    ' convert 87024's value (-32768 ~ +32767) to float value (-10.0 ~ +10.0)
    Private Function convert_87024_1(ByVal val As Int16) As Single
        If (val >= 0) Then
            convert_87024_1 = Convert.ToSingle(val) * 10.0F / 32767.0F
        Else
            convert_87024_1 = Convert.ToSingle(val) * 10.0F / 32768.0F
        End If
    End Function


    ' convert float value (-10.0 ~ +10.0) to int16 value (-32768 ~ +32767)
    Private Function convert_87024(ByVal float_val As Single) As Int16
        If float_val = 10.0 Then  ' upper limit
            convert_87024 = 32767
        ElseIf float_val = -10.0 Then  ' lower limit
            convert_87024 = -32768
        ElseIf (float_val >= 0.0) And (float_val < 10.0) Then   ' 0.0 ~ 10.0
            convert_87024 = Convert.ToInt16(float_val * 3276.7)
        ElseIf (float_val < 0.0) And (float_val > -10.0) Then   ' -10.0 ~ 0.0
            convert_87024 = Convert.ToInt16(float_val * 3276.8)
        Else   ' wrong val
            MsgBox("Value should be between -10.0 to +10.0 !")
            Return 0
        End If
    End Function


    Private Sub ScanAI()

        Dim Value As Int16

        Value = 0

        ' get AI value (short integer) at Modbus No. 9 to 12
        Quicker.UserShare.UserGetReg_Short(Convert.ToUInt16(9), Value)
        AI_Value1.Text = float_str1(convert_8017(Value), 3)     ' display 3 digit under "." symbol

        Quicker.UserShare.UserGetReg_Short(Convert.ToUInt16(10), Value)
        AI_Value2.Text = float_str1(convert_8017(Value), 3)

        Quicker.UserShare.UserGetReg_Short(Convert.ToUInt16(11), Value)
        AI_Value3.Text = float_str1(convert_8017(Value), 3)

        Quicker.UserShare.UserGetReg_Short(Convert.ToUInt16(12), Value)
        AI_Value4.Text = float_str1(convert_8017(Value), 3)

        ' Get short integer (16-bit signed, -32768 ~ +32767) at Modbus No.= 1 (AO 1)
        Quicker.UserShare.UserGetReg_Short(Convert.ToUInt16(1), Value)
        AO_Value1.Text = float_str1(convert_87024_1(Value), 2)  ' display 2 digit under "." symbol

    End Sub


    ' convert float value to string with fixed digit number under dot "." symbol'
    ' float_val: a float value, for ex, 1.234567
    ' dot_num: 0 ~ 6
    ' for ex,  1.234567 return 1.234 if dot_num=3
    '                   return 1.23  if dot_num=2
    Private Function float_str1(ByVal float_val As Single, ByVal dot_num As Byte) As String

        Dim str, str1 As String
        Dim ii, jj, pos As Int16

        ' set float vale as 0 if vale is very close to 0 
        If (float_val > 0.0) And (float_val < 0.000001) Then
            float_val = 0.0
        End If
        If (float_val < 0.0) And (float_val > -0.000001) Then
            float_val = 0.0
        End If

        ' dot bumber should be 0 to 6
        If (dot_num < 0) Then
            dot_num = 0
        ElseIf (dot_num > 6) Then
            dot_num = 6
        End If

        ' search "."
        str = float_val.ToString
        jj = str.Length
        pos = -1
        For ii = 0 To jj - 1
            If str.Substring(ii, 1) = "." Then
                pos = ii
                Exit For
            End If
        Next ii

        If pos <> -1 Then   ' "." found
            If pos < (str.Length - dot_num) Then
                str1 = str.Substring(0, pos + 1 + dot_num)
            Else
                jj = pos + 1 + dot_num - str.Length
                str1 = str
                For ii = 0 To jj - 1
                    str1 += "0"
                Next ii
            End If
        Else    ' "." not found
            str1 = float_val.ToString + "."
            For ii = 0 To dot_num - 1
                str1 += "0"
            Next ii
        End If
        Return str1

    End Function


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' scan A/I every timer1 interval
        ScanAI()
    End Sub


    ' check if value is inside -10.0 to +10.0
    ' return 1:Ok , 0:Error
    Private Function check_87024(ByVal float_val As Single) As Byte
        If (float_val > 10.0) Or (float_val < -10.0) Then
            MsgBox("Value should be between -10.0 to +10.0 !")
            Return 0  ' error
        End If
        Return 1  ' ok
    End Function


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        is_try_ok = 1  ' init as Ok
        Try
            temp_single = Convert.ToSingle(AO_Value4.Text)  ' for ex, if Value = 12AB.cd, error happens. However for ex.  Value = 1.15 is correct
        Catch
            is_try_ok = 0   ' error found
            MsgBox("Wrong value !")
        End Try

        If (is_try_ok = 0) Then  ' error found , return 
            Return
        End If

        ' check if value is correct
        If (check_87024(temp_single) = 0) Then
            Return
        End If

        ' write a short integer (16-bit signed, -32768 ~ +32767) to Modbus No.=4
        Quicker.UserShare.UserSetReg_Short(Convert.ToUInt16(4), convert_87024(temp_single))

    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        is_try_ok = 1  ' init as Ok
        Try
            temp_single = Convert.ToSingle(AO_Value3.Text)  ' for ex, if Value = 12AB.cd, error happens. However for ex.  Value = 1.15 is correct
        Catch
            is_try_ok = 0   ' error found
            MsgBox("Wrong value !")
        End Try

        If (is_try_ok = 0) Then  ' error found , return 
            Return
        End If

        ' check if value is correct
        If (check_87024(temp_single) = 0) Then
            Return
        End If

        ' write a short integer (16-bit signed, -32768 ~ +32767) to Modbus No.=3
        Quicker.UserShare.UserSetReg_Short(Convert.ToUInt16(3), convert_87024(temp_single))

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        is_try_ok = 1  ' init as Ok
        Try
            temp_single = Convert.ToSingle(AO_Value2.Text)  ' for ex, if Value = 12AB.cd, error happens. However for ex.  Value = 1.15 is correct
        Catch
            is_try_ok = 0   ' error found
            MsgBox("Wrong value !")
        End Try

        If (is_try_ok = 0) Then  ' error found , return 
            Return
        End If

        ' check if value is correct
        If (check_87024(temp_single) = 0) Then
            Return
        End If

        ' write a short integer (16-bit signed, -32768 ~ +32767) to Modbus No.=2
        Quicker.UserShare.UserSetReg_Short(Convert.ToUInt16(2), convert_87024(temp_single))

    End Sub


End Class
