Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to write special code ?

Posted on 2013-12-20
2
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
OnegaZhang earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

715 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