LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

visa serial read/write at same time problem

Hallo!

I struggle with a fundamental problem...visa serial read/write.

How do I make a program that both sends data and recieves data via visa serial read/write "at the same time"?
I want it to read data whenever there is incoming data(all the time ready to read) and write whenever
there is something to write(for example a "send command" buttom that user pushes).

I have tested to program in some different ways but I get the same problem(tested program between two PC:s via
hyperterminal)

PROBLEM:
Reading data works fine, it answers on incoming data, but to write data the program´s readfunction
must execute first. The readfunction must take its specified number of bytes before the program
sends the "writebytes". In other words: I cant write anything to hardware unless there is incoming data to read!!

I have tested with one loop containing both read and write. I have tested with two parallell separated
loops with read in one and write in the other.

You can take a look at my attached programexample if you want.

Any suggestions how to to solve this?

Thanks fred!
0 Kudos
Message 1 of 5
(3,583 Views)
Instead of reading one byte from the port on each loop iteration, use the VISA Bytes At Serial Port function to see if there are any bytes to read. If the function returns 0, don't read. If the function returns >0, use this number for the number of bytes to read. In other words, your read function should be in a case structure so it does not have to read on every loop iteration, it only reads when there is something there to read. This allows you to write at any time, and to read whenever something is there to read.
- tbob

Inventor of the WORM Global
0 Kudos
Message 2 of 5
(3,580 Views)
thanks for answer!

I wonder if entering this case in each loop iteration whenever data comes can slow things down?

/Fred
0 Kudos
Message 3 of 5
(3,558 Views)
Unless your app is very time critical, you should not be concerned. If there are no Bytes At Port, the case would be False, and there should be no code in your False case. The check for Bytes At Port is very quick, and is very necessary.
- tbob

Inventor of the WORM Global
0 Kudos
Message 4 of 5
(3,550 Views)
fred wrote:
> thanks for answer!<br><br>I wonder if entering this case in each loop iteration whenever data comes can slow things down?<br><br>/Fred

Don't worry about such things. The LabVIEW compiler will indeed create
code to test the bytes value and only execute the VISA Read if it is not
0 but the necessary time for this code to execute is in modern systems
in the range of nanoseconds. If your application is so time critical,
you have quite a lot of other problems to tackle first before even
thinking about this extra overhead.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 5 of 5
(3,477 Views)