基本信息
源码名称:Modbus连接上位机,接收数据
源码大小:0.16M
文件格式:.rar
开发语言:C#
更新时间:2020-10-23
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 3 元 
   源码介绍
连接上位机,接受数据

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;

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

        ModbusRtu objModbus = new ModbusRtu();
        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                objModbus.OpenMyCom("COM9", 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One);
            }
            catch (Exception)
            {
                MessageBox.Show("Fail");
                return;
            }
            MessageBox.Show("Success");
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            byte[] res = objModbus.ReadKeepReg(17, 107, 3);
            if (res != null && res.Length == 6)
            {

                //返回了6个字节     前面2个字节表示107   中间两个字节表示108  最后两个字节表示109


                // 01 00         256   1

                //Modbus解析   107  ==  40108
                byte[] s = new byte[2];

                s[0] = res[1];
                s[1] = res[0];

                MessageBox.Show(BitConverter.ToInt16(s, 0).ToString());
            }
            else
            {
                MessageBox.Show("Fail");
            }
        }


        /*
         * 
         Tx:034-11 03 00 6B 00 03 76 87

         Rx:035-11 03 06 00 00 00 00 00 00 EC B5

        从站地址:11

        功能码:03

        数据区:06 00 00 00 00 00 00

        CRC区:EC B5     
        
        读取三个寄存器  返回11个字节

        如果读取四个寄存器,应该返回多少个字节? 13

        如果读取五个寄存器,应该返回多少个字节? 15    N*2 5

         */
    }
}