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


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim c1 As Int32   ' long integer
        Dim f1 As Single  ' float val (32-bit)
        Dim t1 As Int32   ' long integer for timer value in ISaGRAF

        ' Get long integer (32-bit signed integer), Modbus No.=1
        Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(1), c1)
        long_val.Text = Convert.ToString(c1)

        ' Get float val (32-bit float), Modbus No.=3
        Quicker.UserShare.UserGetReg_Float(Convert.ToUInt16(3), f1)
        float_val.Text = Convert.ToString(f1)

        ' Get Timer in long integer format (32-bit signed integer), unit is ms, Modbus No.=5
        Quicker.UserShare.UserGetReg_Long(Convert.ToUInt16(5), t1)
        timer_val.Text = Convert.ToString(t1)

    End Sub


    Private Sub Button_long_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_long.Click

        is_try_ok = 1  ' init as Ok
        Try
            temp_int32 = Convert.ToInt32(new_long_val.Text)
        Catch
            is_try_ok = 0   ' error found
            MsgBox("Wrong value !")
        End Try

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

        ' write long integer, Modbus No.=1
        Quicker.UserShare.UserSetReg_Long(Convert.ToUInt16(1), temp_int32)

    End Sub


    Private Sub Button_float_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_float.Click

        is_try_ok = 1  ' init as Ok
        Try
            temp_single = Convert.ToSingle(new_float_val.Text)
        Catch
            is_try_ok = 0   ' error found
            MsgBox("Wrong value !")
        End Try

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

        ' write float value (32-bit float), Modbus No.=3
        Quicker.UserShare.UserSetReg_Float(Convert.ToUInt16(3), temp_single)


    End Sub

End Class
