static S7Client client = new S7Client(); static void Main(string[] args) { var res = client.ConnectTo("192.168.1.1", 0, 1);//連接PLC的IP地址 if (res == 0) { while (true) { byte[] db3Buffer = new byte[4]; var result = client.DBRead(127, 178, db3Buffer.Length, db3Buffer);//讀取DB127.DBD178位置數據到db3Buffer當中 if (result == 0)//判斷操作成功標識,成功0,否則不成功 { double db3dbd4 = S7.GetRealAt(db3Buffer, 0);//將獲取的數據進行轉換成Real類型,需要確定PLC當中此數據為Real類型方可 Console.WriteLine("DB127.DBD178: " + db3dbd4); } db3Buffer = new byte[4]; db3Buffer.SetRealAt(0, 345f);//將 345 數據存入buffer當中后續寫入到PLC //此方法為擴展方法,也可以寫成 //S7.SetRealAt(db3Buffer, 0, 345); result = client.DBWrite(127, 54, db3Buffer.Length, db3Buffer);//將buffer數據寫入PLC位置DB127.DBD54,類型為Real if (result != 0) { Console.WriteLine("Error: " + client.ErrorText(result)); } Thread.Sleep(1000); } } Console.ReadKey(); }