具有委托和CallBack函数的C#代码

时间:2019-05-14 12:09:36

标签: c# callback delegates

我需要编写一些C#代码以使用C ++ DLL连接到票据接收器。 我已经找到一些C#类来连接DLL。 我需要使用CallbackFunctionDelegate调用BVOpen函数。

奇怪的是,该行后面的代码

  callbackFunctionDelegate(ref bVUcontrol, iEvent);

从不执行。因此,使用委托显然存在问题。 提供了代码段(不是完整的代码)。

这就是我实现按钮单击以与Bill Acceptor连接的功能

        public void Connect([In, Out] ref BVUControl bVUcontrol, int iEvent)
        {
            ID003BasicDriver.BVUOpen(ref bVUcontrol, callbackFunctionDelegate);
            this.iEvent = iEvent;
        }


        private void btnConnect_Click(object sender, EventArgs e)
        {

            callbackFunctionDelegate =  Connect;

            callbackFunctionDelegate(ref bVUcontrol, iEvent);


            // Execution never comes to this line....
            if(iEvent == ID003BasicDriver.BVU_IDLING)
            {
                //
            }

            lblStatus.Visible = true;
            lblVersion.Visible = true;
            lblRecycler1.Visible = true;
            lblRecycler2.Visible = true;

            lblStatus.Text = bVUcontrol.CInfo;
            lblVersion.Text = bVUcontrol.firmwareMajorVersion.ToString();
            lblRecycler1.Text = bVUcontrol.RecycleDenom(0).ToString();
            lblRecycler2.Text = bVUcontrol.RecycleDenom(1).ToString();

            btnConnect.Enabled = false;
        }

 [StructLayout(LayoutKind.Sequential)]
    public unsafe struct BVUControl
    {
        public int idebugMode;
        public int iSerialPort;
        public int iDenominations; // Bill denominations to take    
        public int iDirection;     // Bill direction to accept       
        public int iBarCodeLen;    // bar code ticket characters length Min is 6 and Max is 18
        public int iBillValue;      // bill value
        public int iMaxNoResponseInterval; // Maximum time to wait for a response from host after a bill/coupon is received
                                           // if the DLL doesn't receive from a response , it will return the bill

        private fixed byte cInfo[100];
        public byte failureInfo;    //BV failure code 
        private byte cUnitType;

        public BvModels Model;
        private fixed byte cCountry[4];
        public int firmwareMajorVersion;

        private fixed byte cDenomination[16 * ID003BasicDriver.DENOM_MAX];

        private int hasRecycler;
        public int RecycleBoxCount;
        private fixed int recycledNoteCount[10];
        private fixed int recycleDenom[10];
        private fixed int recycleDenomBitPosition[16];

        public int EscrowUnit;
        public int EscrowCapacity;
        public int EscrowBillCount;
        public fixed int EscrowBills[100];

        public BVUControl(int serialPort) : this()
        {
            iSerialPort = serialPort;
            // some initializations with constants
            iBarCodeLen = 18;
            iDenominations = 0;
            iDirection = ID003BasicDriver.BVU_DIRECTION_A | ID003BasicDriver.BVU_DIRECTION_B | ID003BasicDriver.BVU_DIRECTION_C | ID003BasicDriver.BVU_DIRECTION_D;
            iMaxNoResponseInterval = 2550;
            // initialize the first 18 characters to be 0 according to the C++ code, why?
            fixed (byte* p = cInfo)
            {
                for (int i = 0; i < 18; i++)
                {
                    *(p + i) = 0;
                }
            }
        }

    }




    public class ID003BasicDriver
    {
        private const string DllName = "ID003 Basic Driver.dll";


        [DllImport(DllName, EntryPoint = "BVUOpen")]
        public static extern void BVUOpen([In, Out] ref BVUControl pCtl, CallbackFunctionDelegate CallbackFunction);

        /// <summary>
        /// BVUClose();
        /// </summary>
        [DllImport(DllName, EntryPoint = "BVUClose", CallingConvention = CallingConvention.StdCall)]
        public static extern int BVUClose();

        /// <summary>
        /// BVURequestStack();
        /// </summary>
        [DllImport(DllName, EntryPoint = "BVURequestStack", CallingConvention = CallingConvention.StdCall)]
        public static extern int BVURequestStack();


    }


    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    public delegate void CallbackFunctionDelegate([In, Out] ref BVUControl pCtl, int iEvent);

只需提一下,“票据接受器”会亮起,但代码不会执行。因此,没有书写错误,只是功能问题。

0 个答案:

没有答案