LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

EDITING THE CALL BACK

okay in this function below im calling the BIT CONVERSION FUNCTION AND THE BINARY CONVERSION FUNCTION.

 

but when i run the programm and i hit the commandbutton on my gui for the the attn button , it only performs the bit conversion instead of spitting out the binary

 

numbers or running the BINARY CONVERSION FUNCTION. CAN SOMEONE HELP WITH THIS TO AVIOD THAT. IM POSTING MY HEADERFILE also at the bottom of the code.

 

****************************************
int CVICALLBACK UpDate (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{     
    int test=0;
    int command_addr;
    int upper=0;
    int lower=0;
    int switch2;
    int new_value=1;
    int sw_value;
    int CHANGE_CHAR_TO_INTERGER=0,inputControl=0;
        switch(event)
        {
        case EVENT_COMMIT:
           

   //OPEN_TEXT_FILE();
  STATION_CTR_INI_FILE ();


   BIT_CONVERSION((control-12), (control-6));

   BINARY_CONVERSION(control-12,control-6);
  

  GetCtrlVal (panelHandle, (control-12), &sw_value);

  SET_SWITCH((control-15), sw_value);
  


int BINARY_CONVERSION(int inputControl, int outputControl)
{
 int currentPosition = 0;
 int i = 0, inputValue=0;
 char outputString[64];
 
 // read the input value
 GetCtrlVal (panelHandle, inputControl, &inputValue);
  
 // clear the output string
 outputString[0] = '\0';
  
 // convert to binary
 
 // count the bits needed
 do{
 } while (inputValue >= pow(2,currentPosition++));
  
 // show a minumum of 8 bits
 if (currentPosition<8)
  currentPosition=8;
   
 // terminate the string
 outputString[currentPosition] = '\0';
  
 // fill in one bit at a time, from LSB to MSB
 do{
  if (inputValue & 1 << i++)
   outputString[--currentPosition] = '1';
  else
   outputString[--currentPosition] = '0';
 } while (currentPosition > 0);
 
 SetCtrlVal (panelHandle, outputControl, outputString);
  
 return 0;
}


int BIT_CONVERSION(int inputControl, int outputControl)

{
 char myString[16];   // 8 characters followed by NULL

 int bitValue=0;

 GetCtrlVal (panelHandle, inputControl, &bitValue);
 


 strcpy(myString, "00000000");

 // set one bit to 1
 if (bitValue > 0)
  myString[8 - bitValue] = '1';
 
 
 SetCtrlVal (panelHandle, outputControl, myString);


return 0;
 
}
******************************************************************

 

 

#include <userint.h>

#ifdef __cplusplus
    extern "C" {
#endif

     /* Panels and Controls: */

#define  PANEL                            1
#define  PANEL_NUMERIC_1                  2
#define  PANEL_NUMERIC_2                  3
#define  PANEL_NUMERIC_3                  4
#define  PANEL_NUMERIC_4                  5
#define  PANEL_NUMERIC_5                  6
#define  PANEL_NUMERIC_6                  7       /* callback function: DECIMAL_INPUT */
#define  PANEL_BINARY_READ_1              8
#define  PANEL_BINARY_READ_2              9
#define  PANEL_BINARY_READ_3              10
#define  PANEL_BINARY_READ_4              11
#define  PANEL_BINARY_READ_5              12
#define  PANEL_BINARY_READ_6              13
#define  PANEL_COMMANDBUTTON_1            14      /* callback function: UpDate */
#define  PANEL_COMMANDBUTTON_2            15      /* callback function: UpDate */
#define  PANEL_COMMANDBUTTON_3            16      /* callback function: UpDate */
#define  PANEL_COMMANDBUTTON_4            17      /* callback function: UpDate */
#define  PANEL_COMMANDBUTTON_5            18      /* callback function: UpDate */
#define  PANEL_COMMANDBUTTON_6            19      /* callback function: UpDate */
#define  PANEL_RESETBUTTON                20      /* callback function: Reset */
#define  PANEL_UPDATEBUTTON               21      /* callback function: UpDate */
#define  PANEL_QUITBUTTON                 22      /* callback function: QuitCallback */
#define  PANEL_TEXTMSG_6                  23
#define  PANEL_TEXTMSG_5                  24
#define  PANEL_TEXTMSG_4                  25
#define  PANEL_TEXTMSG_3                  26
#define  PANEL_TEXTMSG_2                  27
#define  PANEL_TEXTMSG                    28


     /* Menu Bars, Menus, and Menu Items: */

          /* (no menu bars in the resource file) */


     /* Callback Prototypes: */

int  CVICALLBACK DECIMAL_INPUT(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
int  CVICALLBACK QuitCallback(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
int  CVICALLBACK Reset(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
int  CVICALLBACK UpDate(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);


#ifdef __cplusplus
    }
#endif

0 Kudos
Message 1 of 8
(4,915 Views)

Darnell:

 

Have you tried stepping through your code to see what is happening?

 

Based on a quick review of your code, it looks like you are calling BIT_CONVERSION first, then immediately calling BINARY_CONVERSION, which just overwrites the results of BIT_CONVERSION.

 

On another note, I would strongly reccommend against doing math on the constant control IDs (e.g. control-12, control-6).  I understand that you are trying to use a generic callback function to do all you updates, but there are at least two problems with doing math on the control IDs.

1. You don't directly control the ID assignments (the UI editor does).  Those assignments may change when you make some edits in the UI editor.

2. It makes your program harder to follow, debug, and maintain.

 

To keep your callback generic, I would put all the control constants in an array, one row per relationalship.  Your callback can find the calling control, and will know the output you want to affect.

 

Step through the code and see if it's doing what you think it is.  If you're still having problems, post your project (not including the .exe, .cdb or cvibuild) so we can look at it.

0 Kudos
Message 2 of 8
(4,907 Views)

ok i just post my project, let me try. also can i see what you talking about the control constants in an array, one row per relationalship.

 

can i see an example that may be a good thing to do. but the way i did , i have always use my controls that way  for the last two years. but what i do is 

 

do a tab order always on how i want my UIR file to operator in order, then i use the controls with math. just an old trick. and also how do i give credit on NI.com ?

0 Kudos
Message 3 of 8
(4,903 Views)

nevermind i got it to work.  

 

 

 

i did 

 

 

 

  if (control == PANEL_COMMANDBUTTON_6)
                    BINARY_CONVERSION((control-12), (control-6));
  else
      BIT_CONVERSION((control-12), (control-6));
 

0 Kudos
Message 4 of 8
(4,890 Views)

Darnell:

 

If you're interested in getting away from doing control ID math as I suggested earlier, here are a couple of snippets of code that demonstrate one way of using the control ID constants in an array.

 

Put this array definition in your global section (before main()).

 

// define the relationships between controls:
// each row is one relationship, with the command button, input control, and output control
int g_ControlID_Sets[][3] = {
 {PANEL_COMMANDBUTTON_1, PANEL_NUMERIC_1, PANEL_BINARY_READ_1},
 {PANEL_COMMANDBUTTON_2, PANEL_NUMERIC_2, PANEL_BINARY_READ_2},
 {PANEL_COMMANDBUTTON_3, PANEL_NUMERIC_3, PANEL_BINARY_READ_3},
 {PANEL_COMMANDBUTTON_4, PANEL_NUMERIC_4, PANEL_BINARY_READ_4},
 {PANEL_COMMANDBUTTON_5, PANEL_NUMERIC_5, PANEL_BINARY_READ_5},
 {PANEL_COMMANDBUTTON_6, PANEL_NUMERIC_6, PANEL_BINARY_READ_6},
 {-1, -1, -1} // signal the end of the array: a control ID will never be -1
};

// add this new function

 

// findControlSet() looks for the passed control in the array
// of control sets, and returns the row number if found.
// if control is not found, return -1
int findControlSet(int control)
{
 int controlSetRow = 0;
 
 while (g_ControlID_Sets[controlSetRow][0] != control
  && g_ControlID_Sets[controlSetRow][0] != -1)
  controlSetRow++;
 
 if (g_ControlID_Sets[controlSetRow][0] == -1)
  controlSetRow = -1;
 
 return controlSetRow;
}

 

// replace your calls using control ID math with this

 

  controlSetRow = findControlSet(control);
 
  if (controlSetRow >= 0)
   {
   if (control == PANEL_COMMANDBUTTON_6)
    BINARY_CONVERSION(g_ControlID_Sets[controlSetRow][1],g_ControlID_Sets[controlSetRow][2]);
   else
    BIT_CONVERSION(g_ControlID_Sets[controlSetRow][1],g_ControlID_Sets[controlSetRow][2]);
   }
  else
  MessagePopup ("Control not found", "This should only happen during debug");

That just illustrates one way of using the control IDs directly, still using a generic Update function, but avoiding control ID math.

 

I know your project is a work in process, but here are just a couple of other things I noticed.  When you starting posting questions about this project earlier, you said that you wanted just one update button and that you didn't want to update each switch individually.  Now you're doing them individually, but your Update button is now connected to code that won't work with it: you're now basing your updates on which button was pushed.  The Update button isn't related to one input and output control, so your control ID math won't work as intended.

 

If you still want your update button to update all at once, and you implement the type of control ID array as the example I show here, you could do something like this.

Change the callback for the Update button to Update_all.

 

int CVICALLBACK Update_all (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int controlSetRow = 0;

 switch (event)
  {
  case EVENT_COMMIT:

   while (g_ControlID_Sets[controlSetRow][0] != -1)
    {
    if (g_ControlID_Sets[controlSetRow][0] == PANEL_COMMANDBUTTON_6)
       BINARY_CONVERSION(g_ControlID_Sets[controlSetRow][1],g_ControlID_Sets[controlSetRow][2]);
      else
       BIT_CONVERSION(g_ControlID_Sets[controlSetRow][1],g_ControlID_Sets[controlSetRow][2]);
    
    controlSetRow++;
    }

   break;
  }
 return 0;
}

 

In your Reset function, why are you calling BIT_CONVERSION and BINARY_CONVERSION?

Don't you want to just set the input to 0 and the outputs to blank?

0 Kudos
Message 5 of 8
(4,882 Views)
it works, im giving the engineer the option to update all switches at once and then im giving him the option to check the individual switches at once. so the engineer can either update all at once by hitting the update button or he can check a individual switch by hitting the send_switch button
0 Kudos
Message 6 of 8
(4,876 Views)

With regard on the "math-on-UI-constants" argument Al pointed to, this discussion can be of some help to understand the issue and pros and cons of the different methods involved.

 

As a side note on your project, I noted that you are opening the port in MASTER_RESET_UPDATE_DEVICE () function and you are never closing it anywere in the project: this can lead to errors starting from the second time the function is executed. In an application that needs to communicate over the serial port I usually open the port before the call to RunUserInterface () and leave it open during all program life, closing it only after QuitUserInterface () has been called to terminate it.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 7 of 8
(4,863 Views)
thanks i will be sure to keep that in mind, i thought i close it. Im looking over all my mistakes to nite,  got make my code robust. I got a peer review coming up. any advice on more advice on my code that i need to change. thanks
0 Kudos
Message 8 of 8
(4,860 Views)