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
/* 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
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
TestIo is a function that checks if a input is on or off.
raptorsix
RE: Help with C program "if" statement
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 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);
}