Пример #1
0
        /// <summary>
        /// Closes the communication port.
        /// </summary>
        /// <exception cref="CommPortException">
        /// Thrown when a port failure occurs.
        /// </exception>
        /// <example>
        /// <code>
        /// Communication comm = new Communication();
        /// comm.OpenPort("COM4:");
        /// comm.ClosePort();
        /// comm.Dispose();
        /// </code>
        /// </example>
        // Revision History
        // MM/DD/YY who Version Issue# Description
        // -------- --- ------- ------ ---------------------------------------
        // 01/31/08 mcm 1.0.x   N/A	Created
        // 09/04/08 AF                 Added compiler directive to disable Bluetooth
        //                             stuff if not CE.
        // 09/15/08 AF                 Replaced C177 with EZSP because the interface
        //                             names changed in the new version of the EZSP dlls.
        // 10/17/12 PGH 2.70.36        Replaced EZSP with C177App
        // 09/17/15 jrf 4.21.04 616082 Stops the beaconing when port is closed.
        public override void ClosePort()
        {
            //Port is closing, so we should stop beacons.
            StopIntermittentBeacons();

            C177App.LeaveNetwork();
            if (C177App != null && C177App.IsConnected)
            {
                C177App.Disconnect();
                C177App.StopLogging();
            }

#if (WindowsCE)
            if (false != m_blnUsingBluetooth)
            {
                BToothCe.BT_Deinit();
            }
#endif
            m_bConnected = false;
        }
Пример #2
0
        /// <summary>
        /// Uses the MDAPI interfaces to find the belt clip radio, join devices,
        /// and determine the virtual port to use.
        /// </summary>
        /// <param name="strPortName">the name of the port we read from the registry</param>
        /// <param name="strZBCName">the name of the belt clip radio</param>
        /// <returns>true if we got a virtual port; false, otherwise</returns>
        //  Revision History
        //  MM/DD/YY Who Version Issue#     Description
        //  -------- --- ------- ---------- -------------------------------------------
        //  08/29/08 AF                     Created
        //  09/04/08 AF                     Removed call to BT_SetDeviceInfoFolder().
        //
        public bool SetUpBluetooth(ref string strPortName, string strZBCName)
        {
            BT_ERROR result      = BT_ERROR.SUCCESS;
            bool     blnContinue = true;
            ushort   usCount     = 0;
            bool     blnResult   = false;

            //Initialize API - must be called prior to any other function call
            if (BToothCe.BT_Init())
            {
                //Set pin so that input dialog box won't be displayed
                BToothCe.BT_SetPin("0000");

                while (blnContinue && (2 > usCount))
                {
                    blnContinue = false;

                    // In a normal operation, we always try to map the device first
                    // assuming it's been paired.
                    result = BToothCe.BT_MapSerialPortEx(ref strPortName, BT_DEVICE.ZB);

                    if (result == BT_ERROR.SUCCESS || result == BT_ERROR.PORT_ALREADY_MAPPED)
                    {
                        blnResult = true;
                        m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                             "Bluetooth Setup: Port successfully mapped");
                    }
                    else if (result == BT_ERROR.SERVICE_NOT_SUPPORTED)
                    {
                        // critical - should never happen
                        m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                             "Bluetooth service NOT supported");
                    }
                    else if (result == BT_ERROR.OUT_OF_PORTS)
                    {
                        // critical - reboot the device (PDA) and try again
                        m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                             "Bluetooth Setup: No available ports found. Reboot the handheld device");
                    }
                    else if (result == BT_ERROR.FAILED_CREATE_PORT)
                    {
                        // unknown error - should never happen (but I've seen it)
                        m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                             "Bluetooth Setup: Failed to create port");
                    }
                    else if (result == BT_ERROR.DEVICE_NOT_PAIRED || result == BT_ERROR.DEVICE_NOT_FOUND)
                    {
                        MyBTDEV[] devs = new MyBTDEV[1];
                        devs[0] = new MyBTDEV();
                        byte[] abyDevs     = new byte[MyBTDEV.Length];
                        int    numElements = 1;

                        // Scan for available BT devices.
                        // NOTE: A time consuming function; so running in the main thread
                        // can lock up the interface until the function returns
                        BToothCe.BT_GetAvailableBTDevices(abyDevs, ref numElements, strZBCName);
                        if (1 == numElements)
                        {
                            //Retrieve the device information so that we can pair
                            devs = MyBTDEV.ToMyBTDEVArray(abyDevs, numElements);

                            uint uiLowAddress = (uint)(devs[0].addr);
                            uint uiHiAddress  = (uint)(devs[0].addr >> 32);
                            // Try to pair the device
                            if (BT_ERROR.SUCCESS == BToothCe.BT_PairDeviceEx(uiHiAddress, uiLowAddress))
                            {
                                blnContinue = true;
                                m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                                     "Bluetooth Setup: Devices successfully paired");
                            }

                            usCount++;
                        }
                        else
                        {
                            m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                                 "Could not discover the selected Belt Clip Radio");
                        }
                    }
                }

                m_blnUsingBluetooth = true;
            }
            else
            {
                BToothCe.BT_Deinit();
                m_Logger.WriteDetail(Logger.LoggingLevel.ZigBeeProtocol,
                                     "Bluetooth initialization failed!");
            }

            return(blnResult);
        }