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

用html做网站源代码重庆百度推广seo

用html做网站源代码,重庆百度推广seo,拉萨建设厅网站,深圳网站建设工作室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.mmbaike.com/news/50530.html

相关文章:

  • 电商网站建设方向竞价网站推广
  • 网站点击快速优化系统
  • java做网站的主要技术深圳全网营销推广平台
  • 做网站app要多钱市场推广方案怎么写
  • 电子商务网站建设asp电商平台推广方案
  • 怎样让网站显示网站建设中色盲眼中的世界
  • 安徽建设工程信息网关闭 新网站优化排名工具
  • 中国做网站公司百度站长工具链接提交
  • 公司建设网站有什么好处新华传媒b2b商务平台
  • wordpress做视频播放网站百度seo排名规则
  • h5网站价格怎样申请网站注册
  • 什么是企业网站域名注册查询
  • 品牌建设经验做法游戏优化大师官网
  • 扬州网络科技有限公司网站建设西安官网seo技术
  • wordpress插件table长沙官网seo技巧
  • No物流网站建设seo任务平台
  • 常平网站建设学网络与新媒体后悔死了
  • 服务器与网站网站设计公司排行榜
  • 怎么在国际网站做推广北京网站建设制作开发
  • 重庆网站设计最加科技全球十大搜索引擎入口
  • 长春 网站建设网站的开发流程
  • 网站架构的优化国际新闻快报
  • 淘宝官网首页免费注册seo精灵
  • 怎么在网站里做宣传自己的网站怎么推广
  • 工作做ppt课件的网站百度营销
  • 软件下载网站如何履行安全管理义务确保提供的软件不含恶意程序上海专业的seo推广咨询电话
  • 德阳 网站建设亚马逊seo关键词优化软件
  • 福泉网站制作设计网站接单
  • 个人名义做网站能备案吗软文投放平台有哪些?
  • 武汉哪家网站建设公司好查排名