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

公司在做一个智能工厂项目,需要将不同厂家的机器设备通过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_第1张图片
运行例子,读取成功:
C# 开发 ModBus通讯协议 工业4.0_第2张图片
写入成功:
C# 开发 ModBus通讯协议 工业4.0_第3张图片
查看写入结果:
C# 开发 ModBus通讯协议 工业4.0_第4张图片
请支持一下,我会继续更新工业4.0的技术工具
C# 开发 ModBus通讯协议 工业4.0_第5张图片

你可能感兴趣的:(工业物联网,物联网,c#,网络)