LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Don't get all information from usb port

Hello I have problem by getting information from port.

 

I know that I allways sending information. But just get information 1 time.

 

I can see that information seending by using RealTerm program:

 

Fiksavimas.PNG

 

Information being sent every 4 seconds.

 

This is the code:

 

#include <utility.h>
#include <rs232.h>
#include <cvirte.h>
#include <ansi_c.h>
#include <userint.h>
#include "CVI programa.h"

int portNumber=3;

static int	notifyCount = 5; /* Wait for at least 50 bytes in queue. */
static int eventChar = 10; /* Wait for LF. */
static int eventMask = LWRS_RECEIVE;


static int panelHandle;




void ComCallback(int portNumber, int eventMask, void *callbackdata)
{
	char mas[200];
	int read;
	float Itampa;

if (eventMask & LWRS_RECEIVE)
{

read = ComRd (3, mas, 21);
mas[21] = 0;
InsertTextBoxLine(panelHandle,PANEL_TEXTBOX,-1,mas);
Itampa = atof(&mas[16]);
PlotStripChartPoint (panelHandle, PANEL_STRIPCHART, (double) Itampa);
SetCtrlVal(panelHandle,PANEL_NUMERIC,Itampa);
}

}


int main (int argc, char *argv[])
{
	if (InitCVIRTE (0, argv, 0) == 0)
		return -1;	/* out of memory */
	if ((panelHandle = LoadPanel (0, "CVI programa.uir", PANEL)) < 0)
		return -1;
	
	OpenComConfig (portNumber, "COM3", 115200, 0, 8, 1, 8, 8);
	SetComTime (portNumber, 1);
	
	InstallComCallback (portNumber, eventMask, notifyCount, eventChar, ComCallback, NULL);
	
	DisplayPanel (panelHandle);
	RunUserInterface ();
	DiscardPanel (panelHandle);
	return 0;
}

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

 

 

This what I get:

Fiksavimas.PNG

 

This is from other program (maybe it's helpful):

 

Variable:
char temp[30];

//This is the function who make string array
sprintf((char*)temp,"Temperatura yra %5.2f",J_termoporos_temperatura);

//This is the function who sending information by usb port
CDC_Transmit_FS(temp, sizeof(temp));

P.S. program is Keil

 

 

Can somebody help me, I new to this program.

Thanks for help.

 

 

0 Kudos
Message 1 of 3
(2,113 Views)

I can see a few problems in your code.

 

OpenComConfig is set with a buffer of 8 characters, but you are reading 21 from the queue (you're lucky that CVI always sets 30 bytes if less than 30 are set!). You can state a greater buffer even if it is not to be used.

 

It is better to disable the output queue when opening the port setting -1 as output queue length: see the documentation for OpenComConfig.

 

If you want to use LWRS_RECEIVE the event character parameter is useless.

 

You set 5 as notifyCount when you want to read 21 bytes. This way the callback will be fired before the complete message is in the queue.

 

When inside the com callback it is better to always check that you have the correct number of bytes to read with GetInQLen () function, and/or test after ComRd that you have read the correct number of bytes.

 

Who is sending all those extra characters you see in RealTerm display? These characters mislead ComRd since are counted but are not part of expected string. If you cannot avoid them, you should check that the received string matches the expected format before proceeding interpreting it.

 

It is always better to add some error checking in critical functions: in your case I would test the return value from all functions that handle the serial port.

 

If you can modify the code of the sending device it would be better to use a terminator character and use LWRS_RXFLAG event mask instead.

 



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

Thanks for your answer 🙂

0 Kudos
Message 3 of 3
(2,048 Views)