5.1 - General Protocol [U6 Datasheet] | LabJack

5.1 - General Protocol [U6 Datasheet]

Following is a description of the general U6 low-level communication protocol. There are two types of commands:

Normal: 1 command word plus 0-7 data words.

Extended: 3 command words plus 0-125 data words.

Normal commands have a smaller packet size and can be faster in some situations. Extended commands provide more commands, better error detection, and a larger maximum data payload.

Table 5.1-1. Normal command format

 Byte 0 Checksum8: Includes bytes 1-15. 1 Command Byte: DCCCCWWW Bit 7: Destination bit: 0 = Local, 1 = Remote. Bits 6-3: Normal command number (0-14). Bits 2-0: Number of data words. 2-15 Data Words.

Table 5.1-2. Extended command format

 Byte 0 Checksum8: Includes bytes 1-5. 1 Command Byte: D1111WWW Bit 7: Destination bit: 0 = Local, 1 = Remote. Bits 6-3: 1111 specifies that this is an extended Command. Bits 2-0: Used with some commands. 2 Number of data words 3 Extended command number. 4 Checksum16 (LSB) 5 Checksum16 (MSB) 6-255 Data words.

Checksum calculations:

All checksums are a “1’s complement checksum”. Both the 8-bit and 16-bit checksum are unsigned. Sum all applicable bytes in an accumulator, 1 at a time. Each time another byte is added, check for overflow (carry bit), and if true add one to the accumulator.

In a high-level language, do the following for the 8-bit normal command checksum:

• Get the subarray consisting of bytes 1 and up.
• Convert bytes to U16 and sum into a U16 accumulator.
• Divide by 28 and sum the quotient and remainder.
• Divide by 28 and sum the quotient and remainder.

In a high-level language, do the following for an extended command 16-bit checksum:

• Get the subarray consisting of bytes 6 and up.
• Convert bytes to U16 and sum into a U16 accumulator (can’t overflow).

Then do the following for the 8-bit extended checksum:

• Get the subarray consisting of bytes 1 through 5.
• Convert bytes to U16 and sum into a U16 accumulator.
• Divide by 28 and sum the quotient and remainder.
• Divide by 28 and sum the quotient and remainder.

Destination Bit:

This bit specifies whether the command is destined for the local or remote target. This bit is ignored on the U6.

Multi-byte parameters:

In the following function definitions there are various multi-byte parameters. The least significant byte of the parameter will always be found at the lowest byte number. For instance, bytes 10 through 13 of CommConfig are the IP address which is 4 bytes long. Byte 10 is the least significant byte (LSB), and byte 13 is the most significant byte (MSB).

Some functions have mask parameters. The WriteMask found in some functions specifies which parameters are to be written. If a bit is 1, that parameter will be updated with the new passed value. If a bit is 0, the parameter is not changed and only a read is performed.

The AINMask found in some functions specifies which analog inputs are acquired. This is a 16-bit parameter where each bit corresponds to AIN0-AIN15. If a bit is 1, that channel will be acquired.

The digital I/O masks, such as FIOMask, specify that the passed value for direction and state are updated if a bit 1. If a bit of the mask is 0 only a read is performed on that bit of I/O.

Binary Encoded Parameters:

Many parameters in the following functions use specific bits within a single integer parameter to write/read specific information. In particular, most digital I/O parameters contain the information for each bit of I/O in one integer, where each bit of I/O corresponds to the same bit in the parameter (e.g. the direction of FIO0 is set in bit 0 of parameter FIODir). For instance, in the function ControlConfig, the parameter FIODir is a single byte (8 bits) that writes/reads the direction of each of the 8 FIO lines:

• if FIODir is 0, all FIO lines are input,
• if FIODir is 1 (20), FIO0 is output, FIO1-FIO7 are input,
• if FIODir is 5 (20 + 22), FIO0 and FIO2 are output, all other FIO lines are input,
• if FIODir is 255 (20 + … + 27), FIO0-FIO7 are output.

checksum algorithms

For those interested, LabJack has both 8- and 16-bit checksum algorithms implemented in their Python code:

\LabJackPython-2.0.4.zip\src\LabJackPython.py

def setChecksum16(buffer):     total = 0;     for i in range(6, len(buffer)):         total += (buffer[i] & 0xff)     buffer[4] = (total & 0xff)     buffer[5] = ((total >> 8) & 0xff)     return buffer def setChecksum8(buffer, numBytes):     total = 0     for i in range(1, numBytes):         total += (buffer[i] & 0xff)     buffer[0] = (total & 0xff) + ((total >> 8) & 0xff)     buffer[0] = (buffer[0] & 0xff) + ((buffer[0] >> 8) & 0xff)     return buffer

To add to ElectroLund's comment, there are setChecksum and verifyChecksum commands available, and these can be accessed online in our LabJackPython git repo as well:

https://github.com/labjack/LabJackPython/blob/d41ba56420f8ddd8a7d7118bf6...

c implementation?

Is there a C port of these functions?  My python's not so good.  I'm getting 0xB8 responses back from the LabJack, so my calculated checksums are bad.

Here's what I came up with:

void setChecksum16(char buffer[], int length)
{
short total = 0;

// starting at byte 6 and above
for (int i=6; i<length; i++)
{
total += (buffer[i] & 0xff);
}

buffer[4] = total & 0xff;                // LSB is at 4
buffer[5] = (total >> 8) & 0xff;        // MSB is at 5
}

void setChecksum8(char buffer[], int numBytes)
{
short total = 0;

for (int i=1; i<numBytes; i++)
{
total += (buffer[i] & 0xff);
}

// checksum resides at first location
buffer[0] = (total & 0xff) + ((total >> 8) & 0xff);
buffer[0] = (buffer[0] & 0xff) + ((buffer[0] >> 8) & 0xff);
}

Yes, we have checksum

Yes, we have checksum functions available with our Exodriver examples:

https://github.com/labjack/exodriver/blob/master/examples/U6/u6.c#L54