这个例子使用HslCommunication类库的三菱plc读写测试程序,所以在使用前,请安装这个组件。
HslCommunication类库的作者功底扎实,其代码质量很高,是勇哥学习的标榜。
同时作者有愿望和勇气为工业4.0做技术上的积累,这个高度让人汗颜,这里向作者表达真心的致敬。
示例代码:usingHslCommunication;
usingHslCommunication.Profinet.Melsec;
usingSystem;
usingSystem.Collections.Generic;
ponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespacefx5PlcTest
{
publicpartialclassForm1:Form
{
privateMelsecMcNetmelsec_net=null;
publicForm1()
{
InitializeComponent();
melsec_net=newMelsecMcNet();
}
privatevoidbtnLinkplc_Click(objectsender,EventArgse)
{
//连接
.IPAddressaddress;
if(!.IPAddress.TryParse(tbplcIp.Text,outaddress))
{
MessageBox.Show("Ip地址输入不正确!");
return;
}
melsec_net.IpAddress=tbplcIp.Text;
intport;
if(!int.TryParse(tbplcPort.Text,outport))
{
MessageBox.Show("端口输入格式不正确!");
return;
}
melsec_net.Port=port;
melsec_net.ConnectClose();
try
{
OperateResultconnect=melsec_net.ConnectServer();
if(connect.IsSuccess)
{
MessageBox.Show("连接成功!");
btnTest.Enabled=true;
}
else
{
MessageBox.Show("连接失败!");
btnTest.Enabled=false;
}
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
privatevoidbtnTest_Click(objectsender,EventArgse)
{
varr1=melsec_net.Write("D1000",112.55f);
Thread.Sleep(100);
varr2=melsec_net.Write("D1004",55);
Thread.Sleep(100);
varr3=melsec_net.Write("D1010","allegro");
Thread.Sleep(100);
varres=melsec_net.ReadFloat("D1000");
varres1=melsec_net.ReadInt16("D1004");
varres2=melsec_net.ReadString("D1010",7);
rtbMsg.AppendText(res.Content.ToString()+Environment.NewLine+
res1.Content.ToString()+Environment.NewLine+
res2.Content);
}
privatevoidForm1_FormClosed(objectsender,FormClosedEventArgse)
{
melsec_net.ConnectClose();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
btnTest.Enabled=false;
}
}
}
---------------------
作者:hackpig
来源:
版权声明:本文为博主原创文章,转载请附上博文链接!
#转载请注明出处 《少有人走的路》勇哥的工业自动化技术网站。如果需要本贴图片源码等资源,请向勇哥索取。
如果觉得《三菱5u 上位机mc协议_上位机读写三菱plc fx5u的内存数据示例》对你有帮助,请点赞、收藏,并留下你的观点哦!