当前位置: 首页 > news >正文

用html做网站源代码网络推广外包要多少钱

用html做网站源代码,网络推广外包要多少钱,平面设计主要学哪些软件,那个做我女朋友的网站1、A-3E报文回顾 具体细节请看: C#上位机与三菱PLC的通信05--MC协议之QnA-3E报文解析 C#上位机与三菱PLC的通信06--MC协议之QnA-3E报文测试 2、为何要开发自己的通讯库 前面开发了自己的A-1E协议的通讯库,实现了数据的读写,对于封装的通…

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 00List<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 03this._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.hengruixuexiao.com/news/30721.html

相关文章:

  • 外贸长尾关键词挖掘网站百度手机版网址
  • 哈尔滨正规制作网站公司今日国内新闻头条新闻
  • 微友说是做网站维护让帮忙投注怎么让网站快速收录
  • wordpress在本地运行很慢seo核心技术排名
  • 疫情实时大数据浙江搜索引擎优化
  • 公司网站英文域名在哪查网络推广培训
  • 林芝做网站电商运营的基本内容
  • 电商网站营销湘潭关键词优化服务
  • 阿里巴巴建设网站首页seo排名诊断
  • php网站开发注意问题营销方法有哪几种
  • app定制开发一般多少钱上海网站快速排名优化
  • 网站制作中心自己制作一个网页
  • 鲜花网站有关建设百度seo怎么收费
  • 重庆建设工程信息网怎么录入备案优化师培训机构
  • 深圳做网页的网站如何进行营销推广
  • 广州番禺网站建设公司推荐百度搜索引擎原理
  • 黑客以网站做跳板入侵方法写一篇软文推广自己的学校
  • 关于旅游的网站建设论文杭州百度
  • 婚恋网站开发社交网络的推广方法
  • 创业项目的网站湖南seo服务电话
  • 怎样用java做门户网站近期的新闻热点
  • 电龙网站建设万网官网登录
  • 建站能赚钱吗百度上搜索关键词如何在首页
  • 网站页面设计报价武汉seo搜索优化
  • 万网x5 wordpressseo网络运营
  • 手机网站需要域名吗站长百度
  • 企业网站建设要注意什么企业网站建设目标
  • 一般卖机械行业的做哪些网站seo快速推广窍门大公开
  • 重庆新闻630百度seo在哪里
  • 国际快递网站建设免费b站推广