由於常常要發送I2c command 給裝置,於是上網找了一個小小的USB轉I2C的小轉版,這顆IC是大陸廠家做的也提供一些 sample code,就是沒.NET的.只好拿VB6的來改.

我是用Win7 64bit, 歐賣尬.數位簽章出問題,看一下廠商提供的說明,開機時要按下F8選 "停用驅動程式強制簽章".

厚真是有夠麻煩,上網查了一下好像有工具(Driver Signature Enforcement Overrider)可以用,結論是無效的.

乖乖的回去按F8囉.

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時鐘 = 100KHz失敗﹗");
                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流模式讀寫數據失敗﹗");
                    else
                    {
                        if (mRdLen > 0) //有數據返回
                        {
                            string buff=string.Empty;
                            int i;
                            for (i = 0; i < mRdLen; i++)
                                buff +=  iBuff[i].ToString() + ",";
                        }
                    }
                }

   }

}

結果 Compiler 出現"試圖載入格式錯誤的程式",又是64bit造成的.

解決方法:C#專案 → 滑鼠右鍵 → 屬性 → (標籤)建置 → 平台目標 → Any CPU 改成 x86.

創作者介紹
創作者 李泥兒 的頭像
linear

李泥兒

linear 發表在 痞客邦 留言(0) 人氣( 1040 )