Help reading data | LabJack
 

Help reading data

4 posts / 0 new
Last post
a_keith
a_keith's picture
Help reading data

Hi there, I purchased a U3-LV for use with an analog accelerometer on my OSX Matlab 2014b installation. I installed exodriver and libusb, and obtained labJack.m for use in Matlab (as found at https://github.com/iandol/opticka/blob/master/communication/labJack.m), as well as the Matlab examples (found on this website).

I am able to successfully set up my device by connecting it and running these commands:

hdr = '/usr/local/include/labjackusb.h';
loadlibrary('/usr/local/lib/liblabjackusb',hdr);
lj = labJack('verbose',true);
lj.toggleFIO(6);

This generates reassuring messages that the device is connected and that toggling is being done properly. I am stuck on the last step. How do I poll the device for its current value? I know there is a function rawRead that calls LJUSB_ReadTO, but it requires an undocumented "bytein" parameter, as well as a "count" parameter (number of bytes to be read). When I guess on this parameterization, Matlab siezes up until I physically disconnect the U3.

 Can anyone offer any help? Once I get polling working, I plan to insert it into a loop seeking new values at ~1000Hz in order to "stream" data from the device.

LabJack Support
labjack support's picture
It doesn't look like the

It doesn't look like the labJack.m class has a method for reading digital I/O, so that will need to be implemented. We do not provide MATLAB examples for Mac OS X (the examples on our site are for Windows only and its high-level UD driver), but take a look at the C/C++ examples for helpful code that uses the Exodriver to base your MATLAB code off of. The labJack.m class provides some helpful MATLAB code. Keep in mind that labJack.m is third party code that we do not maintain.

The Exodriver header file (/usr/local/include/labjackusb.h) documents LJUSB_ReadTO. bytein is a byte array and count is how many bytes to read. To perform an operation on the U3, you send a command to tell it what to do (LJUSB_WriteTO) and then read its response (LJUSB_ReadTO). A LJUSB_ReadTo call only will timeout since there is nothing to read, and the labJack.m class looks like it sets the timeout to infinity. This is the likely cause of MATLAB "seizing up" since the LJUSB_ReadTO call is waiting for a response that it will not get and will not timeout.

Command/response packets are documented here and our C/C++ examples demonstrate how to use them:

https://labjack.com/support/datasheets/u3/low-level-function-reference

For reading digital I/O or analog inputs you will be using the Feedback command/response:

https://labjack.com/support/datasheets/u3/low-level-function-reference/f...

1000Hz is pushing the limits of command/response mode (look at the Command/Response section of the U3 datasheet for command/response times), so you may want to look into using stream mode which our u3Stream.c example demonstrates:

https://github.com/labjack/exodriver/blob/master/examples/U3/u3Stream.c

a_keith
a_keith's picture
Thanks for your reply. There

Thanks for your reply. There are many layers of documentation, and it is hard to tell where to focus. But my goal is very simple, which is to read analog values from several ports.

Let's walk through an example to see if I am getting this right. My goal is to set ports FIO2-6 to analog and then read their voltage value. First, I initialize:
hdr = '/usr/local/include/labjackusb.h';
loadlibrary('/usr/local/lib/liblabjackusb',hdr);
lj = labJack('verbose',true);

Because I want FIO2-6, my byte is 01111100, i.e., decimal value 124. This is only eight bits or one byte, so byte length is 1. We'll set a time out of 1 second.
byte = 124; % or maybe [0 1 1 1 1 1 0 0]?
byte_length = 1;
timeOut = 1;
combo_byte = ???

So value 124 is my destination address. How do I combine that destination with the byte from LJ_ioPUT_ANALOG_ENABLE_PORT, which is 2016 to produce a sensible combo_byte instruction?

I will call the exodriver with the following command:
error = calllib('liblabjackusb', 'LJUSB_WriteTO', lj.handle, combo_byte1, byte_length, timeOut);

(this runs, but produces an error flag of 1). Next I will need to issue a command to request a poll on the voltage from those ports. This will probably involve the constant LJ_ioGET_AIN_DIFF, but again I'm not sure how to combine that constant with my destination byte.

combo_byte2 = ???
error = calllib('liblabjackusb', 'LJUSB_WriteTO', lj.handle, combo_byte2, byte_length, timeOut);

Finally, I will prepare to read the result. My destination port is the same. I could adequately describe the output of the accelerometer voltage on each port with one byte. I have five ports, so I'm expecting five bytes.

receive_byte = [ 0 0 0 0 0];
count = 5; % that's five bytes

in =  calllib('liblabjackusb', 'LJUSB_ReadTO', lj.handle, receive_byte, count, obj.timeOut);

Am I on the right track?

LabJack Support
labjack support's picture
Initially in your code you

Initially in your code you will need to set FIO2-6 as analog inputs. Use the ConfigIO command/response packets to do that:

https://labjack.com/support/datasheets/u3/low-level-function-reference/c...

Then use a Feedback command/response to read the analog inputs (use IOType 1).

https://labjack.com/support/datasheets/u3/low-level-function-reference/f...

Note that for every command you need to read back the response. LJUSB_WriteTO and LJUSB_ReadTO look like this in the header file and the parameters are documented there.

unsigned long LJUSB_WriteTO(HANDLE hDevice, const BYTE *pBuff, unsigned long count, unsigned int timeout);
unsigned long LJUSB_ReadTO(HANDLE hDevice, BYTE *pBuff, unsigned long count, unsigned int timeout);

The pBuff (your combo_byte) is the byte array for the command or response packet, and the count (your byte_length) is the amount of bytes you want to send or receive. The pBuff array should be at least count in size.

Base your code on the u3allio.c example, u3.c (u3allio uses some functions from here) and the labJack.m code. In particular u3allio demonstrates sending a ConfigIO command/response (configAllIO function) and using the Feedback command/response (allIO function) to read analog inputs. Many of the labJack.m methods (such as setDIO) code use the Feedback command/response, and you can use that to help build your command and read the response in MATLAB.

Also, keep in mind that the UD driver and its MATLAB examples do not apply to the Exodriver. The UD provides a high-level interface that does the command/response packets for you, and the Exodriver is a low-level USB driver where you need to build these command/reponse packets. Constants/values like LJ_ioPUT_ANALOG_ENABLE_PORT and LJ_ioGET_AIN_DIFF will not apply.

If you are not restricted to a particular programming language, you may want to consider looking at using Python with LabJackPython which provides a higher-level interface for Mac OS X that handles the command/response packets.

Last, if you get stuck further please show the command bytes you are sending, and your response bytes you are reading and trying to parse information out of.