LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

rs232 send a string and fetch the data

Hi how to send the messages to the serial port devices..?

   Hi i have connected the rs232 cable with a device and my pc. I use the openconfig to open the comport and then i try to read from the comport. Now my problem is i wanted to get the data's from the serial port device based on my request. I have set a string which i can use it to fetch the data's from the device. How can i send that string and query the data..?
   For example,
               Rs_Status
   I wanted to send this string and then fetch the Rs_Status infromation from the device.
 
#include <utility.h>
#include <rs232.h>
#include <cvirte.h>  
#include <userint.h>
#include "comrdterm.h"
static int numbytesread;
static char inbuffer[100];
static int panelHandle;
int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1; /* out of memory */
 if ((panelHandle = LoadPanel (0, "comrdterm.uir", PANEL)) < 0)
  return -1;
 DisplayPanel (panelHandle);
 OpenComConfig (3, "dev1", 9600, 0, 7, 1, 512, 512);
 RunUserInterface ();
 DiscardPanel (panelHandle);
 return 0;
}
int CVICALLBACK Quit (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
  {
  case EVENT_COMMIT:
   CloseCom (1);
   CloseCom (2);
   QuitUserInterface (0);
   break;
  }
 return 0;
}
int CVICALLBACK Query (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
  {
  case EVENT_COMMIT:
   numbytesread = ComRdTerm (2, inbuffer, 100, 235);
   Delay (.5);
   FlushInQ (2);
   SetCtrlVal (panelHandle, PANEL_TEXTBOX, inbuffer);
   break;
  }
 return 0;
}
0 Kudos
Message 1 of 2
(3,507 Views)
Hi Monky, writing to the serial port is easy: ComWrt (port, buffer, nunmchars);
 
Some detail more on your question: as far as I understand, your device acts under direct polling (i.e. you "call" it and it answers). In this respect the procedure is quite simple:
 
ComWrt: send the query to the device
Delay:      Wait for the appropriate time for the device to respond
GetInQLen: test if the correct number of characters has been received
ComRd:  read answer
 
The waiting/reading part of the process can be simplified setting an appropriate timeout for reading (SetComTime😞 in this case you will need to test if ComRd has terminated for timeout or because characters have been received.
 
Last suggestion: when configuring the serial port set output buffer lenght to a negative value: this excludes the use of the output buffer during transmission, this way preventing Windows from taking too much time in outputting charactersSmiley Wink.


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 2
(3,493 Views)