LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Bytes available on serial port

I have a threaded application that reads the reads the number of available bytes on a serial port using the VISA API. I would simply like to read the serial port if the number of available bytes is greater then zero. When I use the viGetAttribute() function, it always returns zero. Here is my function below.

    int num_bytes;
     
    while((num_bytes = viGetAttribute(instr, VI_ATTR_ASRL_AVAIL_NUM, &num_bytes)) > 0)
    {
        ErrorPrintf("Num bytes available = %d\n", num_bytes);
       status = viRead (instr, buffer, 100, &retCount);
       if (status < VI_SUCCESS)
       {
          printf ("Error reading a response from the device.\n");
       }
       else
       {
          printf ("\nData read: %*s\n", retCount, buffer);
       }
    }   
     
    return 0;


Am I calling this function incorrectly? I can send data over the port and verify it's being sent using a port monitor.


0 Kudos
Message 1 of 6
(4,891 Views)

I don't have my CVI system available at the moment, so I can't confirm this but at a guess I would expect the viGetAttribute() call to return a status word, not the number of bytes. So perhaps instead of :

    num_bytes = viGetAttribute (instr, VI_ATTR_ASRL_AVAIL_NUM, &num_bytes);

you need something more along the lines of :

    status = viGetAttribute(instr, VI_ATTR_ASRL_AVAIL_NUM, &num_bytes);

This would avoid overwriting num_bytes.

JR

Message 2 of 6
(4,885 Views)
Yes, you are correct. I did notice my typo and corrected it and replaced the zero with VI_SUCCESS.

(status = viGetAttribute(instr, VI_ATTR_ASRL_AVAIL_NUM, &num_bytes)) >= VI_SUCCESS

I now need to fix the problem with stopping my thread correctly.
0 Kudos
Message 3 of 6
(4,858 Views)

 

Hallo map and jr_2005,

 

VI_ATTR_ASRL_AVAIL_NUM flag can be used in order to determine the amount of data in the input serial asynch. COM buffer, but what about determining number of bytes in output buffer ? Is there any VISA function which query number of bytes in output buffer ?

 

BR,

Milan.

0 Kudos
Message 4 of 6
(4,466 Views)

Milan,

 

With a question like this, the best approach would be to make a new thread for your new question, which I can see that you have done on this duplicate post.  Once you have done that, there is no need to post on 9 other threads to ask the same question.  Please see the duplicate post for the answer that SFK has given.

 

Thanks,

 

Eric K

0 Kudos
Message 5 of 6
(4,429 Views)

Sorry again Erick,

problem has been fixed.

 

Best regards,

Milan.

 

0 Kudos
Message 6 of 6
(4,419 Views)