LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to get an array of integers from user on the User Interface

Solved!
Go to solution

Hi all. I am not a very experienced CVI user. I almost exclusively code in LabVIEW but have a client that wants all their work done in CVI.

 

I have been very successful so far (implemented a Modbus API from scratch that supports all connection types) but am stumped on how I can get multiple integers from the user without limiting the number of integers I can get. I essentially need to be able to get an array of integers from the user.

 

Some background, I'm building a UI for testing my Modbus API. There are Modbus functions for "Write Multiple Registers" and "Write Multiple Coils". I want the user to be able to input all the values they want to write in one single control.

 

What is the best way to get an array of integers via the UI?

0 Kudos
Message 1 of 3
(4,896 Views)
Solution
Accepted by OptiJohn

The easiest way is probably to let the user input the numbers into a textbox control: items can be separated with spaces, commas or even a line break (using ctrl+Enter). You will need then to retrieve the string from the textbox and parse it for numerics using any non-numeric character as a separator: strspn (string, "0123456789."); can be used for that.

 

Another, more complex possibility could be to use a table control to receive user input: either you prepare a blank table with a reasonable number of roews, or you will need some coding to add new rows as long as the user fills all the existing ones. On the other hand, it will make retrieving data simpler since in a simple instruction you'll have all values back: GetTableCellRangeVals (panelHandle, PANEL_TABLE, VAL_TABLE_COLUMN_RANGE (1), array, VAL_COLUMN_MAJOR);



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?
Message 2 of 3
(4,871 Views)

Here's a quick example of how you can use the table to let the user enter an integer array.

 

#include <cvirte.h>
#include <userint.h>

static void SetupUI(void);
static int CVICALLBACK PanelCB (int panel, int event, void *callbackData,
                         int eventData1, int eventData2);
int CVICALLBACK TableCB (int panel, int control, int event,
                         void *callbackData, int eventData1, int eventData2);

static int gPanel, gTable;

int main (int argc, char *argv[])
{
    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;  /* out of memory */
    SetupUI();
    DisplayPanel (gPanel);
    RunUserInterface ();
    DiscardPanel (gPanel);
    return 0;
}

static void SetupUI(void)
{
    gPanel = NewPanel(0, "", 100, 100, 520, 200);
    SetPanelAttribute(gPanel, ATTR_CALLBACK_FUNCTION_POINTER, PanelCB);
    SetPanelAttribute(gPanel, ATTR_CONFORM_TO_SYSTEM_THEME, 1);
    SetPanelAttribute(gPanel, ATTR_CONFORM_TO_SYSTEM, 1);
    gTable = NewCtrl(gPanel, CTRL_TABLE_LS, "Integer Array", 50, 10);
    SetCtrlAttribute(gPanel, gTable, ATTR_LABEL_LEFT, 10);
    SetCtrlAttribute(gPanel, gTable, ATTR_WIDTH, 132);
    SetCtrlAttribute(gPanel, gTable, ATTR_HEIGHT, 400);
    SetCtrlAttribute(gPanel, gTable, ATTR_AUTO_EDIT, 1);
    SetCtrlAttribute(gPanel, gTable, ATTR_CALLBACK_FUNCTION_POINTER, TableCB);
    SetCtrlAttribute(gPanel, gTable, ATTR_CELL_TYPE, VAL_CELL_NUMERIC);
    SetCtrlAttribute(gPanel, gTable, ATTR_DATA_TYPE, VAL_INTEGER);
    SetCtrlAttribute(gPanel, gTable, ATTR_CELL_JUSTIFY, VAL_CENTER_LEFT_JUSTIFIED);
    SetCtrlAttribute(gPanel, gTable, ATTR_COLUMN_LABELS_VISIBLE, 0);
    SetCtrlAttribute(gPanel, gTable, ATTR_ROW_LABELS_VISIBLE, 1);
    SetCtrlAttribute(gPanel, gTable, ATTR_SCROLL_BARS, VAL_VERT_SCROLL_BAR);
    SetCtrlAttribute(gPanel, gTable, ATTR_SCROLL_BAR_SIZE, VAL_MEDIUM_SCROLL_BARS);
    InsertTableRows(gPanel, gTable, -1, 1, VAL_CELL_NUMERIC);
    InsertTableColumns(gPanel, gTable, -1, 1, VAL_CELL_NUMERIC);
    SetTableColumnAttribute(gPanel, gTable, 1, ATTR_COLUMN_WIDTH, 75);
}

static void CVICALLBACK SizeTableEditState (void *callbackData)
{
    SetCtrlAttribute(gPanel, gTable, ATTR_TABLE_RUN_STATE, VAL_EDIT_STATE);    
}

int CVICALLBACK TableCB (int panel, int control, int event,
                         void *callbackData, int eventData1, int eventData2)
{
    Point cell;
    switch (event)
    {
        case EVENT_KEYPRESS:
            GetActiveTableCell(panel, control, &cell);
            if (VAL_ENTER_VKEY == GetKeyPressEventVirtualKey(eventData2))
            {
                int num;
                GetNumTableRows(panel, control, &num);
                if (cell.y == num)
                {
                    InsertTableRows(panel, control, -1, 1, VAL_CELL_NUMERIC);
                    cell.y++;
                    SetActiveTableCell(panel, control, cell);
                    PostDeferredCall(SizeTableEditState, 0);
                }
            }
            break;
    }
    return 0;
}

int CVICALLBACK PanelCB (int panel, int event, void *callbackData,
                         int eventData1, int eventData2)
{
    switch (event)
    {
        case EVENT_CLOSE:
            QuitUserInterface (0);
            break;
    }
    return 0;
}
 

 

0 Kudos
Message 3 of 3
(4,821 Views)