LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Method of getting 2 values simultaneously using RS232C serial communication

Solved!
Go to solution

This may be due basically to two reasons: either

1. updated measures are not sent by the onstrument, or

2. sent measures are not received / displayed correctly

 

I suggest you place a breakpoint wher you are expected to receive measures and see if new values are received or not: if they are received you may have problems on the display side, if not you must check your interaction with the instrument.

 

Useful questions that can help you in troubleshooting problems on the communications side:

1. Does the instrument send continuosly or does it require a poll? In the second case, are you actually asking for new measures?

2. Are you getting new strings from the instrument or not? Are you sure your query is correct every time?

3. Are you getting errors in RS232 functions? Are you actually handling function errors?



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 11 of 47
(2,508 Views)

Thanks for your reply.

 

Actually I had already verfied sensor and cable with windows hyper terminal. yes, they are working properly, sensor sends 2 values (torque,r.p.m) to device with its usual speed and while taking data with RS232 sample program no error is shown by RS232 functions.

but I am not getting new values on the screen.

0 Kudos
Message 12 of 47
(2,501 Views)

I was assuming that cabling is correct and communications establishes at least once. My notes were aimed to understand why you cannot get new values after the first (correct, I presume) one.

 

You haven't answered my questions that may help in discriminating what's happening:

1. Does the instrument require polling or does it send measures continuously?

2. If it requires polling, are you actually sending it periodically? If not, do you see new characters at the serial port (GetInQLen)?

3. Are you getting errors in communications?

 

Can you post your code? Only communications with the instrument is needed, you can leave all other stuff away.



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 13 of 47
(2,496 Views)

hey,

Instrument dosen't need polling it sends measure continuously and when I press GetInQLen I got value of Input queue length 4096.

So, when I started taking values. The value shown by readbox were (S1,NT,+000000 &S2,NT,+000.002) and when I flushed Input queue and then value changes.

I think may be read box is not showing real values it shows past values or you can say store value. Is there any method like instead of pressing read button again & again.I will press read button one time and all values are to be shown by read box.

and I tried to attach code but word limit was here. I am using same example as give by NI/CVI lab windows for serial:- (how to communicate with an RS-232 device. This example sends commands to a serial device and receives data through the serial port. You can configure the communication port for speed, handshaking mode, and so on. This example uses the OpenComConfig, ComWrtByte, and ComRdByte functions. You use these functions to open the serial port, write to the port, and read from the port, respectively.)

 


           
      
   



   
        
    
   
   
   
   

0 Kudos
Message 14 of 47
(2,491 Views)

Without looking at the code is difficult to understand what's happening. Extract only the relevant portion of the code, paste it to a new source file and attach it to your posts.

 

Which control are you using to display measures? "read box" does not explains which one you are using but I suppose it's a String or Text Box control, which should update when you write new values to them. Are you using a loop inside a control callback? If to, adding ProcessDrawEvents inside the loop may help.

 

As Wolfgang suggested some post ago, using a timer can be of help if you want to acquire continuously: simply put the acquisition / display code inside the timer callback an run the example. Pay attention to the structure he has sketched: open the port when you want to start acquiring and close if when you have finished; do not open/close the port on each read.



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 15 of 47
(2,493 Views)

This is example which I am using( Iam not using send part of this example) :-

short read_cnt;
double timeout;
char devicename[30],
     send_data[500],
     read_data[5000],
     tbox_read_data[5000],
     com_msg[500],
     msg[100];

#define QuitHelp        1
#define InputqHelp      2

void DisplayRS232Error (void);
void SetConfigParms (void);
void GetConfigParms (void);
void DisplayHelp (int);
void EnablePanelControls (int);
void DisplayComStatus (void);

int main (int argc, char *argv[])
{
    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;
    panel_handle = LoadPanel (0, "serial.uir", SERIAL);
    DisplayPanel (panel_handle);
    RunUserInterface ();
    DiscardPanel (panel_handle);
    CloseCVIRTE ();
    return 0;
}
int CVICALLBACK ConfigCallBack (int panel, int control, int event,
                                void *callbackData, int eventData1,
                                int eventData2)
{
    switch (event)
        {
        case EVENT_COMMIT:
            config_handle = LoadPanel (panel_handle, "serial.uir", CONFIG);
            InstallPopup (config_handle)
            if (config_flag)    
                SetConfigParms ();
            else                
                config_flag = 1;
            break;
        case EVENT_RIGHT_CLICK :
            break;
        }
    return(0);
}
int CVICALLBACK CloseConfigCallback (int panel, int control, int event,
                                     void *callbackData, int eventData1,
                                     int eventData2)
{
    switch (event)
        {
        case EVENT_COMMIT :
            port_open = 0;  
            GetConfigParms ();
            DisableBreakOnLibraryErrors ();
            RS232Error = OpenComConfig (comport, devicename, baudrate, parity,
                                        databits, stopbits, inputq, outputq);
            EnableBreakOnLibraryErrors ();
            if (RS232Error) DisplayRS232Error ();
            if (RS232Error == 0)
                {
                port_open = 1;
                GetCtrlVal (config_handle, CONFIG_XMODE, &xmode);
                SetXMode (comport, xmode);
                GetCtrlVal (config_handle, CONFIG_CTSMODE, &ctsmode);
                SetCTSMode (comport, ctsmode);
                GetCtrlVal (config_handle, CONFIG_TIMEOUT, &timeout);
                SetComTime (comport, timeout);
                EnablePanelControls (0); 
                }
            else
                EnablePanelControls (1); 
            DiscardPanel (config_handle);
            break;
        }
    return(0);
}

void EnablePanelControls (int enable)
{
    SetCtrlAttribute (panel_handle, SERIAL_SEND, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_READ, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_READ_COUNT, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_TBOX_READ, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_BYTES, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_ERROR, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_FLUSHINQ, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_FLUSHOUTQ, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_GETINQ, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_GETOUTQ, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_COMSTATUS, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_READTERM, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_SENDMODE, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_RCV_HELP_MSG, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_TRANS_HELP_MSG, ATTR_DIMMED, enable);
    SetCtrlAttribute (panel_handle, SERIAL_CLEARBOX, ATTR_DIMMED, enable);
    ActivateSendControls (enable);
}
int CVICALLBACK ClearBoxCallBack (int panel, int control, int event,
                                  void *callbackData, int eventData1,
                                  int eventData2)
{
    if (event == EVENT_COMMIT)
        ResetTextBox (panel_handle, SERIAL_TBOX_READ, "\0");
    return 0;
}
int CVICALLBACK SendModeCallBack (int panel, int control, int event,
                                  void *callbackData, int eventData1,
                                  int eventData2)
{
    if (event == EVENT_COMMIT)
        ActivateSendControls (0);
    return 0;
}
int CVICALLBACK FlushInCallBack (int panel, int control, int event,
                                 void *callbackData, int eventData1,
                                 int eventData2)
{
    if (event == EVENT_COMMIT)
        {
        FlushInQ (comport);
        MessagePopup ("RS232 Message", "Input queue flushed.");
        }
    return 0;
}
int CVICALLBACK FlushOutQCallBack (int panel, int control, int event,
                                   void *callbackData, int eventData1,
                                   int eventData2)
{
    if (event == EVENT_COMMIT)
        {
        FlushOutQ (comport);
        MessagePopup ("RS232 Message", "Output queue flushed.");
        }
    return 0;
}
int CVICALLBACK GetInQCallBack (int panel, int control, int event,
                                void *callbackData, int eventData1,
                                int eventData2)
{
    if (event == EVENT_COMMIT)
        {
        inqlen = GetInQLen (comport);
        Fmt (msg, "%s<Input queue length = %i", inqlen);
        MessagePopup ("RS232 Message", msg);
        }
    return 0;
}
int CVICALLBACK GetOutQCallBack (int panel, int control, int event,
                                 void *callbackData, int eventData1,
                                 int eventData2)
{
    if (event == EVENT_COMMIT)
        {
        outqlen = GetOutQLen (comport);
        Fmt (msg, "%s<Output queue length = %i", outqlen);
        MessagePopup ("RS232 Message", msg);
        }
    return 0;
}
int CVICALLBACK ComStatusCallBack (int panel, int control, int event,
                                   void *callbackData, int eventData1,
                                   int eventData2)
{
    if (event == EVENT_COMMIT)
        {
        com_status = GetComStat (comport);
        DisplayComStatus ();
        }
    return 0;
}

int CVICALLBACK ErrorCallBack (int panel, int control, int event,
                               void *callbackData, int eventData1,
                               int eventData2)
{
    switch (event)
        {
        case EVENT_COMMIT:
            RS232Error = ReturnRS232Err ();
            DisplayRS232Error ();
            break;
        case EVENT_RIGHT_CLICK :
            break;
        }
    return 0;
}
int CVICALLBACK ReadCallBack (int panel, int control, int event,
                              void *callbackData, int eventData1,
                              int eventData2)
{
    switch (event)
        {
        case EVENT_COMMIT:
            read_data[0] = '\0';
            GetCtrlVal (panel_handle, SERIAL_READ_COUNT, &read_cnt);
            GetCtrlIndex (panel_handle, SERIAL_READTERM, &read_term_index);
            switch (read_term_index)
                {
                case 0:
                    read_term = 0;
                    break;
                case 1:
                    read_term = 13;
                    break;
                case 2:
                    read_term = 10;
                    break;
                }
            if (read_term)
                bytes_read = ComRdTerm (comport, read_data, read_cnt,
                                        read_term);
            else
                bytes_read = ComRd (comport, read_data, read_cnt);
            CopyString (tbox_read_data, 0, read_data, 0, bytes_read);
            SetCtrlVal (panel_handle, SERIAL_TBOX_READ, tbox_read_data);
            RS232Error = ReturnRS232Err ();
            if (RS232Error)
                DisplayRS232Error ();
            break;
        case EVENT_RIGHT_CLICK :
            break;
        }
    return 0;
}
int CVICALLBACK InputQCallBack (int panel, int control, int event,
                                void *callbackData, int eventData1,
                                int eventData2)
{
    if (event == EVENT_RIGHT_CLICK)
        DisplayHelp (InputqHelp);
    return 0;
}
int CVICALLBACK QuitCallBack (int panel, int control, int event,
                              void *callbackData, int eventData1,
                              int eventData2)
{
    switch (event)
        {
        case EVENT_COMMIT :
            if (port_open)
                {
                outqlen = GetOutQLen (comport);
                if (outqlen > 0)
                    {
                    MessagePopup ("RS232 Message", "The output queue has\n"
                                    "data in it. Wait for device to receive\n"
                                    "the data or flush the queue.\n");
                    break;
                    }
                RS232Error = CloseCom (comport);
                if (RS232Error)
                    DisplayRS232Error ();
                }
            QuitUserInterface (0);
            break;
        case EVENT_RIGHT_CLICK :
            DisplayHelp (QuitHelp);
            break;
        }
    return 0;
}

 

0 Kudos
Message 16 of 47
(2,485 Views)

Ok, this is more or less what CVI serial basic  example does, but what about your code? Or are you simply running the example and try to get data?

 

An, one time more, do you receive some error in reading?



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 17 of 47
(2,478 Views)

No I didnt recieve any error and yeah I already told you that I am using example now. I am  running the example and try to get data

0 Kudos
Message 18 of 47
(2,469 Views)

I need help regarding that data coming to read box are like in this format:-

S1,NT,+000.000 (torque)

S2,NT,+000.002 (r.p.m)

S1,NT,+000.001 (torque)

S2,NT,+000.003 (r.p.m)

.

.

.

.

 

but If I use these 2 different values in two different numeric panel instead of getting this value in read box and yeah, for real time I will use timer.

0 Kudos
Message 19 of 47
(2,466 Views)

Once you receive the string in ComRdyou can use sscanf (ANSI C) or Scan (CVI I/O library) to extract speed / torque values, next SetCtrlVal to display them in the appropriate panel / control. Details on sscanf can be found in any C manual, while CVI offers a detailed help with plenty of examples on Scan command.

 

Regarding not being able to see updated measures, the example uses SetCtrlVal to write to the textbox: SetCtrlVal appends text to the control, leaving old text unchanged. It may thus happen that new text is out of visible boundaries of the control: try scrolling down or right to see the new text or modify the software to use ResetTextBox instead that replaces old content with the new text.



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 20 of 47
(2,457 Views)