首先添加引用 三菱PLCdll文件
1、导入 命名空间:
using HslCommunication.Profinet.Melsec;
2、声明一个PLC对象:
private MelsecMcNet melsecMcNet = null;
3、读取PLC地址值
///
/// 读取bool值
///
/// PLC地址:如M100,X100,Y100,B100
///
public bool ReadBool(string str_Add)
{
OperateResult rt = this.melsecMcNet.ReadBool(str_Add);
if (rt.IsSuccess)
return rt.Content;
else
return false;
}
///
/// 读取short值
///
/// PLC地址:如D100,W100,R100
///
public int ReadInt16(string str_Add)
{
OperateResult rt = melsecMcNet.ReadInt16(str_Add);
if (rt.IsSuccess)
{
return rt.Content;
}
else
{
return 0;
}
}
///
/// 读取ushort值
///
/// PLC地址:如D100,W100,R100
///
public int ReadUInt16(string str_Add)
{
OperateResult rt = melsecMcNet.ReadUInt16(str_Add);
if (rt.IsSuccess)
{
return rt.Content;
}
else
{
return 0;
}
}
///
/// 读取int值
///
/// PLC地址:如D100,W100,R100
///
public int ReadInt32(string str_Add)
{
OperateResult rt = melsecMcNet.ReadInt32(str_Add);
if (rt.IsSuccess)
return rt.Content;
else
return 0;
}
///
/// 读取uint值
///
/// PLC地址:如D100,W100,R100
///
public int ReadUInt32(string str_Add)
{
OperateResult rt = melsecMcNet.ReadUInt32(str_Add);
if (rt.IsSuccess)
return (int)rt.Content;
else
return 0;
}
///
/// 读取float值
///
/// PLC地址:如D100,W100,R100
///
public float ReadFloat(string str_Add)
{
OperateResult rt = melsecMcNet.ReadFloat(str_Add);
if (rt.IsSuccess)
return rt.Content;
else
return 0;
}
///
/// 读取double值
///
/// PLC地址:如D100,W100,R100
///
public double ReadDouble(string str_Add)
{
OperateResult rt = melsecMcNet.ReadDouble(str_Add);
if (rt.IsSuccess)
return rt.Content;
else
return 0;
}
/// <summary>/// 读取多个连续的bool值/// </summary>/// <param name="str_Add">PLC地址:如M100,X100,Y100,B100</param>/// <param name="length">地址个数</param>/// <returns></returns>public bool[] ReadBool(string str_Add, ushort length){OperateResult<bool[]> rt = this.melsecMcNet.ReadBool(str_Add, length);if (rt.IsSuccess) return rt.Content; else return null; }/// <summary>/// 读取short值/// </summary>/// <param name="str_Add">PLC地址:如D100,W100,R100</param>/// <param name="length">地址个数</param> /// <returns></returns>public short[] ReadInt16(string str_Add, ushort length){OperateResult<short[]> rt = melsecMcNet.ReadInt16(str_Add, length);if (rt.IsSuccess) return rt.Content; else return null; }/// <summary>/// 读取ushort值/// </summary>/// <param name="str_Add">PLC地址:如D100,W100,R100</param>/// <param name="length">地址个数</param>/// <returns></returns>public ushort[] ReadUInt16(string str_Add, ushort length){OperateResult<ushort[]> rt = melsecMcNet.ReadUInt16(str_Add, length);if (rt.IsSuccess) return rt.Content; else return null; }/// <summary>/// 读取int值/// </summary>/// <param name="str_Add">PLC地址:如D100,W100,R100</param>/// <param name="length">地址个数</param>/// <returns></returns>public int[] ReadInt32(string str_Add, ushort length){OperateResult<int[]> rt = melsecMcNet.ReadInt32(str_Add, length);if (rt.IsSuccess) return rt.Content; else return null; }/// <summary>/// 读取uint值/// </summary>/// <param name="str_Add">PLC地址:如D100,W100,R100</param>/// <param name="length">地址个数</param>/// <returns></returns>public uint[] ReadUInt32(string str_Add, ushort length){OperateResult<uint[]> rt = melsecMcNet.ReadUInt32(str_Add, length);if (rt.IsSuccess) return rt.Content; else return null; }/// <summary>/// 读取float值/// </summary>/// <param name="str_Add">PLC地址:如D100,W100,R100</param>/// <param name="length">地址个数</param>/// <returns></returns>public float[] ReadFloat(string str_Add, ushort length){OperateResult<float[]> rt = melsecMcNet.ReadFloat(str_Add, length);if (rt.IsSuccess) return rt.Content; else return null; }/// <summary>/// 读取double值/// </summary>/// <param name="str_Add">PLC地址:如D100,W100,R100</param>/// <param name="length">地址个数</param>/// <returns></returns>public double[] ReadDouble(string str_Add, ushort length){OperateResult<double[]> rt = melsecMcNet.ReadDouble(str_Add, length);if (rt.IsSuccess) return rt.Content; else return null; }/// <summary>/// 读取string值/// </summary>/// <param name="str_Add">PLC地址:如D100,W100,R100</param>/// <param name="length">地址个数</param>/// <returns></returns>public string ReadString(string str_Add, ushort length){OperateResult<string> rt = melsecMcNet.ReadString(str_Add, length);if (rt.IsSuccess) return rt.Content; else return string.Empty; }
4、写入PLC地址值
///
/// 写入单个bool数据
///
///
///
///
public bool WriteBool(string str_Add,bool data)
{
OperateResult rt= melsecMcNet.Write(str_Add, data);
if(!rt.IsSuccess)
return false;
return true;
}
///
/// 写入单个ushort数据
///
///
///
///
public bool WriteUInt16(string str_Add, ushort data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 写入单个short数据
///
///
///
///
public bool WriteInt16(string str_Add, short data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 写入单个uint数据
///
///
///
///
public bool WriteUInt32(string str_Add, uint data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 写入单个int数据
///
///
///
///
public bool WriteInt32(string str_Add, int data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 写入单个float数据
///
///
///
///
public bool WriteFloat(string str_Add, float data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 写入单个double数据
///
///
///
///
public bool WriteDouble(string str_Add, double data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 写入字符串
///
///
///
///
public bool WriteString(string str_Add, string data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 连续写入多个bool数据
///
///
///
///
public bool WriteBool(string str_Add, bool[] data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 连续写入多个ushort数据
///
///
///
///
public bool WriteUInt16(string str_Add, ushort[] data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 连续写入多个short数据
///
///
///
///
public bool WriteInt16(string str_Add, short[] data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 连续写入多个uint数据
///
///
///
///
public bool WriteUInt32(string str_Add, uint[] data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 连续写入多个int数据
///
///
///
///
public bool WriteInt32(string str_Add, int[] data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 连续写入多个float数据
///
///
///
///
public bool WriteFloat(string str_Add, float[] data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}
///
/// 连续写入多个double数据
///
///
///
///
public bool WriteDouble(string str_Add, double[] data)
{
OperateResult rt = melsecMcNet.Write(str_Add, data);
if (!rt.IsSuccess)
return false;
return true;
}