« Close

Datasheets and User Guides

App Notes

Software & Driver

 

2.1 - Opening and Closing

Summary

Open a LabJack device to communicate with it. Close it to allow other processes to open it.

Opening

Either LJM_Open or LJM_OpenS must be used to open a LabJack. The LJM_Open and LJM_OpenS functions will detect a LabJack connected to the computer, and create a device handle. The device handle is then passed as an input to other functions.

  • LJM_Open - Open a device based on integer filter parameters.
  • LJM_OpenS - Open a device based on string filter parameters.

Use whichever is more convenient for you.

USB connections are preferred - If ConnectionType is equal to LJM_ctANY (0), LJM_Open will attempt to open a USB connection before trying to open a TCP connection.

Device connections are claimed - Once a LabJack device is opened on a given connection type, other processes will generally not be able to open that same device using the same connection type until the device connection is closed by using LJM_Close or LJM_CloseAll.

Subsequent calls may return the same handle - Once a device is opened, subsequent calls to LJM_Open/LJM_OpenS will return the handle to that device if the DeviceType, ConnectionType, and Identifier parameters describe that device. In this case, calling LJM_Open or LJM_OpenS is nearly instant because no device communication occurs. For example, if a T7 is opened via USB with serial number 470010729, returning handle x, the following calls would return handle x without opening a new device connection:

  • LJM_Open(LJM_dtANY, LJM_ctANY, LJM_idANY, ...)
  • LJM_Open(LJM_dtT7, LJM_ctUSB, "470010729", ...)
While the following would open a new device connection:
  • LJM_Open(LJM_dtANY, LJM_ctANY, "470010999", ...) // Different serial number
  • LJM_Open(LJM_dtANY, LJM_ctETHERNET, "470010729", ...) // Different connection type
Devices like the T7 may have multiple device connections at once. The following calls would each return a different device handle:
  • LJM_Open(LJM_dtT7, LJM_ctUSB, "470010729", ...)
  • LJM_Open(LJM_dtT7, LJM_ctETHERNET, "470010729", ...)
  • LJM_Open(LJM_dtT7, LJM_ctWIFI, "470010729", ...)

Reconnection is automatic - If LJM detects a broken connection, it will automatically attempt to reconnect. If the reconnect is unsuccessful, LJM will return the errorcode LJME_RECONNECT_FAILED. When this happens, many applications can simply retry calling the function that returned LJME_RECONNECT_FAILED with the same parameters. For more details, see the reconnection page.

Using an IP Address as the Identifier parameter is efficient - In order to open a TCP device with a specific serial number or name, LJM will communicate with all LabJack devices on the network, asking each for their serial number or name. In contrast, opening a TCP LabJack device by IP address will only send packets to that IP address, which is faster and more efficient. Setting up static IP addresses can be troublesome, so use what is appropriate for you application.

Specific IPs will be checked - Specific IP addresses may be defined, which LJM will try to open during every Ethernet- or WiFi-based Open or ListAll call.

Closing

Closing a handle will remove the associated device connection from the LJM library. Closing will both free the device to be opened again, and free allocated system resources.

  • LJM_Close - Close a specific device connection, based on the handle number.
  • LJM_CloseAll - Close any/all open device connections.
Devices are closed automatically when LJM is unloaded - When the LJM library (.dll, .dylib, or .so) is unloaded, it will automatically close all devices.