先添加引用HslCommunication.dll
链接:https://pan.baidu.com/s/1k8yP4N6cJuvWY4eJUqgMTQ?pwd=2468
提取码:2468
using HslCommunication;
using HslCommunication.Profinet.Melsec;
namespace DengYongWenCCD
{
public static class Mitsubishi
{
public static MelsecMcNet Melsu_Net;
//创建连接
public static bool Mitsu(string ipAddress, int port)
{
Melsu_Net = new MelsecMcNet(ipAddress, port);
Melsu_Net.ConnectTimeOut = 2000; // 网络连接的超时时间
Melsu_Net.NetworkNumber = 0x00; // 网络号
Melsu_Net.NetworkStationNumber = 0x00; // 网络站号
OperateResult connect = Melsu_Net.ConnectServer();
return connect.IsSuccess;
}
//读
public static bool Read(ref float flo, String address) {
OperateResult<float> tt = Mitsubishi.Melsu_Net.ReadFloat(address);
if (tt.IsSuccess)
{
flo = tt.Content;
return true;
}
else
{
return false;
}
}
//写
public static bool Write(String address,float Value)
{
OperateResult write = Mitsubishi.Melsu_Net.Write(address, Value);
if (write.IsSuccess)
{
return true;
}
else
{
return false;
}
}
}
}
//调用连接方法
//传入iP,端口号
bool t=Mitsubishi.Mitsu("192.168.27.237", 8005);
if (t)
{
MessageBox.Show("连接成功");
}
else {
MessageBox.Show("连接失败");
}
//调用读方法
float flo = 0;
bool Re= Mitsubishi.Read(ref flo,"D100");
if (Re)
{
MessageBox.Show("读取成功" + flo);
}
else
{
MessageBox.Show("读取失败");
}
//调用写方法
bool Wr = Mitsubishi.Write("M100", 52);
if (Wr)
{
MessageBox.Show("写入成功");
}
else {
MessageBox.Show("写入失败");
}