📄 i2c.cs
字号:
try
{
this.DeviceIoControl(IOCTL_TRANSFER2, SerializeToByteArray(packet), null);
result = (Int32)Marshal.PtrToStructure(pResult, typeof(Int32));
if (result < 0)
BusError(result);
}
catch (Exception)
{
throw new Exception("Unable to complete I2C transaction:" + Marshal.GetLastWin32Error());
}
finally
{
Marshal.FreeHGlobal(buffer);
Marshal.FreeHGlobal(pResult);
}
}
/// <summary>
/// Transfer eight bytes (MSB first) to the I2C bus
/// </summary>
/// <param name="address">Slave address</param>
/// <param name="data">MSB transfered first</param>
public void Write(byte address, UInt64 data)
{
Int32 result = 0;
IntPtr pResult = Marshal.AllocHGlobal(Marshal.SizeOf(result));
int rawsize = Marshal.SizeOf(data);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.StructureToPtr(data, buffer, false);
Packet packet = new Packet(address, RW.WRITE, buffer, rawsize, pResult);
try
{
this.DeviceIoControl(IOCTL_TRANSFER2, SerializeToByteArray(packet), null);
result = (Int32)Marshal.PtrToStructure(pResult, typeof(Int32));
if (result < 0)
BusError(result);
}
catch (Exception)
{
throw new Exception("Unable to complete I2C transaction:" + Marshal.GetLastWin32Error());
}
finally
{
Marshal.FreeHGlobal(buffer);
Marshal.FreeHGlobal(pResult);
}
}
/// <summary>
/// Read one byte of data from the I2C bus
/// </summary>
/// <param name="address">Slave address</param>
/// <param name="data">reference to data byte</param>
public void Read(byte address, ref byte data)
{
Int32 result = 0;
IntPtr pResult = Marshal.AllocHGlobal(Marshal.SizeOf(result));
int rawsize = Marshal.SizeOf(data);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Packet packet = new Packet(address, RW.READ, buffer, rawsize, pResult);
try
{
this.DeviceIoControl(IOCTL_TRANSFER2, SerializeToByteArray(packet), null);
data = (Byte)Marshal.PtrToStructure(buffer, typeof(Byte));
result = (Int32)Marshal.PtrToStructure(pResult, typeof(Int32));
if (result < 0)
BusError(result);
}
catch (Exception)
{
throw new Exception("Unable to complete I2C transaction:" + Marshal.GetLastWin32Error());
}
finally
{
Marshal.FreeHGlobal(buffer);
Marshal.FreeHGlobal(pResult);
}
}
/// <summary>
/// Read two bytes from the I2C bus
/// </summary>
/// <param name="address">Slave address</param>
/// <param name="data">reference to data word MSB contains first read byte</param>
public void Read(byte address, ref UInt16 data)
{
Int32 result = 0;
IntPtr pResult = Marshal.AllocHGlobal(Marshal.SizeOf(result));
int rawsize = Marshal.SizeOf(data);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Packet packet = new Packet(address, RW.READ, buffer, rawsize, pResult);
try
{
this.DeviceIoControl(IOCTL_TRANSFER2, SerializeToByteArray(packet), null);
data = (UInt16)Marshal.PtrToStructure(buffer, typeof(UInt16));
result = (Int32)Marshal.PtrToStructure(pResult, typeof(Int32));
if (result < 0)
BusError(result);
}
catch (Exception)
{
throw new Exception("Unable to complete I2C transaction:" + Marshal.GetLastWin32Error());
}
finally
{
Marshal.FreeHGlobal(buffer);
Marshal.FreeHGlobal(pResult);
}
}
/// <summary>
/// Read four bytes from the I2C bus
/// </summary>
/// <param name="address">Slave address</param>
/// <param name="data">reference to data dword MSB contains first read byte</param>
public void Read(byte address, ref UInt32 data)
{
Int32 result = 0;
IntPtr pResult = Marshal.AllocHGlobal(Marshal.SizeOf(result));
int rawsize = Marshal.SizeOf(data);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Packet packet = new Packet(address, RW.READ, buffer, rawsize, pResult);
try
{
this.DeviceIoControl(IOCTL_TRANSFER2, SerializeToByteArray(packet), null);
data = (UInt32)Marshal.PtrToStructure(buffer, typeof(UInt32));
result = (Int32)Marshal.PtrToStructure(pResult, typeof(Int32));
if (result < 0)
BusError(result);
}
catch (Exception)
{
throw new Exception("Unable to complete I2C transaction:" + Marshal.GetLastWin32Error());
}
finally
{
Marshal.FreeHGlobal(buffer);
Marshal.FreeHGlobal(pResult);
}
}
/// <summary>
/// Read eight bytes from the I2C bus
/// </summary>
/// <param name="address">Slave address</param>
/// <param name="data">reference to data qword MSB contains first read byte</param>
public void Read(byte address, ref UInt64 data)
{
Int32 result = 0;
IntPtr pResult = Marshal.AllocHGlobal(Marshal.SizeOf(result));
int rawsize = Marshal.SizeOf(data);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Packet packet = new Packet(address, RW.READ, buffer, rawsize, pResult);
try
{
this.DeviceIoControl(IOCTL_TRANSFER2, SerializeToByteArray(packet), null);
data = (UInt64)Marshal.PtrToStructure(buffer, typeof(UInt64));
result = (Int32)Marshal.PtrToStructure(pResult, typeof(Int32));
if (result < 0)
BusError(result);
}
catch (Exception)
{
throw new Exception("Unable to complete I2C transaction:" + Marshal.GetLastWin32Error());
}
finally
{
Marshal.FreeHGlobal(buffer);
Marshal.FreeHGlobal(pResult);
}
}
#endregion
#region P/Invoke helpers
/// <summary>
/// Byte array serializer
/// </summary>
/// <param name="anything"></param>
/// <returns></returns>
private static byte[] SerializeToByteArray(object anything)
{
int rawsize = Marshal.SizeOf(anything);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.StructureToPtr(anything, buffer, false);
byte[] rawdatas = new byte[rawsize];
Marshal.Copy(buffer, rawdatas, 0, rawsize);
Marshal.FreeHGlobal(buffer);
return rawdatas;
}
/// <summary>
/// De-serializer from byte array
/// </summary>
/// <param name="rawdatas"></param>
/// <param name="anytype"></param>
/// <returns></returns>
private static object DeserializeFromByteArray(byte[] rawdatas, Type anytype)
{
int rawsize = Marshal.SizeOf(anytype);
if (rawsize > rawdatas.Length)
return null;
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.Copy(rawdatas, 0, buffer, rawsize);
object retobj = Marshal.PtrToStructure(buffer, anytype);
Marshal.FreeHGlobal(buffer);
return retobj;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -