12-23-2009 10:40 AM
Hello,
I am writing a simple code that checks COM ports and communication with the connected instrument with RS232 at the launch of the application. The code sends a write command to check the status then reads from the instrument. The (VISA) write and read are included in a while loop that iterates every 100 ms (is this too fast?).
At first, i did not include a delay between read and write and after running serveral tests and changing some code (i put the visa open and close inside the loop) the computer crashed and restarted spontanously. I checked the the Basic serial example by NI, and noted a delay between write and read of 500 ms by default.
Questions: how important is to include a delay and what's the recommended delay
Now about the code itself: the idea of including the Visa open and close inside the loop is to open a new connection in case of error, example if the cable was unplugged. But is it the proper way to do it?
Thank you
Sam
12-23-2009 10:56 AM
There is no need for a fixed delay at all if you write the code correctly or if the instrument sends a termination character. You would simply use a loop with the VISA Bytes at Serial Port in it. Exit the loop when the bytes available is greater than 0. In a following loop, another VISA Bytes at Serial Port is used for the VISA Read and this loop would exit when the number of bytes is 0. Add a some timeout function to the first loop so the loop will exit if no response. An instrument that sends a termination character will terminate the read when the character is detected. So, you can simply set a large number of bytes to read and no delay between the Write and the Read.
Nor is the absolutely any need to open and close a VISA resource inside a loop. Whether or not a cable is connected or disconnected is irrelevant. This is something you can easily see for yourself. Disconnect the instrument and call VISA Open. You will not get an error. The VISA resource you are connecting to is the com port, not the instrument.
12-23-2009 12:03 PM - edited 12-23-2009 12:05 PM
Is a 100mS loop iteration too fast? Likely not, but it depends on your baud rate and the size of the message.