Jump to content
Sign in to follow this  
saget

DllCall: I2C adapter communication help?

Recommended Posts

saget

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

Share this post


Link to post
Share on other sites
FireFox

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

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.