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

哪里的网站建设米东区成业建设集团公司网站

哪里的网站建设,米东区成业建设集团公司网站,装修设计公司快餐店,wordpress区块链快讯模板在 C# 串口通信的一发一收场景里,处理不同功能码的帧数据可采用以下合理的代码结构,它能让代码更具可读性、可维护性和可扩展性。 实现思路 定义帧结构:创建一个类来表示通信帧,其中包含功能码、数据等信息。功能码处理逻辑&…

 

在 C# 串口通信的一发一收场景里,处理不同功能码的帧数据可采用以下合理的代码结构,它能让代码更具可读性、可维护性和可扩展性。

实现思路

  1. 定义帧结构:创建一个类来表示通信帧,其中包含功能码、数据等信息。
  2. 功能码处理逻辑:使用字典或者switch语句来根据不同的功能码执行相应的处理逻辑。
  3. 数据接收与解析:在串口接收事件中解析接收到的数据,并根据功能码调用对应的处理方法。
using System;
using System.Collections.Generic;
using System.IO.Ports;// 定义通信帧类
public class CommunicationFrame
{public byte FunctionCode { get; set; }public byte[] Data { get; set; }public CommunicationFrame(byte functionCode, byte[] data){FunctionCode = functionCode;Data = data;}
}// 定义串口通信类
public class SerialCommunication
{private SerialPort serialPort;private readonly Dictionary<byte, Action<CommunicationFrame>> frameHandlers = new Dictionary<byte, Action<CommunicationFrame>>();public SerialCommunication(string portName, int baudRate){serialPort = new SerialPort(portName, baudRate);serialPort.DataReceived += SerialPort_DataReceived;}// 注册功能码处理方法public void RegisterHandler(byte functionCode, Action<CommunicationFrame> handler){if (!frameHandlers.ContainsKey(functionCode)){frameHandlers.Add(functionCode, handler);}}// 打开串口public void Open(){if (!serialPort.IsOpen){serialPort.Open();}}// 关闭串口public void Close(){if (serialPort.IsOpen){serialPort.Close();}}// 发送数据public void Send(CommunicationFrame frame){if (serialPort.IsOpen){byte[] dataToSend = new byte[1 + frame.Data.Length];dataToSend[0] = frame.FunctionCode;Array.Copy(frame.Data, 0, dataToSend, 1, frame.Data.Length);serialPort.Write(dataToSend, 0, dataToSend.Length);}}// 数据接收处理private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e){try{if (serialPort.BytesToRead > 0){byte functionCode = (byte)serialPort.ReadByte();int dataLength = serialPort.BytesToRead;byte[] data = new byte[dataLength];serialPort.Read(data, 0, dataLength);CommunicationFrame frame = new CommunicationFrame(functionCode, data);if (frameHandlers.TryGetValue(functionCode, out var handler)){handler(frame);}}}catch (Exception ex){Console.WriteLine($"Error receiving data: {ex.Message}");}}
}class Program
{static void Main(){SerialCommunication serialComm = new SerialCommunication("COM1", 9600);// 注册功能码处理方法serialComm.RegisterHandler(1, HandleFunctionCode1);serialComm.RegisterHandler(2, HandleFunctionCode2);serialComm.Open();// 发送示例数据byte[] data = { 0x01, 0x02, 0x03 };CommunicationFrame frameToSend = new CommunicationFrame(1, data);serialComm.Send(frameToSend);Console.WriteLine("Press any key to exit...");Console.ReadKey();serialComm.Close();}// 处理功能码 1 的方法static void HandleFunctionCode1(CommunicationFrame frame){Console.WriteLine($"Received frame with function code 1. Data length: {frame.Data.Length}");}// 处理功能码 2 的方法static void HandleFunctionCode2(CommunicationFrame frame){Console.WriteLine($"Received frame with function code 2. Data length: {frame.Data.Length}");}
}


文章转载自:

http://U5vvQU0i.dbhnx.cn
http://zUQ14SGl.dbhnx.cn
http://WbiBBPs4.dbhnx.cn
http://kcUqf93b.dbhnx.cn
http://o7bi4CZ4.dbhnx.cn
http://BnEsrQe7.dbhnx.cn
http://OYqAdtMX.dbhnx.cn
http://av1FexEw.dbhnx.cn
http://pZlWbUsM.dbhnx.cn
http://KMeL4lpn.dbhnx.cn
http://l0Rkvfjo.dbhnx.cn
http://8bmcYI5u.dbhnx.cn
http://fiRuS2zd.dbhnx.cn
http://mIdZmH91.dbhnx.cn
http://4IrGHQfb.dbhnx.cn
http://6CHZ3TIp.dbhnx.cn
http://lmOoRYBN.dbhnx.cn
http://zvpNjCbu.dbhnx.cn
http://plmcQ8K4.dbhnx.cn
http://BWcueN71.dbhnx.cn
http://siir4s0x.dbhnx.cn
http://n3jcYxFT.dbhnx.cn
http://HtlDnYab.dbhnx.cn
http://342XGOte.dbhnx.cn
http://6OobvVgB.dbhnx.cn
http://f6Mx3PX3.dbhnx.cn
http://lyJ5g9xh.dbhnx.cn
http://fGz87ngq.dbhnx.cn
http://6ExEqpNg.dbhnx.cn
http://fFBcPjj8.dbhnx.cn
http://www.dtcms.com/wzjs/738933.html

相关文章:

  • 自动化网站建设公司做网站做什么类型的网站好
  • 建材 团购 网站怎么做网站建设 力洋网络
  • 出名的建站网站做石膏选图形的网站
  • 深圳网站备案查询新冠流行最新消息
  • 淄博张店做网站的公司网页浏览器怎么设置
  • 网站界面是什么做的贵阳小程序商城建设
  • 网站做商标在那类建设工程教育网网址
  • 遵义公司网站制作哪家好做万词霸屏后网站关键词没有排名
  • 中山做网站企业wordpress能不买服务器
  • 网站建设包含哪些方面微信运营者和管理员的区别
  • 艺术学院网站模板广州微信网站建设咨询
  • 张掖建设网站云南建设招标网站
  • 集团门户网站建设不足中国纪检监察报范耀庚
  • 装修招投标网站建设网站收录平台方法
  • 做互动电影的网站重庆大渝网最新消息
  • 建网站可以铺货手机交互网站
  • 网上做室内设计好的网站网站建设好后怎样形成app
  • 做便民工具网站怎么样专业网站设计制作
  • 大型电商网站开发方案中国建筑出版在线官网app
  • wordpress下载站主题长沙网站建设及推广公司
  • 深圳网站改版公司成都微信网站建设多少钱
  • 海宁市规划建设局网站求网址直接能看的2021
  • 松原手机网站开发公司手机装wordpress
  • 网站的 联系我们怎么做阿里oss wordpress
  • 网站需要去工信部做备案聊城高端网站建设
  • 免费网页奖励自己游戏网站郴州网红店
  • 如何做网站服务器映射网站一直不被收录
  • 北海网站制作东圃那里有做网站设计的
  • 全球知名电子商务网站统计免费做外贸的网站平台
  • 虹口高端网站建设网站规划与建设报告怎么写