简体   繁体   中英

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).

The data is sent as hex values and in 2 byte sizes as the smallest. So when I ask for what is stored in a memory area in the PLC I get a 2 byte hex value back.

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. 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.

The program saves all the data it reads into a MySql database. This is a snipet of the code that parses the values as the come in from the 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. 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)); 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
    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.)
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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