• 大小: 181KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-09
  • 语言: C#
  • 标签: C#  MODBUS主机  

资源简介

C#实现的MODBUS主机模拟器 支持读写功能码:01,02,03,04,05,06,10,0f 资源介绍:https://blog.csdn.net/dt236688160/article/details/94393546

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace MODBUS_POLL
{
    public partial class Form1 : Form
    {
        private SerialPort com = new SerialPort();
        private string strSendCode = null;
        private int MODBUS_SleepTime = 0;
        private int RegNumMAX = 2048;
        private int MODBUS_LIST_INDEX;//用于读寄存器时,串口发送和读取时,对表格同一行操作
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender EventArgs e)
        {
            string[] portlist = SerialPort.GetPortNames();
            for (int i = 0; i < portlist.Length; i++)
            {
                this.comboBox_Port.Items.Add(portlist[i]);
            }
            comboBox_Baud.Items.Add(“115200“);
            comboBox_Baud.Items.Add(“9600“);
            com.ReadBufferSize = 8192;
            com.DataReceived += serialPort1_DataReceived;//使能串口接收中断
        }
        /************************************************************************************
        ComSend
        串口发送函数
        *************************************************************************************/
        private void ComSend(byte[] data int length)//串口发送函数
        {
            try
            {
                com.Write(data 0 length);
            }
            catch
            {
                label_Info.Text = “串口发送数据出错“;
            }
        }

        /************************************************************************************
        serialPort1_DataReceived
        串口接收中断函数
        *************************************************************************************/
        private void serialPort1_DataReceived(object sender SerialDataReceivedEventArgs e)//串口接收中断函数
        {
            int IdleTime;
            if (strSendCode == “MODBUS_READ“ || strSendCode == “MODBUS_WRITE“) IdleTime = MODBUS_SleepTime / 2;
            else IdleTime = 300;
            Thread.Sleep(IdleTime);
            try
            {
                int recvLen = com.BytesToRead;
                if (recvLen > 8191)//数据太大不处理
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        label_Info.Text = “串口接收缓存溢出“;
                    }));
                    return;
                }
                byte[] recv_buf = new byte[recvLen];
                com.Read(recv_buf 0 recvLen);
                string strRecvBuf = System.Text.Encoding.Default.GetString(recv_buf);
                switch (strSendCode)
                {
                    case “MODBUS_WRITE“:
                        strSendCode = ““;
                        break;
                    case “MODBUS_READ“:
                       

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      67584  2019-01-25 11:43  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\bin\Debug\MODBUS_POLL.exe

     文件      48640  2019-01-25 11:43  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\bin\Debug\MODBUS_POLL.pdb

     文件      22472  2019-01-25 11:48  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\bin\Debug\MODBUS_POLL.vshost.exe

     文件        490  2010-03-17 22:39  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\bin\Debug\MODBUS_POLL.vshost.exe.manifest

     文件      26827  2019-01-25 11:43  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\Form1.cs

     文件      24676  2019-01-25 11:43  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\Form1.Designer.cs

     文件      65626  2019-01-25 11:43  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\Form1.resx

     文件       3735  2019-01-25 10:53  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\MODBUS_POLL.csproj

     文件        863  2019-01-25 10:45  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6921  2019-01-25 11:44  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        942  2019-01-25 11:43  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\MODBUS_MASTER.csproj.FileListAbsolute.txt

     文件        975  2019-01-25 11:43  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\MODBUS_MASTER.csproj.GenerateResource.Cache

     文件       2209  2019-01-25 11:43  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\MODBUS_MASTER.csprojResolveAssemblyReference.cache

     文件        938  2019-01-25 11:48  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\MODBUS_POLL.csproj.FileListAbsolute.txt

     文件        975  2019-01-25 11:07  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\MODBUS_POLL.csproj.GenerateResource.Cache

     文件       2209  2019-01-25 11:04  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\MODBUS_POLL.csprojResolveAssemblyReference.cache

     文件      67584  2019-01-25 11:43  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\MODBUS_POLL.exe

     文件      39412  2019-01-25 11:43  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\MODBUS_POLL.Form1.resources

     文件      48640  2019-01-25 11:43  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\MODBUS_POLL.pdb

     文件        180  2019-01-25 11:04  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\MODBUS_POLL.Properties.Resources.resources

     文件        492  2019-01-25 10:45  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\Program.cs

     文件       1360  2019-01-25 10:45  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\Properties\AssemblyInfo.cs

     文件       2874  2019-01-25 10:45  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\Properties\Resources.Designer.cs

     文件       5612  2019-01-25 10:45  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\Properties\Resources.resx

     文件       1098  2019-01-25 10:45  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\Properties\Settings.Designer.cs

     文件        249  2019-01-25 10:45  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\Properties\Settings.settings

     文件        923  2019-01-25 11:44  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL.sln

    ..A..H.     36864  2019-01-25 11:49  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL.v11.suo

     目录          0  2019-01-25 10:45  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\obj\Debug\TempPE

     目录          0  2019-01-25 11:04  MODBUS_POLL\MODBUS_POLL\MODBUS_POLL\bin\Debug

............此处省略10个文件信息

评论

共有 条评论