04-24-2014 02:29 AM
hi, i'm writing program for microcontroller that using serial port for reading and writing data.
after boot, program must to wait 10 seconds to receive data from serial port
code: init serial port by VISA
//init of ssp protocol viSetAttribute (ssp, VI_ATTR_TERMCHAR_EN, VI_FALSE); //disable termination char viSetAttribute (ssp, VI_ATTR_ASRL_END_IN, VI_ASRL_END_LAST_BIT); viSetAttribute (ssp, VI_ATTR_ASRL_BAUD, 38400); viSetAttribute (ssp, VI_ATTR_ASRL_PARITY, VI_ASRL_PAR_NONE); viSetAttribute (ssp, VI_ATTR_TMO_VALUE, 15000); //TMO 15 seconds viFlush (ssp, VI_ASRL_IN_BUF_DISCARD); Sleep(1);
reading from the port
if (!strcmp(command,"readBuffer")) //Reading TST
{
incount = 0;
otcount = 0;
viGetAttribute(ssp, VI_ATTR_ASRL_AVAIL_NUM, &num_bytes);
while(num_bytes <= 3)
{
viGetAttribute(ssp, VI_ATTR_ASRL_AVAIL_NUM, &num_bytes);
viRead (ssp, Databuf, num_bytes, &RTCount);
}
Databuf[RTCount] = '\0';
viFlush (ssp, VI_WRITE_BUF);
viFlush (ssp, VI_READ_BUF);
packetdata = strtok(Databuf,";");
if(!strcmp(Databuf, ";TST;") || !strcmp(Databuf, ";TST") || !strcmp(Databuf, "TST;") || !strcmp(Databuf, "TST"))
{
InsertTextBoxLine (panelHandle, PANEL_DEBUGWINDOW , ++debugBoxLine, Databuf);
//*************** Auto Scroll ***********************
SetCtrlAttribute (panelHandle, PANEL_DEBUGWINDOW, ATTR_FIRST_VISIBLE_LINE, debugBoxLine);
flagout = 1; nboards++;
return 1;
}
are this code optimal , can i change and remove while loop from the code?
and another issue when i'm writing to the port
code:
if(!strcmp(command,";ANALOG;")) { Sleep(1); incount = 0; otcount = 0; num_bytes =0; strcpy(command, ";IN?;"); viWrite(ssp, command, 10, &RTCount); //10 bytes viGetAttribute(ssp, VI_ATTR_ASRL_AVAIL_NUM, &num_bytes); while(num_bytes < 11) { if(num_bytes < 5) { viWrite(ssp, command, 10, &RTCount); } viGetAttribute(ssp, VI_ATTR_ASRL_AVAIL_NUM, &num_bytes); viRead (ssp, Databuf, num_bytes, &RTCount); } viFlush (ssp, VI_WRITE_BUF); viFlush (ssp, VI_READ_BUF); Databuf[RTCount] = '\0'; packetdata = strtok(Databuf,";");
when program sending command ;IN?; it's gets some data. but sometimes buffer is empty. and i added while loop for checking num_bytes for received data.
may be i can remove this while loops and use some VISA attributes? for waiting received data?
thanks
04-29-2014 03:06 PM
Hi Arbo,
I’m not aware of any specific attribute to handle that particular functionality. Typically you would poll for the data as you are doing, or perhaps have some event driven technique. However, I find the NI-VISA Programmer Reference Manual a great resource for looking through specific functions and attributes. Also check out the Community Examples and the examples that are included with the driver.
Perhaps someone else on here might have some more insight into optimization techniques that they have found successful.
Here are some additional resources that you may find useful:
04-30-2014 12:16 AM
David
thanks very much, i will check the examples and manual
04-30-2014 12:18 PM
viQuery(ssp, ";IN?;", "%s", Databuf);
hi i found this command for sending formatted data to serial port and receiving formatted data to Databuf variable.
but now i get an error Timeout expired before operation completed.
and when i making debuging by F8 button i see the right data received in Databuf
using this command attribute:
viSetAttribute (ssp, VI_ATTR_TMO_VALUE, 1000);
not helping me.
04-30-2014 12:55 PM
04-30-2014 02:01 PM
when i using viWrite and viRead functions
everything is fine and working good.
the reason that i searching for another commands is issue that viRead sometimes not receiving the answer from microcontroller and i dont want to use loops such was in the start of this post