LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Using VISA to read from COM port with delay

hi, i read a couple of posts here with a same topic, but my problem not solved.  i'm must to read opcode like this one (";TST;") from chip after his booting, it takes 10-15 seconds.

i wrote this code:

 

//15 Seconds
        for(i=0;i<1500;i++)
        {
            viGetAttribute(ssp, VI_ATTR_ASRL_AVAIL_NUM, &num_bytes);  
            viRead (ssp, Databuf, num_bytes, &RTCount);
    
            if(strlen(Databuf)>5)
            {
             
             string = strtok(Databuf, ";")

             if(strcmp(string, "TST")

                     retrurn 1; 
                
             }
            Sleep(10);
        }


it works but sometimes it is not reading buffer in time or gets ("") in Databuf, and VISA gets out with error -1705something.

 

are VISA have attrinbutes to wait until all buffer full with data or start buffering only then it get string (";TST;")

?

0 Kudos
Message 1 of 4
(4,086 Views)

I'm not sure what exactly you're asking. Could you clarify your question? Are you asking about whether or not the VISA Read is completing before your program reaches the "if" statement (or if there's a way to check that)?

 

If you're just looking for information about different VISA attributes, I've linked a document below lists and gives descriptions of VISA attributes (along with a lot of other information about creating a VISA program in C).

 

NI-VISA Programmer Reference Manual

http://www.ni.com/pdf/manuals/370132c.pdf

 

Let me know if that document helps!

 

Regards,

Ryan K.
Product Manager, ATCA and BEEcube
National Instruments
0 Kudos
Message 2 of 4
(3,993 Views)

thanks for an answer i will check this doc.

 

ok i will describe a problem more specific.

 

after booting a chip. it has delay about 10-15 seconds and return string  ";TST;" thru the com port.

 

what is a best way to catch this string using NI-VISA commands. viRead, viScanf, viReadBuf?

 

my code sometimes catch this string and some times return nothing in buffer. 

0 Kudos
Message 3 of 4
(3,991 Views)

You should read only after having all bytes available in the buffer, otherwise you may be reading only part of the message in one loop and overwriting it with the rest of the message on the second read. Additionally, the strtok is probably unnecessary since you may be testing the complete message without need to split it in tokens.

 

You should rewrite your code along this line:

 

for (i = 0; i < 1500; i++) {
   viGetAttribute(ssp, VI_ATTR_ASRL_AVAIL_NUM, &num_bytes);
   if (numBytes >= 5) {      // Wait for a complete message to be at port
      viRead (ssp, Databuf, num_bytes, &RTCount);
      if (!strcmp (Databuf, ":TST:"))
         return 1;    // Message OK
      else
         return 0;   // Wrong message
      Sleep(10);
}

 

 



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?
0 Kudos
Message 4 of 4
(3,983 Views)