dathlin大佬的HslCommunication.dll控件具有强大的功能有效减少通讯编程时间,现就利用该控件进行欧姆龙Fins TCP通讯,读取与写入操作。
1.先布局相关控件:
文章来源地址https://www.toymoban.com/news/detail-616115.html
2.引入HslCommunication.dll
解决方案管理器→右击引用→引用管理器→找到并选中HslCommunication.dll
注:HslCommunication.dll可以在网站上下载 胡工科技
3.引入命名空间:
using HslCommunication;
using HslCommunication.Profinet.Omron;
4.通讯代码:
private OmronFinsNet omronFinsNet = new OmronFinsNet("192.168.250.1", 9600);//设置连接对象(plcip,端口号)
const string PLCdrr = "H7.00";//定义要修改的寄存器地址
private void But_PLCComState_Click(object sender, EventArgs e)
{
try
{
Byte sa1 = 192;
Byte sa2 = 0;
omronFinsNet.SA1 = sa1;
omronFinsNet.SA2 = sa2;
OperateResult connect = omronFinsNet.ConnectServer();
if (connect.IsSuccess)
{
MessageBox.Show("连接成功!");
But_PLCComState.BackColor =Color.Green;
}
else
{
MessageBox.Show("连接失败!");
But_PLCComState.BackColor = Color.Red;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
5.读、写代码:
private void But_MESStateRead_Click(object sender, EventArgs e)
{
if (omronFinsNet.ConnectServer().IsSuccess)
{
try
{
if (omronFinsNet.ReadBool(PLCdrr).IsSuccess)//读取BOOL型寄存器
{
MessageBox.Show("读取成功!");
Txt_MESState.Text = omronFinsNet.ReadBool(PLCdrr).Content.ToString();
}
else
{
MessageBox.Show("读取失败!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void But_MESControlOpen_Click(object sender, EventArgs e)
{
if (omronFinsNet.ConnectServer().IsSuccess)
{
try
{
if (omronFinsNet.Write(PLCdrr, true).IsSuccess)
{
MessageBox.Show("写入成功!");
}
else
{
MessageBox.Show("写入失败!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
文章来源:https://www.toymoban.com/news/detail-616115.html
到了这里,关于基于HslCommunication的欧姆龙Fins TCP通讯的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!