×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Help with C program "if" statement

Help with C program "if" statement

Help with C program "if" statement

(OP)
I am trying to debug some equipment and am really kind of new at c programming and wonder if anyone can help me with this.  I kind of need to know what this if statement is doing.

 /* Check the vial code from the change parts V1.05 */
    if ((State == INIT_STATE) && (Command == CMD_INIT))
    {
        code = 0;
        if (TestIo(DI_ProductCode0)) code = code | 0x01;
        if (TestIo(DI_ProductCode1)) code = code | 0x02;
        if (TestIo(DI_ProductCode2)) code = code | 0x04;
        if (TestIo(DI_ProductCode3)) code = code | 0x08;
        if (DEBUG) printf("Vial code is: %d\n", code);
        DMC(&CurRecipe, DMC_READ_SINGLE);
        if (DEBUG) printf("Recipe code is: %d\n", ProdCode[CurRecipe.User.VialType]);
        DispatchAlarm(AlmPath, &Alarms[AL_VIAL_CODE_ERR], ProdCode[CurRecipe.User.VialType] != code);
    }

It should be comparing a hard-wired code coming from some change parts with one set in a recipe.

Thanks,

raptorsix

RE: Help with C program "if" statement

Is this code snippet working at all?

What does TestIo do, exactly?  It's called 4 times, so is there an expectation that something will get changed each time or are the results mutually exclusive?

TTFN

RE: Help with C program "if" statement

(OP)
if (TestIo(DI_ProductCode0)) code = code | 0x01;

TestIo is a function that checks if a input is on or off.

raptorsix

RE: Help with C program "if" statement

The way I read it is that first the code is checking if the system has just initiated in that it is looking at the initial state as the programmer has named the variable.

Then initializing the variable "code" to zero (0)

The if statements appear to be cascaded in that each true return on the conditional goes to the next if statement. If it was me programming I would of indented each if statement to show the relationship between each other.

(TestIo(DI_ProductCode0)) code  statement i believe is a type-cast of the function TestIo which is probably a member of some class that has the ability to poll an IO port.  I cant remember but I think the return type of TestIo(DI_ProductCodeX) could return a value to be stored in code.  DI_ProductCode0, 1, 2, 3 could be four seperate IO ports.


Then exclusive "or" -ing each bit of the alarm word in code by the " | " symbol. I am pulling this from memory.  Have not sat and coded in a while.
 
The 0x01 , 0x02 , 0x04, 0x08 are the first four bits in a binary number.

the "printf" command is to display text to your monitor

DMC is probably some type of function call.  The &CurRecipe
is variable and the "&" is called a pass by reference, the variable CurRecipe is used in the function "DMC" and also some where else.  Read up on "Passing by Reference"

Alot of this is subjective in that I am tyring to infer also by the descriptive names the original programmer used.   Hope this helped.  

RE: Help with C program "if" statement

I am not going to risk any guesses without knowing the application , but a simple harness like the one I am pasting below should help you explore all paths.
I haven't initialised the Alram codes etc. as I don't know what they are and there are some assumptions made with regards to data types.

I have supplied the functions which are being called in your snippet of code . You can change them to be more elaborate.


#include <stdio.h>
#include <stdlib.h>
#include <windows.h>



#define DEBUG 1
#define DMC_READ_SINGLE 1
#define AL_VIAL_CODE_ERR 5


#define DI_ProductCode0 0
#define DI_ProductCode1 1
#define DI_ProductCode2 2
#define DI_ProductCode3 3

/* typedefs */
typedef enum
{
TYPE_0,
TYPE_1,
TYPE_MAX

}TYPE;

typedef struct
{

TYPE VialType;

}USER;
typedef struct
{

USER User;

}RECIPE;

/* gloabls */

enum {
INIT_STATE,
NON_INIT_STATE

}State;



enum
{
CMD_INIT,

CMD_NON_INIT
}Command;

/* Instantiate RECIPE */

RECIPE CurRecipe;

/* dummy arrays and Paths */


unsigned int ProdCode[10];
unsigned int AlmPath =1;
unsigned int Alarms [10];

/* Harness function prototypes */
BOOLEAN TestIo(unsigned int Prod_code);
void DMC(RECIPE * rec, unsigned int count);
void DispatchAlarm(unsigned int Path, unsigned int* Alm_code, BOOLEAN IsAlarm);




void main (void)

{
unsigned int code;
State = INIT_STATE;
Command = CMD_INIT;


/* Check the vial code from the change parts V1.05 */
    if ((State == INIT_STATE) && (Command == CMD_INIT))
    {
        code = 0;
        if (TestIo(DI_ProductCode0)) code = code | 0x01;
        if (TestIo(DI_ProductCode1)) code = code | 0x02;
        if (TestIo(DI_ProductCode2)) code = code | 0x04;
        if (TestIo(DI_ProductCode3)) code = code | 0x08;
        if (DEBUG) printf("Vial code is: %d\n", code);
        DMC(&CurRecipe, DMC_READ_SINGLE);
        if (DEBUG) printf("Recipe code is: %d\n", ProdCode[CurRecipe.User.VialType]);
        DispatchAlarm(AlmPath, &Alarms[AL_VIAL_CODE_ERR], ProdCode[CurRecipe.User.VialType] != code);
    }

}



BOOLEAN TestIo(unsigned int Prod_code)
{


 switch (Prod_code)
 {
 case 0:
     return TRUE;
 case 1:
     return TRUE;
 case 2:
     return FALSE;

 case 3:
     return TRUE;

 default:
   
    /* error */
         break;
 }




}





void DMC(RECIPE * rec, unsigned int count)
{

if (count== DMC_READ_SINGLE)
             rec->User.VialType=1;




}




void DispatchAlarm(unsigned int Path, unsigned int *Alm_code, BOOLEAN IsAlarm)
{

/* fill in the alarm dispatch code */
    if (IsAlarm)
         printf("Alarm code %d\n",&Alm_code);


}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources