C# Modbus TCP上位机测试

前面说了三菱和西门子PLC的上位机通信,实际在生产应用中,设备会有很多不同的厂家生产的PLC,那么,我们就需要一种通用的语言,进行设备之间的通信,工业上较为广泛使用的语言之一就是Modbus。

Modbus有多种连接方式,如串口(RTU)、以太网(TCP/IP),今天我们讲的是TCP,也就是插网线的方式。

首先,我们安装从机的仿真,上位机软件作为主机。从机仿真可以用Modbus Slave这个软件。

这样从机就设置好了,接下来用C#编写主机(上位机)代码:

cs 复制代码
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 thinger.DataConvertLib;

namespace Modbus
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterScreen;
            this.MaximizeBox = false;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        }

        private void writeLog(string log)
        {
            string text = txtLog.Text;

            if (text.Length > 10000)
            {
                text = text.Substring(0, 10000) + "\n...";
            }

            txtLog.Text = "【" + DateTime.Now.ToString() + "】" + log + "\n" + text;
        }

        ModbusTcp tcp = new ModbusTcp();
        
        private void button1_Click(object sender, EventArgs e)
        {
            tcp.Connect("192.168.0.108", "502");

            if (tcp != null)
            {
                button1.BackColor = Color.LawnGreen;
            }
            else
            {
                button1.BackColor = Color.Red;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtLog.Text = "";

            int address = 0;//起始地址
            int num = 10;//寄存器数量

            byte[] res = tcp.ReadKeepReg(address, num);

            for (int i = 0; i < num; i++)
            {
                writeLog("【" + i.ToString() + "】" + (res[i * 2] * 256 + res[i * 2 + 1]).ToString());//byte数据类型只能包含0~255的数,超出=res[0]*256+res[1]
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bool success = tcp.PreSetSingleReg(int.Parse(textBox1.Text.Trim()), short.Parse(textBox2.Text.Trim()));

            if (success)
            {
                button2_Click(sender, e);
            }
        }
    }
}

最后附上全部源码:

https://download.csdn.net/download/mojocube/88103605

相关推荐
犀思云14 小时前
构建全球化多云网格:FusionWAN NaaS 在高可用基础设施中的工程实践
运维·网络·人工智能·系统架构·机器人
Black蜡笔小新15 小时前
国密GB35114平台EasyGBS筑牢安防安全防线,GB28181/GB35114无缝接入
网络·安全·音视频·gb35114
多多*15 小时前
2月3日面试题整理 字节跳动后端开发相关
android·java·开发语言·网络·jvm·adb·c#
vortex516 小时前
Alpine Linux syslinux 启动加固(密码保护)
linux·服务器·网络
犀思云16 小时前
网络运维减负:解构FusionWAN NaaS 面向企业广域网的技术逻辑演进
网络·智能仓储·fusionwan·专线·naas
倔强的石头10616 小时前
边缘侧时序数据的选型指南:网络不稳定、数据不丢、回传可控——用 Apache IoTDB 设计可靠链路
网络·apache·iotdb
db_murphy17 小时前
知识篇 | net.ipv4.ip_forward 参数
网络·网络协议·tcp/ip
Vect__17 小时前
TCP Socket编程详解
网络协议·tcp/ip·php
REDcker17 小时前
TCP 拥塞控制算法详解:CUBIC、BBR 及传统算法
tcp/ip·算法·php
科技块儿17 小时前
在线考试防作弊IP工具选型:5款主流IP查询API精度、成本、场景适配全测评
服务器·网络·tcp/ip·安全