PLC instance First you have to create an instance of PLC object. This one wiil be like a factory that produces linked variables.
PLC plc = new PLC(“5.2.100.109.1.1”); When device connected or disconnected plc.DeviceReady += Plc_DeviceReady; plc.DeviceLost += Plc_DeviceLost;
[…]
private void Plc_DeviceReady(object sender, AdsDevice e) { Log(“READY [” + e.Address.Port.ToString() + “]”); } How to create and link variables Create a copy of your PLC’s variable then use it like an ordinary variable We use PLC object that produces linked variables. After that variables will autoupdating their state and value.
//定义变量就这么简单,很容易做其他抽象。直接复杂的结构体类型
Var main_count = plc.Var (“MAIN.count”); Var main_state = plc.Var(“MAIN.state”); Var g_Version = plc.Var(".VERSION");
Var frm0 = plc.Var(“Inputs.Frm0InputToggle”, 27907); Var devState = plc.Var(0xF030, 0x5FE, 27907);
long framesTotal += frm0 / 2; // automatic type casting MessageBox.Show(frm0); // cast into the string type without call of the ToString() From now you can subscribe on value changing.
main_count.ValueChanged += delegate { counterStatusLabel.Text = main_count; }; or
main_count.ValueChanged += delegate (object src, Var v) { ushort val = (ushort)v.GetValue(); framesTotal += val / 2; counterStatusLabel.Text = val.ToString(); }; Write-back to the PLC Use “RemoteValue” propertie to write a new value to the PLC runtime.
main_count.RemoteValue = 123; WinForms data binding For example we will bind Text propertie of the Label control with default name label1. At the PLC side we have MAIN.count variable that contains value of counter that we should show.
Var main_count = plc.Var(“MAIN.count”);
Binding b = new Binding(“Text”, main_count, “RemoteValue”); b.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged; b.DataSourceUpdateMode = DataSourceUpdateMode.Never;
label1.DataBindings.Add(b); If we have to convert given value we define a format converter
Var main_count = plc.Var(“MAIN.count”);
Binding b2 = new Binding(“ForeColor”, main_count, “RemoteValue”); b2.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged; b2.DataSourceUpdateMode = DataSourceUpdateMode.Never; b2.Format += (s, ea) => { ea.Value = (short)ea.Value < 0 ? Color.Blue : Color.Red; }; label1.DataBindings.Add(b2); WPF data bindings In WPF you must use properties instead of variables.