C# 读写PLC数据
1. 参考资料
备用连接:https://blog.youkuaiyun.com/qq_31463571/article/details/134024923?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522172405759816800225539713%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=172405759816800225539713&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduend~default-2-134024923-null-null.142v100pc_search_result_base6&utm_term=C%23%20plc&spm=1018.2226.3001.4187
2. 实现过程
1) 安装博途软件;
2) 安装PLCSIM advanced
3) 安装Visual Studio ;C# 控制台应用程序;
4) 博途软件中数据块准备;
5) 建立TIA portal 与 PLCSIM的仿真
6) 编写C# 程序,建立连接;读取数据;写入数据;
7) 验证测试;
3. C#编程内容
- 简单读写
using S7.Net;
namespace SimS71500V18
{
internal class Program
{
static void Main(string[] args)
{
Plc plc = new Plc(CpuType.S71500, "192.168.0.100", 0, 1); // (PLC类型,PLC IP 地址,机架号,槽号)
plc.Open();
//if (plc.IsConnected)
//{
// Console.WriteLine("连接成功");
//}
string inputKey = "";
bool boolFlag = false;
short iCount = 1;
//Task readPLCTask = Task.Factory.StartNew(() =>
//{
// while (plc.IsConnected && inputKey != "q")
// {
// }
// });
while (plc.IsConnected && inputKey != "q")
{
Console.Clear();
Console.WriteLine("连接成功");
// 布尔量
plc.Write("DB1.DBX0.0", boolFlag);
// 整型量
plc.Write("DB1.DBW4", (short)(iCount * 2));
// 数组中剩余元素
short[] arrValues = new short[9];
for(int i=0;i<arrValues.Length;i++)
{
arrValues[i] = (short)(iCount + 1 + i);
}
plc.Write("DB1.DBW6", arrValues);
// 下面将写入后的值读上来;
// 布尔量
Console.WriteLine(plc.Read("DB1.DBX0.0"));
// 整型量
Console.WriteLine(plc.Read("DB1.DBW2"));
// 数值字中第一个元素的值
Console.WriteLine(plc.Read("DB1.DBW4"));
// 数组中的剩余元素
short[] remainArr = (short[])plc.Read(DataType.DataBlock, 1, 6, VarType.Word, 9);
for (int i = 0; i < remainArr.Length; i++)
{
Console.WriteLine(remainArr[i] + "\t");
}
iCount++;
boolFlag = !boolFlag;
inputKey = Console.ReadLine();
}
plc.Close();
}
}
}
- 多线程读写
using S7.Net;
namespace SimS71500V18
{
internal class Program
{
static void Main(string[] args)
{
Plc plc = new Plc(CpuType.S71500, "192.168.0.100", 0, 1); // (PLC类型,PLC IP 地址,机架号,槽号)
plc.Open();
//if (plc.IsConnected)
//{
// Console.WriteLine("连接成功");
//}
string inputKey = "";
bool boolFlag = false;
short iCount = 1;
Task readPLCTask = Task.Factory.StartNew(() =>
{
while (plc.IsConnected && inputKey != "q")
{
Console.Clear();
// 下面将写入后的值读上来;
// 布尔量
Console.WriteLine(plc.Read("DB1.DBX0.0"));
// 整型量
Console.WriteLine(plc.Read("DB1.DBW2"));
// 数值字中第一个元素的值
Console.WriteLine(plc.Read("DB1.DBW4"));
// 数组中的剩余元素
short[] remainArr = (short[])plc.Read(DataType.DataBlock, 1, 6, VarType.Word, 9);
for (int i = 0; i < remainArr.Length; i++)
{
Console.WriteLine(remainArr[i] + "\t");
}
Task.Delay(200).Wait();
}
});
Task writePLCTask = Task.Factory.StartNew(() =>
{
while (plc.IsConnected && inputKey != "q")
{
// 布尔量
plc.Write("DB1.DBX0.0", boolFlag);
// 整型量
plc.Write("DB1.DBW2", iCount);
//数组中的第一个元素
plc.Write("DB1.DBW4", (short)(iCount * 2));
// 数组中剩余元素
short[] arrValues = new short[9];
for (int i = 0; i < arrValues.Length; i++)
{
arrValues[i] = (short)(iCount + 1 + i);
}
plc.Write("DB1.DBW6", arrValues);
iCount++;
boolFlag = !boolFlag;
Task.Delay(200).Wait();
}
});
inputKey = Console.ReadLine();
plc.Close();
Task.WaitAll(readPLCTask,writePLCTask);
}
}
}
4. 实现结果:
以上