Ejemplo n.º 1
0
    public void ConfigureContinuousScan(ANT_ReferenceLibrary.ChannelType channelType, byte radioFreq, ushort USBNum)
    {
        userChannel  = 0;
        RXQueue      = new Queue <byte[]>(16);
        messageQueue = new Queue <ANT_Response>(16);
        device       = AntManager.Instance.devices[USBNum];
        device.enableRxExtendedMessages(true, 500);
        channel = device.getChannel(0);
        channel.channelResponse += new dChannelResponseHandler(ChannelResponse);

        channel.assignChannelExt(ANT_ReferenceLibrary.ChannelType.ADV_TxRx_Only_or_RxAlwaysWildCard_0x40, 0, ANT_ReferenceLibrary.ChannelTypeExtended.ADV_AlwaysSearch_0x01, 500);
        channel.setChannelID(0, false, 0, 0, 500);
        channel.setChannelFreq(radioFreq, 500);
        channel.setChannelSearchTimeout(0);
        channel.setLowPrioritySearchTimeout((byte)0xFF);
        isBackgroundScan = true;
        channel.openChannel();
        device.openRxScanMode();
        broadcasting = true;
    }
Ejemplo n.º 2
0
        private void setupAndOpenScan(ANT_Device deviceToSetup, ANT_ReferenceLibrary.ChannelType channelType)
        {
            //We try-catch and forward exceptions to the calling function to handle and pass the errors to the user
            try
            {
                if (!deviceToSetup.setNetworkKey(0, USER_NETWORK_KEY, 500))
                {
//                    threadSafePrintLine("Network Key set to ... well, you know,  on net 0.", textBox_Display);
//                else
                    throw new Exception("Channel assignment operation failed.");
                }


                //To access an ANTChannel on a paticular device we need to get the channel from the device
                //Once again, this ensures you have a valid object associated with a real-world ANTChannel
                //ie: You can only get channels that actually exist
                ANT_Channel channel0 = deviceToSetup.getChannel(0);

                //Almost all functions in the library have two overloads, one with a response wait time and one without
                //If you give a wait time, you can check the return value for success or failure of the command, however
                //the wait time version is blocking. 500ms is usually a safe value to ensure you wait long enough for any response.
                //But with no wait time, the command is simply sent and you have to monitor the device response for success or failure.

                //To setup channels for communication there are three mandatory operations assign, setID, and Open
                //Various other settings such as message period and network key affect communication
                //between two channels as well, see the documentation for further details on these functions.

                //So, first we assign the channel, we have already been passed the channelType which is an enum that has various flags
                //If we were doing something more advanced we could use a bitwise or ie:base|adv1|adv2 here too
                //We also use net 0 which has the public network key by default
                if (!channel0.assignChannel(channelType, 0, 500))
                {
//                    threadSafePrintLine("Ch assigned to " + channelType + " on net 0.", textBox_Display);
//                else
                    throw new Exception("Channel assignment operation failed.");
                }

                //Next we have to set the channel id. Slaves will only communicate with a master device that
                //has the same id unless one or more of the id parameters are set to a wild card 0. If wild cards are included
                //the slave will search until it finds a broadcast that matches all the non-wild card parameters in the id.
                //For now we pick an arbitrary id so that we can ensure we match between the two devices.
                //The pairing bit ensures on a search that you only pair with devices that also are requesting
                //pairing, but we don't need it here so we set it to false
                if (!channel0.setChannelID(USER_DEVICENUM, false, USER_DEVICETYPE, USER_TRANSTYPE, 500))
                {
//                    threadSafePrintLine("Set channel ID to " + USER_DEVICENUM + " " + USER_DEVICETYPE + " " + USER_TRANSTYPE, textBox_Display);
//                else
                    throw new Exception("Set Channel ID operation failed.");
                }

                //Setting the channel period isn't mandatory, but we set it slower than the default period so messages aren't coming so fast
                //The period parameter is divided by 32768 to set the period of a message in seconds. So here, 16384/32768 = 1/2 sec/msg = 2Hz
                if (!channel0.setChannelPeriod(USER_CHANNELPERIOD, 500))
                {
//                    threadSafePrintLine("Message Period set to " + USER_CHANNELPERIOD + "/32768 seconds per message", textBox_Display);
//                else
                    throw new Exception("Set Channel Period Op Faildd.");
                }

                if (!channel0.setChannelFreq(USER_RADIOFREQ, 500))
                {
//                    threadSafePrintLine("Message Radio Freq set to +" + USER_RADIOFREQ, textBox_Display);
//                else
                    throw new Exception("Set Radio Freq failed.");
                }

                if (!deviceToSetup.enableRxExtendedMessages(true, 500))
                {
//                    threadSafePrintLine("Requesting Extended Messages", textBox_Display);
//                else
                    throw new Exception("Extenned Message Request Failed.");
                }



                //Now we open the channel
                if (!deviceToSetup.openRxScanMode(500))
                {
//                    threadSafePrintLine("Opened Device in Scan mode" + Environment.NewLine, textBox_Display);
//                else
                    throw new Exception("Channel Open operation failed.");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Setup and Open Failed. " + ex.Message + Environment.NewLine);
            }
        }