using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Quicker;

namespace wp_CSharp03
{
    public partial class Form1 : Form
    {
        byte is_try_ok;     // try ... catch state, 1: Ok, 0: error
        int temp_int32;   // temporary used int32 value
        float temp_single;  // temporary used 32-bit float value

        public Form1()
        {
            InitializeComponent();
        }

        private void Button_long_Click(object sender, EventArgs e)
        {
            is_try_ok = 1;  //init as Ok
            try
            {
                temp_int32 = Int32.Parse(new_long_val.Text);
            }
            catch
            {
                is_try_ok = 0;   // error found
                MessageBox.Show("Wrong value !");
            }

            if (is_try_ok == 0)  // error found , return 
                return;


            // write long integer, Modbus No.=1
            Quicker.UserShare.UserSetReg_Long(1,out temp_int32);
        }

        private void Button_float_Click(object sender, EventArgs e)
        {
            is_try_ok = 1;  // init as Ok
            try
            {
                temp_single = float.Parse(new_float_val.Text);
            }
            catch
            {
                is_try_ok = 0;   // error found
                MessageBox.Show("Wrong value !");
            }

            if (is_try_ok == 0)   // error found , return 
                return;
        

            // write float value (32-bit float), Modbus No.=3
            Quicker.UserShare.UserSetReg_Float(3,out temp_single);

        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            // enable timer1
            timer1.Interval = 250;   // 250 ms
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Int32 c1;   // long integer
            float f1;   // float val (32-bit)
            Int32 t1;   // long integer for timer value in ISaGRAF

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

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

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

       
    }
}