C#呼叫指標DLL存取USB[CS_call_cpp_pointer_dll_for_usb] ~ EasyCOM/RD200

C#呼叫指標DLL存取USB[CS_call_cpp_pointer_dll_for_usb] ~ EasyCOM/RD200

C#呼叫指標DLL存取USB[CS_call_cpp_pointer_dll_for_usb] ~ EasyCOM/RD200


GITHUB: https://github.com/jash-git/CS_call_cpp_pointer_dll_for_usb


C# 使用 unsafe + fixed 建立使用指標

C# 建立 DLL 函數原型宣告檔

C# 使用 class 宣告 struct 


DLL

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct MW_EasyPOD
{
    public uint VID;			        // Need to match user device's "Vendor ID".
    public uint PID;			        // Need to match user device's "Product ID".
    public uint ReadTimeOut;		    // Specifies the read data time-out interval, in milliseconds.
    public uint WriteTimeOut;		    // Specifies the write data time-out interval, in milliseconds.    
    public uint Handle;                 // Do not modify this value, reserved for DLL
    public uint FeatureReportSize;      // Do not modify this value, reserved for DLL
    public uint InputReportSize;        // Do not modify this value, reserved for DLL
    public uint OutputReportSize;       // Do not modify this value, reserved for DLL   
}


class PODfuncs
{
    [DllImport("EasyPOD.dll", CallingConvention = CallingConvention.StdCall)]
    unsafe public static extern uint ConnectPOD(MW_EasyPOD* pEasyPOD, uint Index);

    [DllImport("EasyPOD.dll", CallingConvention = CallingConvention.StdCall)]
    unsafe public static extern uint WriteData(MW_EasyPOD* pEasyPOD, byte[] lpBuffer, uint nNumberOfBytesToWrite, uint* lpNumberOfBytesWritten);

    [DllImport("EasyPOD.dll", CallingConvention = CallingConvention.StdCall)]
    unsafe public static extern uint ReadData(MW_EasyPOD* pEasyPOD, byte[] lpBuffer, uint nNumberOfBytesToRead, uint* lpNumberOfBytesRead);

    [DllImport("EasyPOD.dll", CallingConvention = CallingConvention.StdCall)]
    unsafe public static extern uint DisconnectPOD(MW_EasyPOD* pEasyPOD);

    [DllImport("EasyPOD.dll", CallingConvention = CallingConvention.StdCall)]
    unsafe public static extern uint ClearPODBuffer(MW_EasyPOD* pEasyPOD);
}

Main

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using System.BitConverter;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        public MW_EasyPOD EasyPOD;

        unsafe public void btnGetUID_Click(object sender, EventArgs e)
        {           
            UInt32 drResult, dwResult, Index, uiWritten, uiRead;

            byte[] WriteBuffer_SN = new byte[] { 0x02, 0x01, 0x0D };//Command {STX, LEN, CMD,.....}
            byte[] sResponse_SN = null;
            sResponse_SN = new byte[10+2];


            EasyPOD.VID = 0xe6a;
            EasyPOD.PID = 0x317;
            Index = 1;
            

            fixed (MW_EasyPOD* pPOD = &EasyPOD)
            {

                dwResult = PODfuncs.ConnectPOD(pPOD, Index);

                if ((dwResult != 0))
                {
                    MessageBox.Show("Not connected yet");
                }
                else
                {
                    EasyPOD.ReadTimeOut = 200;
                    EasyPOD.WriteTimeOut = 200;

                    dwResult = PODfuncs.WriteData(pPOD, WriteBuffer_SN, Convert.ToUInt32(WriteBuffer_SN.Length), &uiWritten);
                    drResult = PODfuncs.ReadData(pPOD, sResponse_SN, Convert.ToUInt32(sResponse_SN.Length), &uiRead);

                    txbGetSNh.Text = BitConverter.ToString(sResponse_SN,4).Replace("-"," "); //HEX
                    txbGetSNt.Text = System.Text.Encoding.Default.GetString(sResponse_SN, 4, sResponse_SN.Length - 4);  //ASCII
                }
                dwResult = PODfuncs.ClearPODBuffer(pPOD);
                dwResult = PODfuncs.DisconnectPOD(pPOD);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *