LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Help with reading response from serial commands

Background:

I am trying to control a servo motor linear stage from Parker. The controller, ViX250iH requires serial communication. Using the conmmand reference found in the ViX250 user guide I have been able to successfull read/write to the controller using a terminal such as Tera Term and RealTerm. Attached shows a screen shot of me sending commands and getting response back.

 

Note: User guide says I each command should end in "carriage return".

 

Issue:

I am trying to build a VI that would allow me to send commands to the controller and read response back. Using the attached VI, I am able to write commands to the controller and get the motor to move. However I am unable to read the output from the terminal which is required for some of the commands. For example, when I send the command 1HOME or 1R(PA), I don't see the response the controller sent back; I only see the command I sent and not the reply back. I need to be able to read the response in order to determine position of the stage and determine if I should move the motor again or not...

 

What do I need to do to my VI in order to be able to read only the response back from the controller and not the command I sent? Also, how can I modify the VI so I can send multiple commands at once. According to the user guide, I need to seperate each command with "carriage return", but I am not sure how to do that in LabVIEW on the front UI.

 

Serial-comm-with-ViX-drive.png

Serial-read-write-labview-issue.png

0 Kudos
Message 1 of 6
(2,874 Views)

It looks like the instrument has an echo feature.  Just do another read and the second read should be your expected response.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 6
(2,826 Views)

This VI should do what you need.  I did some cleaning up (mostly removing stuff you did not need), added a loop and event structure so that you can keep sending commands, and added a VISA Read to read the echo before reading the response.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Download All
0 Kudos
Message 3 of 6
(2,813 Views)

Is the serial connection a two-wire RS-485 interface? In that case, the receiver on your serial port always receives an echo of what it sends. Your serial hardware (e.g. USB to RS-485) might have an echo cancellation feature that you can enable in its options, but if not, the software solution above should work.

0 Kudos
Message 4 of 6
(2,793 Views)

Thank you. Your suggestion worked. However, I am having additional issues.

 

  1. If I do a fixed amount of byte reads (ex: 1000) at the response, I get timeout error when I send commands that don't have a response back from the servo controller. For example, if I send the command to move the motor, 1G,  there is no reply back from the controller. The controller simply moves the motor. In this case, I get a timeout error on VISA Serial Read

  2. If I do read based on bytes at port, I get partial read response which is dependent on the amount of time I wait between sending the VISA Write command and VISA Read command. For example, when I send command 1HOME with 25ms wait, I get response *AR1 E- TP0 V+1. However, if I do a 100ms wait, I get the full response *AR1 E- TP0 V+1.000 A100.00 M3.
    Serial-read-write-issue-2.png

  3. How do I deal with a situation where I send a command such as 1STATUS, which has multiline response which may take 5-10s to complete as shown below:


    Serial-read-write-multiline-response.png
  4. How do I send multiple commands seperated by carriage return? Like what if the user inputs multiple commands in the front UI where each line is a command.
0 Kudos
Message 5 of 6
(2,777 Views)

@aab-ufl wrote:

Thank you. Your suggestion worked. However, I am having additional issues.

 

  1. If I do a fixed amount of byte reads (ex: 1000) at the response, I get timeout error when I send commands that don't have a response back from the servo controller. For example, if I send the command to move the motor, 1G,  there is no reply back from the controller. The controller simply moves the motor. In this case, I get a timeout error on VISA Serial Read

In this situation, then you don't do a VISA Read to look for a response since you already know you aren't going to get one.

 

  1. If I do read based on bytes at port, I get partial read response which is dependent on the amount of time I wait between sending the VISA Write command and VISA Read command. For example, when I send command 1HOME with 25ms wait, I get response *AR1 E- TP0 V+1. However, if I do a 100ms wait, I get the full response *AR1 E- TP0 V+1.000 A100.00 M3.
    Serial-read-write-issue-2.pngBytes at Port is the wrong thing to use about 99% of the time.  If you know your reponse comes back with a LF or CR character, just request a larger number of bytes than you ever expect to get.  That way if it takes 26 ms to get the response, you'll get it all.  You won't be short if you only waited 25 ms, and you won't be wasting time waiting around an additional 74 ms if you try to wait 100 ms.  What if there is some odd delay and it takes longer than 100 ms.  Why play guessing games with how long you need to wait to be sure you get full response?

  2. How do I deal with a situation where I send a command such as 1STATUS, which has multiline response which may take 5-10s to complete as shown below:


    Serial-read-write-multiline-response.png

 

Do you know how many lines you'll be getting?  Then do a VISA read in a For Loop that many times concatenating the responses?  If you don't know how many lines, then how do you know when you've received a full response?  You could incorporate a wait and a bytes at port response in that case.  After you read the first line, wait a small amount of time and check bytes at port.  If >0 do a VISA read, and repeat.  If =0, stop your loop and move on.

  1. How do I send multiple commands seperated by carriage return? Like what if the user inputs multiple commands in the front UI where each line is a command.

Just send them.  VISA Write doesn't care if it has additional LF's or CR's in there.  Make sure you have the option NOT turned on that automatically appends a termination character on outgoing messages.  It is off by default.  You should put your own line feed character at the end of any outgoing message whether a user does it as part of a user interface, or you do it programmatically.  I consider it bad form and unnecessary to rely on the VISA driver to add the termination character for you.  The only issue you might have is that if you send multiple commands at one time, do you expect multiple responses, one from each command?  If so, do you have a plan to handle that?


 

0 Kudos
Message 6 of 6
(2,773 Views)