Jump to content

DllCall: I2C adapter communication help?


saget
 Share

Recommended Posts

Hi All,

i'm using base autoit for 1 years but never touch dllcall..

Recently, i just got a cheam I2C USB adapter (made in china), and want read/wirte the device by autoit code, doese anyone get time to take a look at...? OR, is there any i2c UDF...?
 
The .net code

public partial class Form1 : Form
    {

[DllImport("USBIOX")]
extern static int USBIO_OpenDevice(int iIndex);

   [DllImport("USBIOX.dll")]
        public static extern bool USBIO_StreamI2C(int iIndex,int iWriteLength,byte[] iWriteBuffer,int iReadLength,byte[] oReadBuffer); 

private void btnI2CRun_Click(object sender, EventArgs e)
{

            bool mOpen=false;
            int mIndex=0;
            int hopen = USBIO_OpenDevice(0);
            if (hopen == INVALID_HANDLE_VALUE)
                mOpen = false;
            else
                mOpen = true;

         if (USBIO_SetStream(mIndex, 0x81) == false)
            {
                MessageBox.Show("I2c clock set fail = 100KHz fail!");
                return;
            }

        else

{

       int mWRLen=2, mRdLen=3;
       byte[] iBuff = new byte[3], buffer = new byte[2];
       buffer[0] = 0x92;
       buffer[1] = 0x31;

       if (mOpen)
                {
                    if (USBIO_StreamI2C(mIndex, mWRLen, buffer, mRdLen, iBuff) == false)
                        MessageBox.Show( "I2C stream read/write fail﹗");
                    else
                    {
                        if (mRdLen > 0) //get returned data
                        {
                            string buff=string.Empty;
                            int i;
                            for (i = 0; i < mRdLen; i++)
                                buff +=  iBuff[i].ToString() + ",";
                        }
                    }
                }

   }

}

Also, i found the full vb code from vendor, the point is how can i import the dll function in autit just like VB does?

e,g.

Declare Function USBIO_OpenDevice Lib "USBIOX.DLL" (ByVal iIndex As Long) As Long
Declare Sub USBIO_CloseDevice Lib "USBIOX.DLL" (ByVal iIndex As Long)

Declare Function USBIO_StreamI2C Lib "USBIOX.DLL" (ByVal iIndex As Long, ByVal iWriteLength As Long, ByRef iWriteBuffer As Any, ByVal iReadLength As Long, ByRef oReadBuffer As Any) As Boolean

USB2I2C_DEMO_VBEN.zip

Link to comment
Share on other sites

Hi,

Try this :

;Declare Function USBIO_OpenDevice Lib "USBIOX.DLL" (ByVal iIndex As Long) As Long
DllCall("USBIOX.DLL", "long", "USBIO_OpenDevice", "long", $iIndex)

;Declare Sub USBIO_CloseDevice Lib "USBIOX.DLL" (ByVal iIndex As Long)
DllCall("USBIOX.DLL", "none", "USBIO_CloseDevice", "long", $iIndex)

;Declare Function USBIO_StreamI2C Lib "USBIOX.DLL" (ByVal iIndex As Long, ByVal iWriteLength As Long, ByRef iWriteBuffer As Any, ByVal iReadLength As Long, ByRef oReadBuffer As Any) As Boolean
Local $tWriteBuffer = DllStructCreate("byte[" & $iWriteLength & "]")
DllStructSetData($tWriteBuffer, "0xABCDE0123...")

Local $tReadBuffer = DllStructCreate("byte[" & $iReadLength & "]")

DllCall("USBIOX.DLL", "boolean", "USBIO_StreamI2C", "long", $iIndex, "long", $iWriteLength, "struct*", $tWriteBuffer, "long", $iReadLength, "struct*", $tReadBuffer)

_

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...