Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

How to write special code ?

We have a large table which is array of structure.  Among other information, each table entry contains ParameterId and Modbus Register Number.

In a tight while loop we find out how many consecutive mobus registers table entries are there, then we call a function to read modbus registers and store values in variables that were dynamically allocated during init.  This works for most parameters but some parameters are special.

I call them special because I can't simply store values read from modbus register into variables.  Some processing is needed first.  For example, Product ID read from modbus register needs to be mapped to another number and then stored into a variable.

So, special code is required to map integer product ID read from mobus register to 67, and 67 needs to be stored in the variable.  I can do the processing in the tight while loop but I don't want to clutter it.  In the tight while loop I can say if parameter id is product ID, then map it's value to 67 and store 67 in the variable.  If there were one or two special parameters then I could do the processing in the tight while loop but there are about half a dozen special parameters.

I'm thinking about following design for the special parameters:  Add special parameter bits member at the end of the structure as follows:

unsigned_16  special_parameters;

Bit 0 would indicate that this is a special parameter.  Bits 1 - 15 would indicate type of special parameters.  For example, bits 1 - 15 can be defined as follows:

Bit 1 -  Map 16-bit modbus register value to some integer, then store in a variable.
Bit 2 -  Swap 16-bit modbus register bytes, then store in a variable.
Bit 3 -  Read part of 16-bit modbus register, and store in a variable.  Read remaining part
           of modbus register and store in another variable.

Bit 4 - .....
....

Here is portion of tight while loop that reads 16-bit words from modbus registers and stores them in variables.

modbusError = _modMgrSocketPtr->readHoldingRegs(startReg, regSize, responseBuffer);
      if (modbusError == Modbus::MessageSuccess)
      { // Force values read to write into parameter space.
        unsigned short buffIndex = regOffset;
        unsigned short dataSize = 0;
        for (unsigned int i = infoIndex; i <= endIndex; ++i )
        {
          dataSize = pTable[i].NumRegisters;
          pTable[i].Param->setValueRaw((char *)&responseBuffer[buffIndex], dataSize * 2, true );
          buffIndex += dataSize;
        }
      }

Open in new window


In the beginning of the for loop body read special_parameters bit 0 to find out it it's a special parameter.  If it is, call some special function that processes special parameters.  In the special function, read special_ parameters bits 1 - 15 to find out how it's special.  Maybe special function will have a switch statement with case statements that will process the special parameter.  

Please code my design or come up with different design and code it.  Please connect new code to the tight while loop.

Code the case where Product ID read from modbus register is mapped to 67 and this number is store in a variable.


Thank you!
0
naseeam
Asked:
naseeam
1 Solution
 
OnegaZhangCommented:
"there are about half a dozen special parameters."
I would recommend switch statement.
0
 
naseeamAuthor Commented:
So, it looks like you approve one part of my design but I didn't receive comments on major part of my design.  I'm planning to add following at the end of the structure:

unsigned_16  special_parameter_bits;

Adding this variable and defining each of the bits, do you approve this part of design?
Also, how to check for special parameter in tight loop and call special function to process special parameters.  Do you approve that ?

Or is there a better solution ?

I also asked to add code in tight loop to check for special parameter and call function for special processing.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now