LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

CNVString

I am not able to get strings using the network variable library.
 
Can anybody solve may problem ?
Thank you in advance.
 
Paolo
 
 
EXAMPLE :
******************************************************************************
WRITER
******************************************************************************
#include <windows.h>
#include <ansi_c.h>
#include <cvinetv.h>
#include <cvirte.h>  
#include <userint.h>
#include "Writer.h"
#include <utility.h>
//-----------------------------------------------------------------------------
// Macros
//-----------------------------------------------------------------------------
#define errChk(f) if (error = (f), error < 0) goto Error; else
//#define DOUBLEDATA  1
 

static int panelHandle;
char message [50];
double doubleValue;

CNVWriter writer = 0;
CNVData  data = 0;

//-----------------------------------------------------------------------------
// Entry-point function
//-----------------------------------------------------------------------------
int main (int argc, char *argv[])
{
 int error = 0;
 int status = 0;

 if (InitCVIRTE (0, argv, 0) == 0)
  return -1; /* out of memory */
 if ((panelHandle = LoadPanel (0, "Writer.uir", PANEL)) < 0)
  return -1;

    // Create a buffered writer
#ifdef DOUBLEDATA
 //    errChk(CNVCreateBufferedWriter(NETWORK_VARIABLE_PATH, 0, 0, 0, 64, 5000, 0, &writer));
 errChk(CNVCreateWriter ("\\\\localhost\\paolo\\doubleValue", 0, 0, 1000, 0, &writer));
#else
 errChk(CNVCreateWriter ("\\\\localhost\\paolo\\stringa", 0, 0, 1000, 0, &writer));
#endif
 
#ifdef DOUBLEDATA
 status = CNVCreateScalarDataValue (&data, CNVDouble, 123.456);
#else
 status = CNVCreateScalarDataValue (&data, CNVString, "");
#endif

 DisplayPanel (panelHandle);
 RunUserInterface ();
 DiscardPanel (panelHandle);
 
 
Error:
    // Cleanup
    if (data)
        CNVDisposeData(data);
    if (writer)
        CNVDispose(writer);
    // Cleanup Network Variable Library
    CNVFinish();
    // Report error
    if (error < 0)
    {
        fprintf(stderr, "\n%s", CNVGetErrorDescription(error));
        fflush(stdin);
        fgetc(stdin);
    }

 return 0;
}
int CVICALLBACK QUITBUTTON (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   QuitUserInterface (0);
   break;
 }
 return 0;
}
int CVICALLBACK CB_Timer (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int status = 0;
 switch (event)
 {
  case EVENT_TIMER_TICK:
  
#ifdef DOUBLEDATA
   GetCtrlAttribute (panel, PANEL_DOUBLE, ATTR_CTRL_VAL, &doubleValue);
     status = CNVSetScalarDataValue(data, CNVDouble, doubleValue);
   status = CNVWrite (writer, data, CNVWaitForever);
#else
   GetCtrlAttribute (panel, PANEL_MESS, ATTR_CTRL_VAL, message);
     status = CNVSetScalarDataValue(data, CNVString, message);
   status = CNVWrite (writer, data, CNVWaitForever);
#endif   
   break;
 }
 return 0;
}
 
 
 
 
******************************************************************************
READER
******************************************************************************
#include <cvinetv.h>
#include <cvirte.h>  
#include <userint.h>
#include "Reader.h"
//-----------------------------------------------------------------------------
// Macros
//-----------------------------------------------------------------------------
#define errChk(f) if (error = (f), error < 0) goto Error; else
//#define DOUBLEDATA 1

static int panelHandle;
CNVReader reader = 0;
CNVData  data = 0;
double doubleValue = 0.0;
char message [100];

int main (int argc, char *argv[])
{
 int error = 0;
 int status = 0;
 
 
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1; /* out of memory */
 if ((panelHandle = LoadPanel (0, "Reader.uir", PANEL)) < 0)
  return -1;
 
 
#ifdef DOUBLEDATA
 //    errChk(CNVCreateBufferedWriter(NETWORK_VARIABLE_PATH, 0, 0, 0, 64, 5000, 0, &writer));
 errChk(CNVCreateReader ("\\\\localhost\\paolo\\doubleValue", 0, 0, 1000, 0, &reader));
#else
 errChk(CNVCreateReader ("\\\\localhost\\paolo\\stringa", 0, 0, 1000, 0, &reader));
#endif 
#ifdef DOUBLEDATA
 status = CNVCreateScalarDataValue (&data, CNVDouble, 123.456);
#else
 status = CNVCreateScalarDataValue (&data, CNVString, "");
#endif 
 
 DisplayPanel (panelHandle);
 RunUserInterface ();
Error :
 
 DiscardPanel (panelHandle);
    // Clean up
    if (reader)
        CNVDispose (reader);
 
 return 0;
}
int CVICALLBACK QUITBUTTON (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   QuitUserInterface (0);
   break;
 }
 return 0;
}
int CVICALLBACK CB_Timer (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int status;
 
 
 switch (event)
 {
  case EVENT_TIMER_TICK:
#ifdef DOUBLEDATA
   status = CNVRead (reader, 1000, &data);
   status = CNVGetScalarDataValue(data, CNVDouble, &doubleValue);
   SetCtrlAttribute (panel, PANEL_DOUBLE, ATTR_CTRL_VAL, doubleValue);
#else
   status = CNVRead (reader, 1000, &data);
     status = CNVGetScalarDataValue(data, CNVString, message);
   SetCtrlAttribute (panel, PANEL_MESS, ATTR_CTRL_VAL, message);
#endif 
   break;
 }
 return 0;
}
0 Kudos
Message 1 of 3
(3,691 Views)
After briefly looking over your code, I think your main problem is that you are trying to get the string value into a buffer, but the function expects you to pass the address of a pointer. Afterward, you have to free the memory the function allocates to store the string using CNVFreeMemory.

Your code:

char message[50];
status = CNVRead (reader, 1000, &data);
status = CNVGetScalarDataValue(data, CNVString, message);

Correct code:

char *message = 0;
status = CNVRead (reader, 1000, &data);
status = CNVGetScalarDataValue(data, CNVString, &message);
// use message
CNVFreeMemory(message);

Hope this helps,

-alex
0 Kudos
Message 2 of 3
(3,678 Views)

Sorry ! My fault !

Hope to be more helpful next time.

Thank you

Paolo

0 Kudos
Message 3 of 3
(3,664 Views)