The low-level Feedback functions LJM_AddressesToMBFB, LJM_MBFBComm, and LJM_UpdateValues may be used together or separately to perform Modbus Feedback (MBFB) operations on the LabJack device.
For simpler functions that do the same thing as the low-level Feedback functions, see the Single Value Functions or the Multiple Value Functions.
[C/C++] Write to DAC0, read from AIN0 using LJM_AddressesToMBFB, LJM_MBFBComm, and LJM_UpdateValues
/** * Name: stepwise_feedback.c * Desc: Shows how to read from a few analog inputs * using a LabJack and the cross platform LabJackM * Library using the C-style API **/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <LabJackM.h> #include "LJM_Utilities.h" int main() { int handle, err; // Just something that isn't positive so we know // errAddress has or hasn't been modified int errAddress = -2; // Set up the data enum { NUM_FRAMES = 2 }; int numFrames = NUM_FRAMES; const char * ADDRESS_STRINGS[NUM_FRAMES] = {"DAC0", "AIN0"}; const double VALUE_0 = 1.23; const double VALUE_1 = 0.0; int aAddresses[NUM_FRAMES]; int aTypes[NUM_FRAMES]; int aWrites[NUM_FRAMES] = {LJM_WRITE, LJM_READ }; int aNumValues[NUM_FRAMES] = {1, 1 }; enum { NUM_VALUES = 2 }; double aValues[NUM_VALUES] = {VALUE_0, VALUE_1 }; int MaxBytesPerMBFB = LJM_DEFAULT_FEEDBACK_ALLOCATION_SIZE; unsigned char aMBFB[LJM_DEFAULT_FEEDBACK_ALLOCATION_SIZE]; unsigned char UnitID = LJM_DEFAULT_UNIT_ID; // Fill out aAddresses err = LJM_NamesToAddresses(NUM_FRAMES, ADDRESS_STRINGS, aAddresses, aTypes); ErrorCheck(err, "LJM_NamesToAddresses"); // Open first found LabJack err = LJM_Open(LJM_dtANY, LJM_ctANY, "LJM_idANY", &handle); ErrorCheck(err, "LJM_Open"); PrintDeviceInfoFromHandle(handle); err = LJM_AddressesToMBFB(MaxBytesPerMBFB, aAddresses, aTypes, aWrites, aNumValues, aValues, &numFrames, aMBFB); ErrorCheck(err, "LJM_AddressesToMBFB"); printf("\nLJM_MBFBComm will overwrite the Transaction ID and Unit ID of the following command\n"); PrintFeedbackCommand(aMBFB, "Feedback command"); // Send the command and receive the response err = LJM_MBFBComm(handle, UnitID, aMBFB, &errAddress); ErrorCheckWithAddress(err, errAddress, "LJM_MBFBComm"); PrintFeedbackResponse(aMBFB, "Feedback response"); // Get the data back in a readable form err = LJM_UpdateValues(aMBFB, aTypes, aWrites, aNumValues, numFrames, aValues); ErrorCheck(err, "LJM_UpdateValues"); // Print results printf("%s: %f\n", ADDRESS_STRINGS[1], aValues[1]); // Close err = LJM_Close(handle); ErrorCheck(err, "LJM_Close"); WaitForUserIfWindows(); return LJME_NOERROR; }
Possible output:
deviceType: LJM_dtT7
connectionType: LJM_ctUSB
serialNumber: 470010117
pipe: 0
The maximum number of bytes you can send to or receive from this device in one packet is 64 bytes.
LJM_MBFBComm will overwrite the Transaction ID and Unit ID of the following command
Feedback command:
Header: 0x00 0x00 0x00 0x00 0x00 0x0e 0x01 0x4c
frame 00: 0x01 0x03 0xe8 0x02 0x3f 0x9d 0x70 0xa4
frame 01: 0x00 0x00 0x00 0x02
Feedback response:
Header: 0x01 0x89 0x00 0x00 0x00 0x06 0x01 0x4c
data: 0x40 0x9a 0xfd 0x5f
AIN0: 4.843429
Creates a Modbus Feedback (MBFB) packet. This packet can be sent to the device using LJM_MBFBComm.
LJM_ERROR_RETURN LJM_AddressesToMBFB(
int MaxBytesPerMBFB,
int * aAddresses,
int * aTypes,
int * aWrites,
int * aNumValues,
double * aValues,
int * NumFrames,
unsigned char * aMBFBCommand)
LJM errorcodes or 0 for no error.
The LJM_NamesToAddresses and LJM_NameToAddress functions may be used to initialize the aAddresses and aTypes parameters from register names.
Please see the Low-level Feedback Functions overview page for an example.
Sends a Feedback command and receives a Feedback response, parsing the response for obvious errors. The Feedback command may be generated using LJM_AddressesToMBFB and the Feedback response may be parsed with the LJM_UpdateValues function.
LJM_ERROR_RETURN LJM_MBFBComm(
int Handle,
unsigned char UnitID,
unsigned char * aMBFB,
int * ErrorAddress)
LJM errorcodes or 0 for no error.
LJM_MBFBComm provides device synchronization within LJM.
Please see the Low-level Feedback Functions overview page for an example.
Converts read values in a Modbus Feedback response packet.
LJM_ERROR_RETURN LJM_UpdateValues(
unsigned char * aMBFBResponse,
const int * aTypes,
const int * aWrites,
const int * aNumValues,
int NumFrames,
double * aValues)
Note that some of these parameters may be used directly from LJM_AddressesToMBFB and/or LJM_MBFBComm.
LJM errorcodes or 0 for no error.
The Type Conversion functions may also be used to read bytes to types.
Please see the Low-level Feedback Functions overview page for an example.