Open a LabJack device to communicate with it. Close it to allow other processes to open it.
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.
Use whichever is more convenient for you.
Use the Identifier parameter to open a specific device
The Identifier parameter of LJM_Open and LJM_OpenS optionally selects which device to open by serial number, IP address, or customizable device name. See Identifier Parameter for information.
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:
LJM_Open(LJM_dtANY, LJM_ctANY, LJM_idANY, ...)
// If this returns a handle to a T7, with serial number 470010729, connected via USB...
LJM_Open(LJM_dtT7, LJM_ctUSB, "470010729", ...)
// ...then this would return the same handle, without opening a new device connection
// ...and these would attempt to open a new device connection and return a different handle:
LJM_Open(LJM_dtANY, LJM_ctANY, "470010999", ...) // Different serial number
LJM_Open(LJM_dtANY, LJM_ctETHERNET, "470010729", ...) // Different connection type
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 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.
If a device isn't properly being found, some important debugging information can be found in our Device Not Found? App Note. If a network connection needs to be debugged, read through our Setup WiFi and Ethernet App Note as well.
Opens a desired LabJack and associates a device handle number (connection ID). The device handle may then be passed as an input to other functions.
LJM_ERROR_RETURN LJM_Open(
int DeviceType,
int ConnectionType,
const char * Identifier,
int * Handle)
LJM errorcode or 0 for no error.
LJM_Open and LJM_OpenS are essentially the same, except that LJM_OpenS uses string parameters for DeviceType and ConnectionType rather than integer parameters.
See LJM_GetHandleInfo to retrieve information about a handle.
See LJM_ListAll or LJM_ListAllS to find multiple devices.
See the notes in Opening and Closing.
When the ConnectionType parameter of this function is network-based, this function will check the IP addresses listed in LJM_SPECIAL_ADDRESSES_FILE.
[C/C++] Opening the first found T7, using LJM_Open
int LJMError; int handle; LJMError = LJM_Open(7, 0, "ANY", &handle);
Opens a desired LabJack and associates a device handle number (connection ID). The device handle may then be passed as an input to other functions.
LJM_ERROR_RETURN LJM_OpenS(
const char * DeviceType,
const char * ConnectionType,
const char * Identifier,
int * Handle)
LJM errorcode or 0 for no error.
LJM_OpenS and LJM_Open are essentially the same, except that LJM_Open uses integer parameters for DeviceType and ConnectionType rather than string parameters.
See LJM_GetHandleInfo to retrieve information about a handle.
See LJM_ListAll or LJM_ListAllS to find multiple devices.
See the notes in Opening and Closing.
When the ConnectionType parameter of this function is network-based, this function will check the IP addresses listed in LJM_SPECIAL_ADDRESSES_FILE.
[C#] Opening a T7 via TCP using LJM_OpenS
int LJMError; int handle; LJMError = LabJack.LJM.OpenS("T7", "Ethernet", "ANY", ref handle);
[C/C++] Opening a T7 via TCP using LJM_OpenS
int LJMError; int handle; LJMError = LJM_OpenS("T7", "TCP", "ANY", &handle);
This page describes the Identifier parameter that is used in LJM_Open and LJM_OpenS.
Specifying the Port
TCP/UDP port can be specified by appending a colon and the port number. For example, if your Identifier is the IP address 192.168.1.42 and you want to connect to port 502, your Identifier would be "192.168.1.42:502".
Most users should not need to specify the TCP/UDP port.
Hex vs. Decimal IP Address
LJM assumes IP addresses to be decimal, unless they specifically look hexadecimal. To use a hex IP address, append "0x" to the IP address and write it with a dot between each byte.
For example, for the hexadecimal IP address C0A8012A can be passed as Identifier as "0xC0.A8.01.2A". (An Identifier like "C0.A8.01.2A" would be interpreted as a hex IP address because it contains letters, but it is highly recommended to use the "0x" prefix anyway.)
Any port specified will still be interpreted as a decimal number.
Specifying the Pipe
For USB, pipe can be specified by appending a colon and the pipe number. For example, if your Identifier is the serial number 470010117 and you want to connect to pipe 0, your Identifier would be "470010117:0".
Most users should not need to specify the USB pipe.
If you don't have a device available, you can use LJM_DEMO_MODE (or the string "-2") to open a fake device. For more details, see Demo Mode.
Closing a handle will remove the associated device connection from the LJM library, free the device to be opened again, and free allocated system resources.
LJM_ERROR_RETURN LJM_Close(int Handle)
LJM errorcode or 0 for no error.
LJM_Close is useful when multiple LabJack device connections are open and only a single connection needs to be closed. Use LJM_CloseAll to close every device in a single function call.
[C#] Closing one device
int LJMError; //handle from LJM_Open() LJMError = LabJack.LJM.Close(handle);
[C/C++] Closing one device
int LJMError; //handle from LJM_Open() LJMError = LJM_Close(handle);
Closing all devices will remove all device handles from the LJM library, free all previously open devices to be opened again, and free allocated system resources.
LJM_ERROR_RETURN LJM_CloseAll()
LJM errorcode or 0 for no error.
LJM_CloseAll is useful on program exit. Use LJM_Close to close an individual device handle.
[C/C++] Closing all open devices
int LJMError; LJMError = LJM_CloseAll();