简体   繁体   English

C#中的动态类型加载

[英]Dynamic type loading in C#

I'm using a protocol named Ethernet Industrial Protocol (EIP) and I use it to send and receive data from a Programmable Logic Controller (PLC). 我正在使用名为以太网工业协议(EIP)的协议,并使用它来发送和接收来自可编程逻辑控制器(PLC)的数据。

The data is sent as hex values and in 2 byte sizes as the smallest. 数据以十六进制值发送,最小2字节。 So when I ask for what is stored in a memory area in the PLC I get a 2 byte hex value back. 因此,当我询问存储在PLC的存储区中的内容时,我会得到一个2字节的十六进制值。

Currently I'm using hardcoded approach for parsing the data that comes back. 目前,我正在使用硬编码方法来解析返回的数据。 What I'm looking at is the ability to use a config file or something instead to tell what the string of bytes should look like. 我正在查看的是能够使用配置文件或其他工具来告知字节串应该是什么样的功能。

Let's say I have 3 temperature readings and product type, the 3 temperatures are floating point and use 4 bytes per and the product type is an integer. 假设我有3个温度读数和产品类型,这3个温度是浮点数,每个使用4个字节,产品类型是整数。 If I want to change it I need to change the program.. 如果要更改它,则需要更改程序。

What should I read up on to be able to change this in for instance a config file instead of rewriting the application? 我应该继续阅读哪些内容才能在配置文件中进行更改,而不是重写应用程序? I want to be able to say that I have x number of instances of this type and the program should then parse it as that. 我想说的是,我有x个这种类型的实例,然后程序应将其解析为该实例。

The program saves all the data it reads into a MySql database. 该程序将读取的所有数据保存到MySql数据库中。 This is a snipet of the code that parses the values as the come in from the PLC. 这是对从PLC传入的值进行解析的代码的摘要。

Krakk = (BitConverter.ToUInt16(data, bIndex)); bIndex += 2;
Small = (BitConverter.ToUInt16(data, bIndex)); bIndex += 2;
Medium = (BitConverter.ToUInt16(data, bIndex)); bIndex += 2;
Large = (BitConverter.ToUInt16(data, bIndex)); bIndex += 2;

If I use a config file I would like to say something in the lines of: name, uint, size and the program should then read that. 如果我使用配置文件,我想在以下几行中说:name,uint,size,然后程序应读取该内容。 So for instance -> Krakk, uint16, 2 and then the program would know that it should change that out for this: Krakk = (BitConverter.ToUInt16(data, bIndex)); 因此,例如-> Krakk,uint16,2,然后程序将知道应该对此进行更改:Krakk =(BitConverter.ToUInt16(data,bIndex)); bIndex += 2; bIndex + = 2;

Even though I think you are already answering yourself, but here is my answer with some details: You may need to create a new custom configuration section ( How to Create and Access a Custom Configuration ) with four properties as follows: 即使我认为您已经在回答自己,但是这是我的答案,其中包含一些细节:您可能需要使用以下四个属性创建一个新的自定义配置部分( 如何创建和访问自定义配置 ):

  1. ReadingName (accepts string value and represents the name of the ReadingName(接受字符串值,代表
    instance you want to read). 您想阅读的实例)。
  2. StartIndex (accepts integer value and represents the starting index from which you start reading the data bytes of the instance.) StartIndex(接受整数值,表示您从该索引开始读取实例的数据字节。)
  3. Length (accepts integer value and represents the number of data bytes of the instance.) 长度(接受整数值并表示实例的数据字节数。)
  4. Data Type. 数据类型。

In the app.config file you should add a section for each instance you want to read, then in your program you should read these values and act accordingly. 在app.config文件中,应为每个要读取的实例添加一个部分,然后在程序中应读取这些值并采取相应的措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM