03-23-2020 06:34 PM
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:
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:
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.
03-24-2020 09:47 AM - edited 03-24-2020 09:49 AM
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.
03-24-2020 11:38 AM
Thanks for your answer 🙂