none
Windows CE wrapper doubt RRS feed

  • Question

  • Hi all,

    I am using a embedded PC with a Windows CE image.

    I need to use I2C bus. The vendor gave me an API with drivers to manipulate hardware features.

    For the i2c there are some code wrappers to call since C#. I have succeed in using some of them, but i need to trnasfer data and it is neccessary to define some structures and i dont´t do it well.

    These are the structures in C++ and after my ones in C#, please tell me if thre are something wrong.

    typedef struct {
    BYTE byAddr;
    BYTE byRW;
    PBYTE pbyBuf;
    WORD wLen;
    LPINT lpiResult;
    } I2C_PACKET, *PI2C_PACKET;

    Members
    byAddr 7-bit slave address that specifies the target I2C device to or from which data is read
    or written
    byRW Determines whether the packet is a read or a write packet. Set to I2C_RW_READ
    for reading and I2C_RW_WRITE for writing. Set to I2C_POLLING_MODE to
    force polling mode for transfer.
    pbyBuf Pointer to a buffer of bytes. For a read operation, this is the buffer into which data
    is read. For a write operation, this buffer contains the data to write to the target
    device.
    wLen If the operation is a read, wLen specifies the number of bytes to read into pbyBuf.
    If the operation is a write, wLen specifies the number of bytes to write from
    pbyBuf.
    lpiResult Pointer to an int that contains the return code from the transfer operation

    Ahora la otra:
    typedef struct {
    I2C_PACKET *pI2CPackets;
    INT32 iNumPackets;
    } I2C_TRANSFER_BLOCK, *PI2C_TRANSFER_BLOCK;
    Members
    pI2CPackets Pointer to an array of I2C_PACKET objects
    iNumPackets Number of I2C_PACKET objects pointed to by pI2CPackets

    The mines:
    [StructLayout(LayoutKind.Sequential)]
        public struct I2C_PACKET
        {
            public byte byAddr;
            public byte byRW;
            public byte pbyBuf;
            public UInt16 wLen;
            public IntPtr lpiResult;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct I2C_TRANSFER_BLOCK
        {
            public I2C_PACKET[] paquete;
            public int iNunPackets;

        } 

     

    Thanks in advance.

    Tuesday, February 15, 2011 7:24 AM

All replies

  • Hi,

    there is a mistake in your I2C_PACKET declaration, pbyBuf is a pointer, not a byte !

    try this, this should work

     

    [StructLayout(LayoutKind.Sequential)]
        public struct I2C_PACKET
        {
            public byte byAddr;
            public byte byRW;
            public byte[] pbyBuf;
            public UInt16 wLen;
            public IntPtr lpiResult;
        }

    Other thing you have to notice, is that lpiResult should be a pointer to an integer, which will store transfer result status.



    Best regards
    Fabrice

     

    Tuesday, February 15, 2011 7:35 AM
  • Hi,

    Thanks for your answer KroMignon,

    pbyBuf should point to a buffer of bytes, how could i make this in C#?  because i am lost with this issue.

    Thank you again.

    Tuesday, February 15, 2011 8:08 AM
  • You can do it like that

    [StructLayout(LayoutKind.Sequential)]
    public struct I2C_PACKET
    {
        public byte byAddr;
        public byte byRW;
        public byte[] pbyBuf;
        public UInt16 wLen;
        public IntPtr lpiResult;
    }

    then create a instance of this structure

    [StructLayout(LayoutKind.Sequential)]
    public struct I2C_PACKET
    {
        public byte byAddr;
        public byte byRW;
        public byte[] pbyBuf;
        public UInt16 wLen;
        public IntPtr lpiResult;
    }

    I2C_PACKET myPacket = new I2C_PACKET();

    myPacket.byAddr = 1; // Address of device
    myPacket.byRW = 0;   // Reading access (or write ??)
    myPacket.wLen = 4;   // Size of the message
    myPacket.pbyBuf = new byte[myPacket.wLen];
    myPacket.lpiResult = IntPtr.Zero;

     

    Tuesday, February 15, 2011 8:23 AM