C# S7通信类库代码
时间: 2023-11-03 14:06:54 浏览: 53
以下是一个简单的C# S7通信类库代码示例:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using S7.Net;
namespace S7Communication
{
public class S7Comm
{
private Plc _plc;
public S7Comm(string ipAddress, int rack, int slot)
{
_plc = new Plc(CpuType.S71200, ipAddress, rack, slot);
}
public bool Connect()
{
try
{
_plc.Open();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public bool Disconnect()
{
try
{
_plc.Close();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public bool ReadBool(string address)
{
try
{
bool value = _plc.ReadBool(address);
return value;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public bool WriteBool(string address, bool value)
{
try
{
_plc.Write(address, value);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
}
```