Tutorial: Sending Basic Xtralien Commands in LabVIEW
In this tutorial, we will build a simple virtual instrument (VI) that allows us to send commands to the X100 board and receive information from it in return. The image below shows what the code will look like by the end of the tutorial. Both USB and Ethernet can be used to communicate with the board and the commands are the same for each. For the purposes of this tutorial however, we will be only using the USB connection.
The code will involve four processes: START, WRITE, READ and STOP. These are all separated in to their own sub-VIs to keep the code neat and tidy. Splitting code in to smaller VIs is good practice in LabVIEW, as it will allow people to read the code at a glance and keep large programs clear. A connection to the Xtralien is initialised in the START VI. We then WRITE a command to the board in the form of a string (a list of useful commands is available here). Some of the commands we send such as ‘cloi hello’ will prompt the Xtralien to send us a response string. These are READ and then we STOP the connection with the board.
This simple virtual instrument (VI) initialises the serial port connection Xtralien. The VISA serial VI can be accessed via the VISA menu in a LabVIEW block diagram. Timeout, VISA name and error in are all inputs and can be wired up in the main VI shown above. We then output the VISA name and error to allow them to be used by the next program in the flow.
The string we typed is sent to the Xtralien board by this VI. As before, the VISA name and error are both inputs and outputs.
The READ VI is slightly bigger than the previous two programs. The length of the returned string will vary depending on the command we send to the board and some commands such as a very high accuracy 'sweep' can take several seconds to complete. Because of this, we cannot simply send one read command to the board and be sure to receive all the data that we should. Instead, the READ VI continuously tries to read in a string from the Xtralien board until there is nothing left to read. The VISA read tool will output an error if there is no string to read. We can use this error output to continuously test for the presence of a string. Once a the board has completed its task and sent its string, the errors will stop and the VI will start reading. We continue to read in the data until we receive another error, indicating that the Xtralien board has finished sending us data.
Stage 1 - Before Reading
When the VISA read function returns an error, the error is cleaned ready for the next loop, as shown below.
Stage 2 - During Reading
As soon as the Xtralien board begins to send a string, the VISA read function will stop returning an error and the string is stored in a shift register. For a large string, the program will not read the entirety of it during a single loop. Every while loop iteration will therefore read a new section of the string until the program has read the whole thing. These sections are concatenated to rebuild the original string sent by the Xtralien board.
Once the whole string has been read, the buffer will be empty and the VISA read function will return an error again. This returns the program to Stage 1. Using the 'Implies' and 'NOT' functions, we can create a system that will stop the program at this point. The 'Implies' function return False when the top wire is True and the bottom wire is False, with all other cases will returning False. We can use this and a shift register to detect when the VISA read returns True during one loop and False during the next, indicating that it has finished reading the string. We then use a 'NOT' function so that the program will only stop with this condition instead of stopping with any other condition.
Once the while loop has been stopped, the output string is sent back to the main program.
Once the program has read in a string, we want to end communication with the Xtralien board. We use the VISA close function to do this in a simple way as shown below. If an error occurred during the main program, this is displayed in a pop-up dialogue box before the program shuts down. We also flush the board's buffer at this point.
Combining these steps we can create the block diagram shown at the beginning of this tutorial. We should end up with two front panel inputs as shown below.
In this example, we send a 'cloi hello' command and receive a 'Hello World' string.