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