private void button1_Click(object sender, EventArgs e) { if (MainFormsPlc != null) { var value = MainFormsPlc.Read("DB9.DBD2"); } }
public Form1() { InitializeComponent(); // new plc instance MyPlc = new S7.Net.Plc(S7.Net.CpuType.S71500, "192.168.0.1", 0, 0); // connect to PLC MyPlc.Open(); // read a value var plcValue = MyPlc.Read("DB8.DBD2"); }
/// <summary> /// Lettura di una singola variabile del set Data /// </summary> /// <param name="index">index della variabile da leggere</param> public void ReadSingleVariable(int index) { try { lock (Blocker) { switch (Data[index].VariableType) { case S7.Net.VarType.Bit: break; case S7.Net.VarType.Byte: break; case S7.Net.VarType.Word: break; case S7.Net.VarType.DWord: if (Data[index].DotNetDataType == typeof(Int32)) { Data[index].RawContent = S7.Net.Conversion.ConvertToInt((uint)Plc.Read(Data[index].DataType, Data[index].DBNumber, Data[index].DBOffset, Data[index].VariableType, 1)); } if (Data[index].DotNetDataType == typeof(UInt32)) { Data[index].RawContent = S7.Net.Conversion.ConvertToInt((uint)Plc.Read(Data[index].DataType, Data[index].DBNumber, Data[index].DBOffset, Data[index].VariableType, 1)); } break; case S7.Net.VarType.Int: if (Data[index].DotNetDataType == typeof(Int16)) { Data[index].RawContent = Convert.ToInt16(Plc.Read(Data[index].DataType, Data[index].DBNumber, Data[index].DBOffset, Data[index].VariableType, 1)); } if (Data[index].DotNetDataType == typeof(UInt16)) { Data[index].RawContent = S7.Net.Conversion.ConvertToUshort((short)Plc.Read(Data[index].DataType, Data[index].DBNumber, Data[index].DBOffset, Data[index].VariableType, 1)); } break; case S7.Net.VarType.DInt: break; case S7.Net.VarType.Real: if (Data[index].DotNetDataType == typeof(Single)) { Data[index].RawContent = (double)Plc.Read(Data[index].DataType, Data[index].DBNumber, Data[index].DBOffset, Data[index].VariableType, 1); } if (Data[index].DotNetDataType == typeof(double)) { throw new Exception("La lettura del LREAL non è ancora stata implementata correttamente"); //Data[index].RawContent = Convert.ToDecimal(Plc.Read(Data[index].DataType, Data[index].DBNumber, Data[index].DBOffset, Data[index].VariableType, 1)); } break; case S7.Net.VarType.String: Data[index].RawContent = Plc.Read(Data[index].DataType, Data[index].DBNumber, Data[index].DBOffset, Data[index].VariableType, Data[index].MaxStringLenght + 2); BuildWorkString_FromRaw(Data[index]); break; case S7.Net.VarType.Timer: break; case S7.Net.VarType.Counter: break; default: break; } } } catch (Exception ex) { Console.WriteLine("SiemensPLC.ReadSingleVariable = " + ex.Message); } }