Solved

How to write special code ?

Posted on 2013-12-20
2
264 Views
Last Modified: 2014-03-03
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
Comment
Question by:naseeam
2 Comments
 
LVL 12

Accepted Solution

by:
OnegaZhang earned 500 total points
ID: 39733225
"there are about half a dozen special parameters."
I would recommend switch statement.
0
 
LVL 1

Author Comment

by:naseeam
ID: 39752882
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 109
wordappend challenge 8 203
drawing animated level bar based on numbers 3 104
Copy Files - Python 7 89
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question