PLC設備通訊創建連接
using (var plc = new Plc(CpuType.S71200, "127.0.0.1", 0, 1))
{
try
{
plc.Open();
}
catch (Exception)
{
Console.WriteLine($"連接到PLC設備失。篒sConnect={plc.IsConnected},IsAvailable={plc.IsAvailable}");
return;
}
}
#region 單個數據項讀取
var db1Bool1 = plc.Read("DB1.DBX0.0");
Console.WriteLine("DB1.DBX0.0: " + db1Bool1);
bool db1Bool2 = (bool)plc.Read("DB1.DBX0.1");
Console.WriteLine("DB1.DBX0.1: " + db1Bool2);
// Int
int intVariable = (ushort)plc.Read("DB1.DBW2.0");
Console.WriteLine("DB1.DBW2.0: " + intVariable);
// Real
var realVariabl = ((uint)plc.Read("DB1.DBD4.0")).ConvertToFloat();
Console.WriteLine("DB1.DBD4.0: " + realVariabl);
// DInt
var dIntVariable = (uint)plc.Read("DB1.DBD8.0");
Console.WriteLine("DB1.DBD8.0: " + dIntVariable);
// DWord
var dWordVariable = (uint)plc.Read("DB1.DBD12.0");
Console.WriteLine("DB1.DBD12.0: " + dWordVariable);
// Word
var wordVariable = (ushort)plc.Read("DB1.DBW16.0");
Console.WriteLine("DB1.DBW16.0: " + wordVariable);
#endregion
// 讀取int類型
byte [] int0 = (byte[])plc.ReadBytes(DataType.DataBlock, 1, 2, 2);
byte[] revint0 = int0.Reverse().ToArray();
short f5 = BitConverter.ToInt16(revint0, 0);
Console.WriteLine("DB1.DBD2: " + f5); |
|