利用三菱的MX Component与三菱PLC进行以太网通信,我们可以用官方的dll编写C#代码,特别简单,最后附上整个源码下载。
1. 安装MX Component(必须)和GX WORKS3(主要是仿真用,实际可以不装)。
2. 手动连接PLC(这里用仿真):
3. 编写软件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ActUtlTypeLib;
namespace PLCTester
{
public class PLC
{
public ActUtlType actUtlType;
/// <summary>
/// 读PLC数据
/// </summary>
/// <param name="deviceName"></param>
/// <returns></returns>
public string Read(string deviceName)
{
try
{
int Result = 0;
int iReturnCode = actUtlType.GetDevice(deviceName, out Result);
if (iReturnCode == 0)
{
return Result.ToString();
}
else
{
return "";
}
}
catch
{
return "";
}
}
/// <summary>
/// 写PLC数据
/// </summary>
/// <param name="deviceName"></param>
/// <param name="data"></param>
/// <returns></returns>
public bool Write(string deviceName, int data)
{
try
{
int iReturnCode = actUtlType.SetDevice(deviceName, data);
if (iReturnCode == 0)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
}
}
下载:https://download.csdn.net/download/mojocube/88051677