欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 开发 ModBus通讯协议 工业4.0

程序员文章站 2022-03-22 14:40:51
...

公司在做一个智能工厂项目,需要将不同厂家的机器设备通过ModBus协议连接起来,于是在网上找了个支持ModBus协议的DLL库HslCommunication。将DLL引入的C#项目中
废话不多说,上代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HslCommunication;
using HslCommunication.ModBus;
using MyModbus.Model;

namespace MyModbus
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }
        public static SystemTableInfo washIpInfo = null;
        public static string WashIP = string.Empty;
        public static int? WashPort = null;
        public static int? WashStation = null;
        public ModbusTcpNet GetModbusTcpNetInstance()
        {

            //获取洗水设备IP信息
            if (washIpInfo == null || string.IsNullOrEmpty(WashIP))
            {
                //washIpInfo = SystemTableDAL.GetSystemTable("WashIpInfo", "WashIpInfo").FirstOrDefault();
                WashIP = washIpInfo != null ? washIpInfo.Sys_Value1 : "127.0.0.1";
                //WashIP = "127.0.0.1";
                WashPort = washIpInfo != null ? Convert.ToInt32(washIpInfo.Sys_Value2) : 502;
                //WashPort = 502;
                WashStation = washIpInfo != null ? Convert.ToInt32(washIpInfo.Sys_Value3) : 0;
            }


            ModbusTcpNet instance = new ModbusTcpNet(WashIP, (int)WashPort, (byte)WashStation);
            OperateResult isOK = instance.ConnectServer();

            if (!isOK.IsSuccess)//判断是否连接成功
            {
                //MessageBox.Show(Language.GetChiEng("ConnectFiald", "Connect wash fiald!\\连接滚筒设备失败!"));

            }
            return instance;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ModbusTcpNet busTcpClient = null;
            try
            {
                busTcpClient = GetModbusTcpNetInstance();
                OperateResult isOK = busTcpClient.ConnectServer();
                if (isOK.IsSuccess)//判断是否连接成功
                {
                    MessageBox.Show("连接成功!");
                    short i_result = busTcpClient.ReadInt16("100").Content;
                    MessageBox.Show("地址100的值:" + Convert.ToString(i_result));
                    OperateResult write = busTcpClient.Write("2", true);//打开挂衣完成滚筒
                    if (write.IsSuccess)//判读是是否修改线圈成功
                    {
                        MessageBox.Show("写入成功!");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (busTcpClient != null)
                {
                    busTcpClient.Dispose();
                    busTcpClient = null;
                }
            }
        }
    }
}

启动ModBus虚拟服务器,设置地址100的值为10,现在地址:Modbus协议服务端模拟器 ModbusTcpServer1.zip
C# 开发 ModBus通讯协议 工业4.0
运行例子,读取成功:
C# 开发 ModBus通讯协议 工业4.0
写入成功:
C# 开发 ModBus通讯协议 工业4.0
查看写入结果:
C# 开发 ModBus通讯协议 工业4.0
请支持一下,我会继续更新工业4.0的技术工具
C# 开发 ModBus通讯协议 工业4.0