The GenICamI2CUsage program is a short example which does not include any image acquisition. It will try to open the selected device and will try to obtain access to the cameras I2C bus and show some data received from the device's temperature sensor.
- Since
- 2.27.0
- Note
- Not every device supports the customer usage of the I2C interface. The following I2C addresses are locked and therefore not usable: 0x00, 0x20, 0x34, 0x36, 0x3E, 0x48, 0x60, 0x62, 0x64, 0x66, 0x6E, 0x90, 0x92, 0xA0, 0xA2, 0xA4, 0xA6, 0xAE, 0xB0, 0xB2, 0xB4, 0xB6, 0xB8, 0xBA, 0xBC, 0xBE, 0xF8
- GenICamI2cUsage example:
- Opens a device
- Checks if the opened device supports I2C access
- Obtains access to one of the temperature sensors
- Prints the values read from the temperature sensor to the console
- Note
- This example only shows how to read out the temperature sensor using I2C. To read out the temperature later in the final application in a more convenient way, use the corresponding property (mvIMPACT.acquire.GenICam.DeviceControl.deviceTemperature).
- How it works
In the first step it is verified if the device is allows access to its I2C bus. Once the device allows access to the I2C bus, it will be configured.
- Note
- Do not write to other temperature sensor registers than the one used in this example, otherwise the sensor may not work properly afterwards.
GenICam.mvI2cInterfaceControl pI2C = null;
try
{
int i2cDeviceAddress = pDev.manufacturerSpecificInformation.read().Contains(";SF3") || pDev.manufacturerSpecificInformation.read().Contains("-3M") ? 0x32 : 0x30;
pI2C = new GenICam.mvI2cInterfaceControl(pDev);
if (!pI2C.mvI2cInterfaceEnable.isValid && !pI2C.mvI2cInterfaceWrite.isValid )
{
Console.WriteLine("No custom I2C support available on selected camera, press return to end the application...");
Console.ReadLine();
Environment.Exit(1);
}
pI2C.mvI2cInterfaceEnable.write(TBoolean.bTrue);
pI2C.mvI2cInterfaceSpeed.writeS("kHz_400");
pI2C.mvI2cInterfaceDeviceAddress.write(i2cDeviceAddress);
pI2C.mvI2cInterfaceDeviceSubAddress.write(0x08);
pI2C.mvI2cInterfaceWrite.call();
}
catch (Exception e)
{
Console.WriteLine("ERROR: Selected device does not support i2c interface: {0}, terminating...", e.Message);
System.Environment.Exit(1);
}
Finally the measurement value of the temperature sensor is read out in a loop and printed to console.
while (!terminated)
{
pI2C.mvI2cInterfaceDeviceSubAddress.write(0x05);
pI2C.mvI2cInterfaceBytesToRead.write(0x02);
pI2C.mvI2cInterfaceRead.call();
byte[] i2cReadBinaryData = pI2C.mvI2cInterfaceBinaryBuffer.readBinary();
int temp = (i2cReadBinaryData[0] * 256) + i2cReadBinaryData[1];
float tempDisp = (float)((int)((temp << 3) & 0xffff)) / 128;
if (tempDisp != tempPrinted)
{
Console.WriteLine("Mainboard Temperature {0} degrees Celsius", tempDisp);
tempPrinted = tempDisp;
}
System.Threading.Thread.Sleep(1000);
}
- Source code
using mv.impact.acquire.examples.helper;
using System;
using System.Threading;
namespace mv.impact.acquire.examples
{
class GenICamI2cUsage
{
static void Main(string[] args)
{
Device pDev = DeviceAccess.getDeviceFromUserInput();
if (pDev == null)
{
Console.WriteLine("Unable to continue! Press any key to end the program.");
Console.Read();
Environment.Exit(1);
}
Console.WriteLine("Initialising the device. This might take some time...");
try
{
pDev.open();
}
catch (ImpactAcquireException e)
{
Console.WriteLine("An error occurred while opening device " + pDev.serial +
"(error code: " + e.Message + "). Press any key to end the application...");
Console.ReadLine();
Environment.Exit(1);
}
bool terminated = false;
Console.WriteLine("Press [ENTER] to end the application");
Thread thread = new Thread(delegate ()
{
GenICam.mvI2cInterfaceControl pI2C = null;
try
{
int i2cDeviceAddress = pDev.manufacturerSpecificInformation.read().Contains(";SF3") || pDev.manufacturerSpecificInformation.read().Contains("-3M") ? 0x32 : 0x30;
pI2C = new GenICam.mvI2cInterfaceControl(pDev);
if (!pI2C.mvI2cInterfaceEnable.isValid && !pI2C.mvI2cInterfaceWrite.isValid)
{
Console.WriteLine("ERROR: I2C properties which are essential to run this sample are not available for the selected device, press return to end the application...");
Console.ReadLine();
Environment.Exit(1);
}
pI2C.mvI2cInterfaceEnable.write(TBoolean.bTrue);
pI2C.mvI2cInterfaceSpeed.writeS("kHz_400");
pI2C.mvI2cInterfaceDeviceAddress.write(i2cDeviceAddress);
pI2C.mvI2cInterfaceDeviceSubAddress.write(0x08);
pI2C.mvI2cInterfaceWrite.call();
pI2C.mvI2cInterfaceDeviceSubAddress.write(0x08);
byte[] writeBuffer = new byte[] { 1 };
pI2C.mvI2cInterfaceBinaryBuffer.writeBinary(writeBuffer);
pI2C.mvI2cInterfaceBytesToWrite.write(1);
pI2C.mvI2cInterfaceWrite.call();
pI2C.mvI2cInterfaceDeviceSubAddress.write(0x10801);
pI2C.mvI2cInterfaceBytesToWrite.write(0);
pI2C.mvI2cInterfaceWrite.call();
}
catch (Exception e)
{
Console.WriteLine("ERROR! I2C properties which are essential to run this sample are not available for the selected device (error {0}), terminating...", e.Message);
System.Environment.Exit(1);
}
double lastTemperatureDisplayed = 0;
while (!terminated)
{
pI2C.mvI2cInterfaceDeviceSubAddress.write(0x05);
pI2C.mvI2cInterfaceBytesToRead.write(0x02);
pI2C.mvI2cInterfaceRead.call();
byte[] i2cReadBinaryData = pI2C.mvI2cInterfaceBinaryBuffer.readBinary();
int temp = (i2cReadBinaryData[0] * 256) + i2cReadBinaryData[1];
double currentTemperature = (double)(((temp << 3) & 0xffff)) / 128;
if (currentTemperature != lastTemperatureDisplayed)
{
Console.WriteLine("Mainboard Temperature {0} °C", currentTemperature);
lastTemperatureDisplayed = currentTemperature;
}
System.Threading.Thread.Sleep(1000);
}
});
thread.Start();
Console.ReadLine();
terminated = true;
thread.Join();
}
}
}
A small helper class to administer various library search path related variables and paths.
Definition LibraryPath.cs:14
static void init()
Calling this method will add the folders containing unmanaged libraries to the systems library search...
Definition LibraryPath.cs:251
This namespace contains classes and functions belonging to the image acquisition module of this SDK.
Definition Enumerations.cs:2
Definition Enumerations.cs:2
Definition Enumerations.cs:2