Solved

How to write special code ?

Posted on 2013-12-20
2
262 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
 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now