using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using UserShareNet; namespace demo_CSharp02 { public partial class Form1 : Form { byte is_try_ok; // try ... catch state, 1: Ok, 0: error float temp_single; // temporary used 32-bit float value public Form1() { InitializeComponent(); } // convert 8017H's value (-32768 ~ +32767) to float value (-10.0 ~ +10.0) private float convert_8017(short val) { if (val >= 0) return Convert.ToSingle(val) * 10.0f / 32767.0f; else return Convert.ToSingle(val) * 10.0f / 32768.0f; } private void ScanAI() { Single Value ; // get AI value (short integer) at Address No. 9 to 12 UserShare.Get_REAL(Convert.ToUInt16(9), out Value); AI_Value1.Text = Value.ToString("0.000"); UserShare.Get_REAL(Convert.ToUInt16(10), out Value); AI_Value2.Text = Value.ToString("0.000"); UserShare.Get_REAL(Convert.ToUInt16(11), out Value); AI_Value3.Text = Value.ToString("0.000"); UserShare.Get_REAL(Convert.ToUInt16(12), out Value); AI_Value4.Text = Value.ToString("0.000"); // Get Real number at Address No.= 1 (AO 1) UserShare.Get_REAL(Convert.ToUInt16(1), out Value); AO_Value1.Text = Value.ToString("0.000"); } // check if value is inside -10.0 to +10.0 // return 1:Ok , 0:Error private byte check_87024(float float_val) { if (float_val > 10.0 || float_val < -10.0) { MessageBox.Show("Value should be between -10.0 to +10.0 !"); return 0; // error } return 1; // ok } private void Button4_Click(object sender, EventArgs e) { is_try_ok = 1; // init as Ok try { temp_single = float.Parse(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 MessageBox.Show("Wrong value !"); } if (is_try_ok == 0) // error found , return return; // check if value is correct if (check_87024(temp_single) == 0) return; // temp = convert_87024(temp_single); // write a real number to Address No.=4 UserShare.Set_REAL(Convert.ToUInt16(4),temp_single); } private void Button3_Click(object sender, EventArgs e) { is_try_ok = 1; // init as Ok try { temp_single = short.Parse(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 MessageBox.Show("Wrong value !"); } if (is_try_ok == 0) // error found , return return; // check if value is correct if (check_87024(temp_single) == 0) return; // write a short integer (16-bit signed, -32768 ~ +32767) to Modbus No.=3 UserShare.Set_REAL(Convert.ToUInt16(3), temp_single); } private void Button2_Click(object sender, EventArgs e) { is_try_ok = 1; // init as Ok try { temp_single = float.Parse(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 MessageBox.Show("Wrong value !"); } if (is_try_ok == 0) // error found , return return; // check if value is correct if (check_87024(temp_single) == 0) return; // write a real number to Address No.=2 UserShare.Set_REAL(Convert.ToUInt16(2), 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) { // scan A/I every timer1 interval ScanAI(); } } }