I would imagine that the two independent loops each writing/reading from the same bus might be the cause of your issue. You could end up with something like the following:
string wr1 | | string rd1
| string wr |
Uncertain how the instrument might react
As stated in one of your other threads, you are dealing with raw/hex/binary data. Therefore you need to make sure the Termination Character is turned OFF. Otherwise, if there just happens to be a 0xA byte in your message, the read will end there and you will not have all of your message. Use the VISA Configure Serial Port. One of the inputs on the top is to enable the termination character. Set that to FALSE.
I tried setting the termination character to false in the visa configure serial port. But Still the return count from visa read changes. I am expecting a return count of 23 from the visa read. But I obtain a return count of 4 at times which changes to 23 after a while.
So is the 4chars response some sort of error? What does the 4 char response mean according to the instrument manual?
One solution is to put the read-write in a while loop, use a case statement to catch the 4 vs 23 char cases, repeat the write-read for 4 char and continue for 23 char cases.
It likely that you are doing the read too quickly after the write and a short delay (10ms? 100ms?) might prevent the 4char responses.
I am expecting my string(Hexadecimal string) read to be : 4E54 0013 052B 0D00 0001 60FD 0000 0000 0400 0003 0096 00 (23 bytes). But I get a return count of 19 from my visa read at times (randomly ) which changes my string read to :0003 0096 (4 bytes) causing a timeout in my visa read.
As Minions pointed out, you are hammering away at the instrument in two parallel loops. You might want to introduce a small delay between Write and Read. You might also want to capture when the read byte count isn't correct and do something. Here's a first take at just repeating the Write, delay, Read process when the read count and byte count are not equal.
I'd run this and monitor when the loop has to iterate more than once (read count) and track down the source of the problems.
The problem was using two parallel loops. Whenever the read file in both the loops tried to access the resource at the same time, it got timed out thus causing an error. I made a serial connection and it started to work well.