LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Writing multiple commands with serial communication

Solved!
Go to solution
Highlighted

Hi,

 

I'm relatively familiar with the basics of LabVIEW, but I am new to serial communication with VISA. I have had no luck finding a solution to my problem, but this one is similar: http://forums.ni.com/t5/LabVIEW/VISA-read-write-to-serial-port-Unable-to-impelement-multiple/td-p/93....

 

I am able to send a command and read the response with the device, but I cannot send multiple commands to change settings of the device. For example, I send 'F' to the device to read the current operating frequency, and read the response (22). But if I send 'f' to the device to change the operating frequency, read the response ("Send desired frequency..."), then send my desired frequency (120), I get no response.

 

The default frequency upon opening the port to the device is 22. I would like to change to frequency to 120, then read the current operating frequency to make sure it was properly changed.

 

I am confused on the data format to send within LabVIEW. In MatLab, it works out fine if I send 'f' as a character (fwrite), followed by the desired frequency as an integer. However, in LabVIEW, it seems the number I want to send is not being read properly. I have tried typecasting the integer to a string and reading it as slash code, hex code, normal code, everything I could think of.

 

Please help 🙂

Download All
0 Kudos
Message 1 of 11
(5,931 Views)
Highlighted

First, have you tried to see if there are drivers available for your device? http://www.ni.com/downloads/instrument-drivers/

 

Next, type cast doesn't work the way you think it does. Use Format Into String to do what you want to do.

create frequency string.png

 

Next, the device may take some time to send a response. There may not be any bytes at the port to read. Add a wait after you send your command or specify the number of bytes to read. This example might help: C:\Program Files (x86)\National Instruments\LabVIEW 2015\examples\Instrument IO\Serial\Continuous Serial Write and Read.vi

 

Finally, try using a terminal emulator like PuTTY to send the commands and see the responses.

Message 2 of 11
(5,907 Views)

Do you need to send a termination character (line feed or carriage return)? LabVIEW does not do that automatically. You need to explicilty append the termination character to the string you send.

 

Also, you should almost never need to use Bytes at Port when receiving data with a termination character. Just set bytes to a value larger than the longest expected message. VISA Read will wait until the termination character is received and tehn return the message.

 

Lynn

Message 3 of 11
(5,901 Views)
Highlighted

Yes, I've installed all of the necessary drivers for the device. I opened the port using RealTerm, which has the option to send commands as an ascii or as a number. This works fine when I send the characters as asciis and the input frequency as a number. However in Labview, because the input for VISA write takes only strings, I am unable to send the second command as a number. I've used your 'Format into String' suggestion, but the device still does not read the command properly.

0 Kudos
Message 4 of 11
(5,883 Views)
Highlighted

If you installed the drivers, try looking under the following palette for your device: Instrument I/O >> Instr Drivers

 

The format into string example will output "120". It includes the quotes.

 

Can you copy the commands/responses from RealTerm here? I'm not sure how your device wants the command formatted.

Message 5 of 11
(5,877 Views)
Highlighted

So, what is the device?  Can you link the manual?  if "f" is replied with a "Send Desired Ferquency.." the instrument is surely not SCPI compliant so, we need to read the manual to help you format the expected commands and parse the expected responses.

"Should be" isn't "Is" -Jay
0 Kudos
Message 6 of 11
(5,866 Views)
Highlighted

Sure, here are some screenshots using RealTerm, as well as the command format described in the demo kit user manual.

 

Download All
0 Kudos
Message 7 of 11
(5,858 Views)
Highlighted

Snippets from the user manual...

Download All
0 Kudos
Message 8 of 11
(5,855 Views)
Highlighted

From what I can tell, your program is almost correct.  Your issue is that the instrument takes time to send a response.  But you were just reading the number of bytes at the port almost immediately after you send the command.  So the actual response likely has not even started yet.  Luckily, the solution is really simple.

 

It looks like the instrument sends a CR LF at the end of every response.  So leave the termination character on and use the default (LF, 0xA).  Now just use a "large" constant for the number of bytes to read.  The VISA Reads will stop a read when the termination character is found.  With the 10 second timeout, this will give plenty of time for the response to come in and you get all of the response.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
Message 9 of 11
(5,833 Views)
Highlighted
Solution
Accepted by Bananafanafofana

Give this a try. Press the "Prepare" button followed by the "Set Freq" button. You'll need to configure your serial port appropriately first though.

Message 10 of 11
(5,764 Views)