C#上位机与三菱PLC的通信09---开发自己的通讯库(A-3E版)

1、A-3E报文回顾

 

 具体细节请看:

C#上位机与三菱PLC的通信05--MC协议之QnA-3E报文解析

C#上位机与三菱PLC的通信06--MC协议之QnA-3E报文测试

2、为何要开发自己的通讯库
  

 前面开发了自己的A-1E协议的通讯库,实现了数据的读写,对于封装的通讯库,其实是一个dll文件,请看上节的dll文件,有了这个文件,就可以在项目中直接引用 。

我们只要引用并调用相关的方法即可实现目的, 但写一个通讯库需要非凡的技术,需要考虑的东西很多,比如扩展性,通用性,等等之类的。通过封装通讯库达到更高的层次, 大师就是这样锻造出来的,接下来马上安排A-3E协议的封装,代码是基于上节的基础上添加。

 3、说干就干

1、添加类文件

2、编写核心的通信类A3E.cs

A3E.cs完整代码

using Mitsubishi.Communication.MC.Mitsubishi.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Mitsubishi.Communication.MC.Mitsubishi
{
    /// <summary>
    /// A3E报文通讯库
    /// </summary>
    public class A3E : MelsecBase
    {
        byte _netCode = 0x00, _stationCode = 0x00;

        public A3E(string ip, short port, byte net_code = 0x00, byte station_code = 0x00) : base(ip, port)
        {
            _netCode = net_code;
            _stationCode = station_code;
        }

        #region 读数据
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <typeparam name="T">读取的数据类型</typeparam>
        /// <param name="address">存储区地址</param>
        /// <param name="count">读取长度</param>
        /// <returns></returns>
        public Result<T> Read<T>(string address, short count)
        {
            AreaCode areaCode; string start;
            (areaCode, start) = this.AnalysisAddress(address);
            return Read<T>(areaCode, start, count);
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <typeparam name="T">读取的数据类型</typeparam>
        /// <param name="areaCode">存储区代码</param>
        /// <param name="startAddr">开始地址</param>
        /// <param name="count">读取长度</param>
        /// <returns></returns>
        public Result<T> Read<T>(AreaCode areaCode, string startAddr, short count)
        {
            Result<T> result = new Result<T>();
            try
            {
                // 连接
                var connectState = this.Connect();
                if (!connectState.IsSuccessed)
                {
                    throw new Exception(connectState.Message);
                }
                // 子命令(位/字)
                byte readCode = (byte)(typeof(T) == typeof(bool) ? 0x01 : 0x00);
                //开始地址
                List<byte> startBytes = this.StartToBytes(areaCode, startAddr);
                // 读取长度
                int typeLen = this.CalculatLength<T>();


                // 读取报文
                List<byte> bytes = new List<byte>
                {
                    0x50,0x00,//请求副头部,固定50 00
                    _netCode,// 网络号,根据PLC的设置
                    0xFF,//PLC编号,固定值
                    0xFF,0x03,//目标模块IO编号,固定FF 03
                    _stationCode,// 目标模块站号 
                    0x0C,0x00,  // 剩余字节长度
                    0x0A,0x00,//PLC响应超时时间,以250ms为单位计算 
                    0x01,0x04,// 成批读取
                    readCode,0x00,// 字操作  0x0001 
                    startBytes[0],startBytes[1],startBytes[2],// 起始地址
                    (byte)areaCode,// 区域代码 
                    (byte)(typeLen*count%256),
                    (byte)(typeLen*count/256%256) //长度
                };
                //发送报文
                List<byte> dataBytes = this.Send(bytes, 0);
                //数据解析
                result.Datas = this.AnalysisDatas<T>(dataBytes, typeLen);
            }
            catch (Exception ex)
            {
                result = new Result<T>(false, ex.Message);
            }
            return result;
        }
        #endregion

        #region 写数据

        /// <summary>
        /// 写入数据
        /// </summary>
        /// <typeparam name="T">写入的数据类型</typeparam>
        /// <param name="values">写入的数据列表</param>
        /// <param name="address">开始地址</param>
        /// <returns></returns>
        public Result Write<T>(List<T> values, string address)
        {
            AreaCode areaCode; string start;
            (areaCode, start) = this.AnalysisAddress(address);
            return this.Write<T>(values, areaCode, start);
        }

        /// <summary>
        /// 写入数据
        /// </summary>
        /// <typeparam name="T">写入的数据类型</typeparam>
        /// <param name="values">写入的数据列表</param>
        /// <param name="areaCode">存储区代码</param>
        /// <param name="address">开始地址</param>
        /// <returns></returns>
        public Result Write<T>(List<T> values, AreaCode areaCode, string startAddr)
        {
            Result result = new Result();

            try
            {
                // 连接
                var connectState = this.Connect();
                if (!connectState.IsSuccessed)
                {
                    throw new Exception(connectState.Message);
                }
                // 子命令(位/字)
                byte writeCode = (byte)(typeof(T) == typeof(bool) ? 0x01 : 0x00);

                // 起始地址  XY    直接翻译  100   00 01 00    D100  64 00 00
                List<byte> startBytes = this.StartToBytes(areaCode, startAddr);
                //计算数据类型的长度
                int typeLen = this.CalculatLength<T>();
                int count = values.Count;
                //计算数据的字节列表
                List<byte> datas = this.GetDataBytes<T>(values);
                List<byte> baseBytes = new List<byte>
                {
                    0x50,0x00,
                    this._netCode,// 可变,根据PLC的设置
                    0xFF,//PLC编号,固定值
                    0xFF,0x03,//目标模块IO编号,固定FF 03
                    this._stationCode,// 可变,目标模块站号
                };
                //0x0E,0x00,  // 剩余字节长度
                List<byte> commandBytes = new List<byte> {
                    0x0A,0x00,//超时时间
                    0x01,0x14,// 成批写入
                    writeCode,0x00,// 字操作
                    startBytes[0],startBytes[1],startBytes[2],// 起始地址
                    (byte)areaCode,// 区域代码 
                    (byte)(typeLen*count%256),
                    (byte)(typeLen*count/256%256), //长度
                };
                commandBytes.AddRange(datas);
                baseBytes.Add((byte)(commandBytes.Count % 256));
                baseBytes.Add((byte)(commandBytes.Count / 256 % 256));
                baseBytes.AddRange(commandBytes);
                socket.Send(baseBytes.ToArray());

                // 解析响应
                byte[] respBytes = new byte[11];
                socket.Receive(respBytes, 0, 11, SocketFlags.None);

                // 状态
                if ((respBytes[9] | respBytes[10]) != 0x00)
                {
                    throw new Exception("响应异常。" + respBytes[9].ToString() + respBytes[10].ToString());
                }
            }
            catch (Exception ex)
            {
                result.IsSuccessed = false;
                result.Message = ex.Message;
            }

            return result;
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 地址解析
        /// </summary>
        /// <param name="address">地址字符串</param>
        /// <returns></returns>
        public Tuple<AreaCode, string> AnalysisAddress(string address)
        {
            // 取两个字符
            string area = address.Substring(0, 2);
            if (!new string[] { "TN", "TS", "CS", "CN" }.Contains(area))
            {
                area = address.Substring(0, 1);
            }
            string start = address.Substring(area.Length);

            // 返回一个元组对象 
            return new Tuple<AreaCode, string>((AreaCode)Enum.Parse(typeof(AreaCode), area), start);
        }


        /// <summary>
        /// 发送报文
        /// </summary>
        /// <param name="reqBytes">字节列表</param>
        /// <param name="count"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public override List<byte> Send(List<byte> reqBytes, int count)
        {
            socket.Send(reqBytes.ToArray());
            // 解析
            byte[] respBytes = new byte[11];
            socket.Receive(respBytes, 0, 11, SocketFlags.None);

            // 状态
            if ((respBytes[9] | respBytes[10]) != 0x00)
            {
                throw new Exception("响应异常。" + respBytes[9].ToString() + respBytes[10].ToString());
            }
            // 数据长度 
            int dataLen = BitConverter.ToUInt16(new byte[] { respBytes[7], respBytes[8] },0) - 2;  // -2 的意思去除响应代码(状态)
            byte[] dataBytes = new byte[dataLen];
            socket.Receive(dataBytes, 0, dataLen, SocketFlags.None);
            return new List<byte>(dataBytes);
        }
        #endregion

        #region plc控制
        /// <summary>
        /// PLC远程启动
        /// </summary>
        /// <returns></returns>
        public Result Run()
        {
            return PlcStatus(0x01, new List<byte> { 0x00, 0x00 });
        }
        /// <summary>
        /// PLC远程停止
        /// </summary>
        /// <returns></returns>
        public Result Stop()
        {
            return PlcStatus(0x02);
        }
        /// <summary>
        /// PLC运行状态
        /// </summary>
        /// <param name="cmdCode"></param>
        /// <param name="cmd"></param>
        /// <returns></returns>
        private Result PlcStatus(byte cmdCode, List<byte> cmd = null)
        {
            Result result = new Result();
            try
            {
                var connectState = this.Connect();
                if (!connectState.IsSuccessed)
                {
                    throw new Exception(connectState.Message);
                }
                List<byte> commandBytes = new List<byte>
                {
                    0x50,0x00,
                    this._netCode,// 可变,根据PLC的设置
                    0xFF,
                    0xFF,0x03,
                    this._stationCode,// 可变
                };
                //0x08,0x00,  // 剩余字节长度
                List<byte> cmdBytes = new List<byte> {
                    0x0A,0x00,
                    cmdCode,0x10,
                    0x00,0x00,
                    0x01,0x00,//模式
                };
                if (cmd != null)
                {
                    cmdBytes.AddRange(cmd);
                }

                commandBytes.Add((byte)(commandBytes.Count % 256));
                commandBytes.Add((byte)(commandBytes.Count / 256 % 256));
                commandBytes.AddRange(cmdBytes);

                socket.Send(commandBytes.ToArray());

                byte[] respBytes = new byte[11];
                socket.Receive(respBytes, 0, 11, SocketFlags.None);

                // 状态
                if ((respBytes[9] | respBytes[10]) != 0x00)
                {
                    throw new Exception("响应异常。" + respBytes[1].ToString());
                }
            }
            catch (Exception ex)
            {
                result.IsSuccessed = false;
                result.Message = ex.Message;
            }
            return result;
        }
        #endregion
    }
}

 4、测试通讯库

1、启动MC服务器

2、利用通讯库读写数据

1、读取D区100开始的3个short数据

 

2、读取M区100开始的5个float数据

 

 

3、读取X区100开始的4个bool数据

 

4、写入M区200开始的2个short数据

 

5、写入D区200开始的5个float数据

 

 

3、完整代码

 /// <summary>
 /// 测试A-3E通讯库
 /// </summary>
 static void MCLibTestA3E()
 {
     A3E qNA3E = new A3E("192.168.1.7", 6000);
     #region 读数据
     //Console.WriteLine("读取D区100开始的3个short数据");
     //var result1 = qNA3E.Read<short>("D100", 3);
     //if (result1.IsSuccessed)
     //{
     //    result1.Datas.ForEach(d => Console.WriteLine(d));
     //}
     //else
     //{
     //    Console.WriteLine(result1.Message);
     //}
     //Console.WriteLine("读取M区100开始的5个float数据");
     //var result2 = qNA3E.Read<float>("M100", 5);
     //if (result2.IsSuccessed)
     //{
     //    result2.Datas.ForEach(d => Console.WriteLine(d));
     //}
     //else
     //{
     //    Console.WriteLine(result2.Message);
     //}
     //Console.WriteLine("读取X区100开始的4个bool数据");
     //var result3 = qNA3E.Read<bool>(AreaCode.X, "100", 4);
     //if (result3.IsSuccessed)
     //{
     //    result3.Datas.ForEach(d => Console.WriteLine(d));
     //}
     //else
     //{
     //    Console.WriteLine(result3.Message);
     //}

     #endregion

     #region 写数据

     Console.WriteLine("写入M区200开始的2个short数据");
     var result4 = qNA3E.Write<short>(new List<short> { -541, 982 }, "M200");
     if (result4.IsSuccessed)
     {
         Console.WriteLine(result4.Message);
     }
     Console.WriteLine("写入D区200开始的5个float数据");
     var result5 = qNA3E.Write<float>(new List<float> { 111, 0, -8076, 13.67f, -985.325f }, "D200");
     if (result5.IsSuccessed)
     {
         Console.WriteLine(result5.Message);
     }
     #endregion 
 }

5、小结

原创真的不容易,走过路过不要错过,点赞关注收藏又圈粉,共同致富。

原创真的不容易,走过路过不要错过,点赞关注收藏又圈粉,共同致富。

原创真的不容易,走过路过不要错过,点赞关注收藏又圈粉,共同致富

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/400712.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

vue中切换tab时echart不显示或显示不正常

项目中在不同的tab中都使用了echart&#xff0c;但是在切换tab的时候发现第二个tab没有正常显示&#xff0c;通过排查代码和网上查阅才发现是因为element是通过display来控制tab的显示的&#xff0c;没有点击tab2的时候第二个echart图表的容器是 display:none&#xff0c;echar…

压缩感知常用的重建算法

重建算法的基本概念 在压缩感知&#xff08;Compressed Sensing, CS&#xff09;框架中&#xff0c;重建算法是指将从原始信号中以低于奈奎斯特率采集得到的压缩测量值恢复成完整信号的数学和计算过程。由于信号在采集过程中被压缩&#xff0c;因此重建算法的目标是找到最符合…

一文了解大数据生态

大数据一词最早指的是传统数据处理应用软件无法处理的过于庞大或过于复杂的数据集。 现在&#xff0c;对“大数据”一词的使用倾向于使用预测分析、用户行为分析或者其他一些从大数据中提取价值的高级数据分析方法&#xff0c;很少用于表示特定规模的数据集。 定义 大数据是…

FariyGUI × Cocos Creator 入门

前言 程序员向的初探Cocos Creator结和FairyGUI的使用&#xff0c;会比较偏向FairyGUI一点&#xff0c;默认各位读者都熟练掌握Cocos Creator以及js/ts脚本编写。 初探门径&#xff0c;欢迎大佬指教&#xff0c;欢迎在评论区或私信与本人交流&#xff0c;谢谢&#xff01; 下…

微服务篇之雪崩、降级和熔断

一、服务雪崩 服务雪崩&#xff1a;一个服务失败&#xff0c;导致整条链路的服务都失败的情形。 二、服务降级 服务降级是服务自我保护的一种方式&#xff0c;或者保护下游服务的一种方式&#xff0c;用于确保服务不会受请求突增影响变得不可用&#xff0c;确保服务不会崩溃。 …

docker pullpush 生成镜像文件并push 到阿里云

pull docker docker pull ultralytics/ultralytics # 拉取yolov8的镜像仓库 docker run -it ultralytics/ultralytics # 运行镜像 conda create -n gsafety python3.8 # 创建环境 source activate gsafety # 激活环境 pip install -i https://pypi.tuna.tsinghua.edu.cn/simp…

C++ Primer 笔记(总结,摘要,概括)——第7章 类

目录 ​编辑 7.1 定义抽象数据类型 7.1.1 设计Sales_data类 7.1.2 定义改进的Sales_data类 7.1.3 定义类相关的非成员函数 7.1.4 构造函数 7.1.5 拷贝、赋值和析构 7.2 访问控制和封装 7.2.1 友元 7.3 类的其他特性 7.3.1 类成员再探 7.3.2 返回*this的成员函数 7.3.3 类类…

【机器学习科学库】全md文档笔记:Jupyter Notebook和Matplotlib使用(已分享,附代码)

本系列文章md笔记&#xff08;已分享&#xff09;主要讨论人工智能相关知识。主要内容包括&#xff0c;了解机器学习定义以及应用场景&#xff0c;掌握机器学习基础环境的安装和使用&#xff0c;掌握利用常用的科学计算库对数据进行展示、分析&#xff0c;学会使用jupyter note…

电脑远控工具Venom Rat 毒液的测试和预防

电脑远控工具的概念 电脑远控工具是一种软件程序&#xff0c;能够让用户通过网络在远程位置控制另一台计算机。使用远控工具&#xff0c;用户可以在不同地点之间实现计算机的连接和控制&#xff0c;方便远程管理、技术支持、远程教学等应用场景。远控工具通常包括远程桌面查看…

Redis之缓存击穿问题解决方案

文章目录 一、书接上文二、介绍三、解决方案1. 单例双检锁2. 缓存预热和定时任务 一、书接上文 Redis之缓存雪崩问题解决方案 二、介绍 缓存击穿就是大量并发访问同一个热点数据&#xff0c;一旦这个热点数据缓存失效&#xff0c;则请求压力都来到数据库。 三、解决方案 1…

git版本回退在eclipse和命令中的操作

一.背景 老程序员了&#xff0c;熟悉eclipsesvn&#xff0c;git用的不溜。近几年用了git&#xff0c;偶尔修改了某个文件希望放弃本次修改重新恢复到最新版本重新修改。或者回退到某个版本&#xff0c;再修改。记录一下Eclipse中的操作&#xff0c;和命令操作的情况。 二.Ecli…

如何在debian上实现一键恢复操作系统?

在Debian或任何其他Linux发行版上实现一键恢复操作系统&#xff0c;需要创建一个系统镜像或快照&#xff0c;并设置一个简单的方法来从该镜像恢复。以下是创建和恢复系统的基本步骤&#xff1a; 1. 创建系统镜像&#xff1a; 使用像dd&#xff0c;rsync或专门的备份工具&#…

解决Uncaught SyntaxError: Cannot use import statement outside a module(at XXX)报错

报错原因&#xff1a;这个错误通常是因为你正在尝试在一个不支持 ES6 模块语法的环境中使用 import 语句。这可能是因为你的代码是在一个只支持 CommonJS 或 AMD 模块系统的环境中运行的&#xff0c;或者你的代码运行的环境没有正确配置以支持 ES6 模块。如果是在浏览器环境&am…

C++面向对象程序设计-北京大学-郭炜【课程笔记(四)】

C面向对象程序设计-北京大学-郭炜【课程笔记&#xff08;四&#xff09;】 1、this指针1.1、this指针的作用1.2、this指针和静态成员函数 2、静态成员变量和静态成员函数2.1、基本概念2.2、基本概念总结2.3、如何访问静态成员2.4、静态成员变量的使用场景&#xff08;重要&…

浏览器垃圾回收机制与 Vue 项目内存泄漏场景分析

目录 一、Vue项目 二、浏览器垃圾回收机制 三、内存泄漏场景 四、Vue项目会内存泄漏吗&#xff1f; 一、Vue项目 Vue.js 是一种流行的JavaScript 框架&#xff0c;用于构建用户界面。以下是关于Vue项目的一些基本信息和概念&#xff1a; 安装 Vue&#xff1a;您可以通过使…

探究网络工具nc(netcat)的使用方法及安装步骤

目录 &#x1f436;1. 什么是nc&#xff08;netcat&#xff09;&#xff1f; &#x1f436;2. nc&#xff08;netcat&#xff09;的基本使用方法 2.1 &#x1f959;使用 nc 进行端口监听 2.2 &#x1f959;使用 nc 进行端口扫描 2.3 &#x1f959;使用 Netcat 进行文件传输…

报表控件Stimulsoft 新版本2024.1中,功能区工具栏新功能

今天&#xff0c;我们将讨论Stimulsoft Reports、Dashboards 和 Forms 2024.1版本中的一项重要创新 - 在一行中使用功能区工具栏的能力。 Stimulsoft Ultimate &#xff08;原Stimulsoft Reports.Ultimate&#xff09;是用于创建报表和仪表板的通用工具集。该产品包括用于WinF…

第六十六天 API安全-接口安全阿里云KEY%postmanDVWSXEE鉴权泄露

第66天 API安全-接口安全&阿里云KEY%postman&DVWS&XEE&鉴权&泄露 知识点 1.HTTP类接口-测评 2.RPC类接口-测评 3.Web Service类-测评 参考链接&#xff1a;https://www.jianshu.com/p/e48db27d7c70 内容点&#xff1a; SOAP(Simple Object Access Prot…

板块一 Servlet编程:第八节 文件上传下载操作 来自【汤米尼克的JavaEE全套教程专栏】

板块一 Servlet编程&#xff1a;第八节 文件的上传下载操作 一、文件上传&#xff08;1&#xff09;前端内容&#xff08;2&#xff09;后端内容 二、文件下载&#xff08;1&#xff09;前端的超链接下载&#xff08;2&#xff09;后端下载 在之前的内容中我们终于结束了Servle…

《安全历史第1讲——从故宫修建看软件物料清单的重要性》

故宫&#xff0c;这座中国传统文化的重要代表和象征性建筑已屹立近600年&#xff0c;是世界上现存规模最大、保存最为完整的木质结构古建筑之一。 故宫之所以能至今保存完好&#xff0c;除持续保护和修缮外&#xff0c;其使用的木材和砖石等材料也经过了精挑细选&#xff0c;保…
最新文章