2025-11-24 7:55 AM - last edited on 2025-11-25 4:45 AM by Andrew Neil
Hi
I am new to the I2C communications. I am trying to communicate to the IMS330dlc chip. I am using a lab jack U3-HV.. I am programming this via Visual Studio 2022 using c# code. I cant get back the 106 for Who am I. below is the code that I setup on a push button so I could step through the code. or if anyone has example code using Visual Studio C# 2022 please send it .
private void Read_AccelBtn_Click(object sender, EventArgs e)
{
{
LJUD.IO ioType = 0;
LJUD.CHANNEL channel = 0;
double dblValue = 0;
double dummyDouble = 0;
int dummyInt = 0;
double numI2CBytesToWrite;
double numI2CBytesToRead;
byte[] writeArray = new byte[12];
byte[] readArray = new byte[12];
long i = 0;
long serialNumber = 0;
double slopeDACA = 0, offsetDACA = 0, slopeDACB = 0, offsetDACB = 0;
double writeACKS = 0, expectedACKS = 0;
numI2CBytesToWrite = 1;
writeArray[0] = 0; //Memory address. User area is 0-63.
//Open the LabJack.
try
{
device = new U3(LJUD.CONNECTION.USB, "0", true); // Connection through USB
}
catch (LabJackUDException f)
{
showErrorMessage(f);
}
//Configure the I2C communication.
//The address of the ISM330dcl on the LJTick-DAC is 0xA0.
LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_ADDRESS_BYTE, 106, 0, 0);
//SCL is FIO4
LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_SCL_PIN_NUM, 4, 0, 0);
//SDA is FIO5
LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_SDA_PIN_NUM, 5, 0, 0);
//See description of low-level I2C function.
// LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_OPTIONS, 0, 0, 0);
//See description of low-level I2C function.
LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_OPTIONS, 4, 0, 0);
//See description of low-level I2C function. 0 is max speed of about 130 kHz.
LJUD.AddRequest(device.ljhandle, LJUD.IO.PUT_CONFIG, LJUD.CHANNEL.I2C_SPEED_ADJUST, 0, 0, 0);
//Execute the requests on a single LabJack.
LJUD.GoOne(device.ljhandle);
//Get all the results just to check for errors.
LJUD.GetFirstResult(device.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble);
bool finished = false;
while (!finished)
{
try { LJUD.GetNextResult(device.ljhandle, ref ioType, ref channel, ref dblValue, ref dummyInt, ref dummyDouble); }
catch (LabJackUDException f)
{
if (f.LJUDError == LJUD.LJUDERROR.NO_MORE_DATA_AVAILABLE)
finished = true;
else
showErrorMessage(f);
}
}
//build write request
// Tell the labjack to cou
//tell the LabJack to count the ACK bits
// LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS,0,0,0 );
// LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE);
//Execute the requests on a single LabJack.
// LJUD.GoOne(device.ljhandle);
// double dblAcks = ();
// Console write dblAcks;
// LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, ref writeACKS);
// expectedACKS = Math.Pow(2, numI2CBytesToWrite + 1) - 1;
// if (writeACKS != expectedACKS)
// Console.Out.WriteLine("Expected ACKs = {0}, Received ACKs = {1}\n", expectedACKS, writeACKS);
numI2CBytesToWrite = 1;
writeArray[0] = 15; //Memory address. who am i.
LJUD.AddRequestPtr(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, numI2CBytesToWrite, writeArray, 0);
LJUD.AddRequest(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, 0, 0, 0);
numI2CBytesToRead = 1;
LJUD.AddRequestPtr(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_READ, numI2CBytesToRead, readArray, 0);
//Execute the requests.
LJUD.GoOne(device.ljhandle);
//Get the result of the write just to check for an error.
LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_WRITE, ref dummyDouble);
//Get the write ACKs and compare to the expected value. We expect bit 0 to be
//the ACK of the last data byte progressing up to the ACK of the address
//byte (data bytes only for Control firmware 1.43 and less). So if n is the
//number of data bytes, the ACKs value should be (2^(n+1))-1.
LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_GET_ACKS, ref writeACKS);
expectedACKS = Math.Pow(2, numI2CBytesToWrite + 1) - 1;
if (writeACKS != expectedACKS)
Console.Out.WriteLine("Expected ACKs = {0}, Received ACKs = {1}\n", expectedACKS, writeACKS);
//When the GoOne processed the read request, the read data was put into the readArray buffer that
//we passed, so this GetResult is also just to check for an error.
LJUD.GetResult(device.ljhandle, LJUD.IO.I2C_COMMUNICATION, LJUD.CHANNEL.I2C_READ, ref dummyDouble);
//Display the first 2 elements.
Console.Out.WriteLine("Read User Mem [0-1] = {0}, {1},\n\n", readArray[0], readArray[1]);Edited to apply source code formatting - please see How to insert source code for future reference.