Esempio n. 1
0
        private void startChannel()
        {
            //Just try this in case we get an active channel for some reason
            channel.closeChannel(500);
            channel.unassignChannel(500);

            if (!channel.assignChannel(ANT_ReferenceLibrary.ChannelType.BASE_Master_Transmit_0x10, 0, 500))
            {
                statusReport = "Failed to start, assign() failed";
            }
            else if (!channel.setChannelID((ushort)deviceNum, false, 9, 5, 500))
            {
                statusReport = "Failed to start, setChannelId() failed";
            }
            else if (!channel.setChannelFreq(57, 500))
            {
                statusReport = "Failed to start, setChannelFreq() failed";
            }
            else if (!channel.setChannelPeriod(32768, 500))
            {
                statusReport = "Failed to start, setChannelPeriod() failed";
            }
            else if (!channel.setChannelTransmitPower(ANT_ReferenceLibrary.TransmitPower.RADIO_TX_POWER_0DB_0x03, 500))
            {
                statusReport = "Failed to start, setChannelTransmitPower() failed";
            }
            else if (!channel.openChannel(500))
            {
                statusReport = "Failed to start, openChannel() failed";
            }
            else
            {
                statusReport = "Remote Control Channel is open";
            }
        }
Esempio n. 2
0
 public void Close()
 {
     if (!isClosed)
     {
         isBroadcasting = false;
         channel.closeChannel();
     }
 }
        /// <summary>
        /// Handle ANT-FS response events
        /// </summary>
        public void HandleClientResponses(ANTFS_ClientChannel.Response response)
        {
            Console.WriteLine(Print.AsString(response));   // Display response
            switch (response)
            {
            case ANTFS_ClientChannel.Response.ConnectionLost:
            case ANTFS_ClientChannel.Response.DisconnectPass:
                // Close the channel
                if (antfsClient.GetDisconnectParameters().CommandType == (byte)DisconnectType.Broadcast)
                {
                    channel0.closeChannel();
                }
                else
                {
                    antfsClient.CloseBeacon();
                }
                break;

            case ANTFS_ClientChannel.Response.BeaconClosed:
                demoDone = true;
                Console.WriteLine("Press enter to exit");
                break;

            case ANTFS_ClientChannel.Response.PairingRequest:
                Console.WriteLine("Pairing request from " + antfsClient.GetHostName());
                antfsClient.SendPairingResponse(true);
                break;

            case ANTFS_ClientChannel.Response.DownloadRequest:
                try
                {
                    ushort index = antfsClient.GetRequestedFileIndex();
                    if (index == 0)
                    {
                        antfsClient.SendDownloadResponse(DownloadResponse.Ok, index, dirFS.ToByteArray());
                    }
                    else
                    {
                        antfsClient.SendDownloadResponse(DownloadResponse.Ok, index, ReadFile(dirFS.GetFileSize(index)));
                    }
                }
                catch (ANTFS_Exception)
                {
                    antfsClient.SendDownloadResponse(DownloadResponse.InvalidIndex, 0, null);
                }
                break;

            default:
                break;
            }
        }
Esempio n. 4
0
    public void Close()
    {
        if (channel != null)
        {
            channel.closeChannel();
            channel.Dispose();
        }
        if (isBackgroundScan)
        {
            device.enableRxExtendedMessages(false, 500);
        }

        broadcasting = false;
        AntManager.Instance.channelList.Remove(this);
        Destroy(this);
    }
Esempio n. 5
0
 private void CloseAllChannles()
 {
     if (channel0 != null)
     {
         Console.WriteLine("Closing Channel");
         channel0.closeChannel();
         channel0.Dispose();
         channel0 = null;
     }
     if (device0 != null)
     {
         device0.Dispose();
         device0 = null;
     }
     //dispose time and wait object and semaphore
 }
Esempio n. 6
0
 static void ReleaseUSBDongle()
 {
     WriteLog("Releasing USB ANT Dongle...");
     try
     {
         channel0.closeChannel();
         System.Threading.Thread.Sleep(1000);
         device0.ResetSystem();
         device0.ResetUSB();
         ANT_Device.shutdownDeviceInstance(ref device0);
     }
     catch (Exception ex)
     {
         WriteLog("USB ANT Dongle is already released !");
     }
 }
Esempio n. 7
0
 public void Close()
 {
     if (channel != null)
     {
         channel.closeChannel();
         channel.Dispose();
     }
     if (isBackgroundScan)
     {
         device.enableRxExtendedMessages(false, 500);
         isBackgroundScan = false;
     }
     broadcasting = false;
     AntManager.Instance.channelList.Remove(this);
     AntManager.Instance.channelIDUsed[device.getOpenedUSBDeviceNum(), userChannel] = false;
     Destroy(this);
 }
Esempio n. 8
0
        public void Stop()
        {
            // Nothing to stop if not started or already finished.
            if (state == AntState.NotStarted || state == AntState.Finished)
            {
                return;
            }

            //if (state == AntState.StartFail || state == AntState.ConnectFail)
            //    return;

            if (this.channel != null)
            {
                try
                {
                    ANT_Channel tempChannel = this.channel;
                    this.channel = null;
                    tempChannel.closeChannel(RESPONSE_WAIT_TIME);
                    tempChannel.Dispose();
                }
                catch { }
            }

            if (this.device != null)
            {
                try
                {
                    ANT_Device tempDevice = this.device;
                    this.device = null;

                    // We use a temp var here because this Dispose method is a little strange...
                    tempDevice.Dispose();
                }
                catch { }
            }

            UpdateState(AntState.Finished);
        }
Esempio n. 9
0
        ////////////////////////////////////////////////////////////////////////////////
        // Start
        //
        // Start the demo program.
        //
        // ucChannelType_:  ANT Channel Type. 0 = Master, 1 = Slave
        //                  If not specified, 2 is passed in as invalid.
        ////////////////////////////////////////////////////////////////////////////////
        static void Start(byte ucChannelType_)
        {
            byte ucChannelType = ucChannelType_;

            bDone         = false;
            bDisplay      = true;
            bBroadcasting = false;

            PrintMenu();

            // If a channel type has not been set at the command line,
            // prompt the user to specify one now
            do
            {
                if (ucChannelType == CHANNEL_TYPE_INVALID)
                {
                    Console.WriteLine("Channel Type? (Master = 0, Slave = 1)");
                    try
                    {
                        ucChannelType = byte.Parse(Console.ReadLine());
                    }
                    catch (Exception)
                    {
                        ucChannelType = CHANNEL_TYPE_INVALID;
                    }
                }

                if (ucChannelType == 0)
                {
                    channelType = ANT_ReferenceLibrary.ChannelType.BASE_Master_Transmit_0x10;
                }
                else if (ucChannelType == 1)
                {
                    channelType = ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00;
                }
                else
                {
                    ucChannelType = CHANNEL_TYPE_INVALID;
                    Console.WriteLine("Error: Invalid channel type");
                }
            } while (ucChannelType == CHANNEL_TYPE_INVALID);

            try
            {
                ConfigureANT();

                while (!bDone)
                {
                    string command = Console.ReadLine();
                    switch (command)
                    {
                    case "M":
                    case "m":
                    {
                        PrintMenu();
                        break;
                    }

                    case "Q":
                    case "q":
                    {
                        // Quit
                        Console.WriteLine("Closing Channel");
                        bBroadcasting = false;
                        channel0.closeChannel();
                        break;
                    }

                    case "A":
                    case "a":
                    {
                        // Send Acknowledged Data
                        byte[] myTxBuffer = { 1, 2, 3, 4, 5, 6, 7, 8 };
                        channel0.sendAcknowledgedData(myTxBuffer);
                        break;
                    }

                    case "B":
                    case "b":
                    {
                        // Send Burst Data (10 packets)
                        byte[] myTxBuffer = new byte[8 * 10];
                        for (byte i = 0; i < 8 * 10; i++)
                        {
                            myTxBuffer[i] = i;
                        }
                        channel0.sendBurstTransfer(myTxBuffer);
                        break;
                    }

                    case "R":
                    case "r":
                    {
                        // Reset the system and start over the test
                        ConfigureANT();
                        break;
                    }

                    case "C":
                    case "c":
                    {
                        // Request capabilities
                        ANT_DeviceCapabilities devCapab = device0.getDeviceCapabilities(500);
                        Console.Write(devCapab.printCapabilities() + Environment.NewLine);
                        break;
                    }

                    case "V":
                    case "v":
                    {
                        // Request version
                        // As this is not available in all ANT parts, we should not wait for a response, so
                        // we do not specify a timeout
                        // The response - if available - will be processed in DeviceResponse
                        device0.requestMessage(ANT_ReferenceLibrary.RequestMessageID.VERSION_0x3E);
                        break;
                    }

                    case "S":
                    case "s":
                    {
                        // Request channel status
                        ANT_ChannelStatus chStatus = channel0.requestStatus(500);

                        string[] allStatus = { "STATUS_UNASSIGNED_CHANNEL",
                                               "STATUS_ASSIGNED_CHANNEL",
                                               "STATUS_SEARCHING_CHANNEL",
                                               "STATUS_TRACKING_CHANNEL" };
                        Console.WriteLine("STATUS: " + allStatus[(int)chStatus.BasicStatus]);
                        break;
                    }

                    case "I":
                    case "i":
                    {
                        // Request channel ID
                        ANT_Response respChID           = device0.requestMessageAndResponse(ANT_ReferenceLibrary.RequestMessageID.CHANNEL_ID_0x51, 500);
                        ushort       usDeviceNumber     = (ushort)((respChID.messageContents[2] << 8) + respChID.messageContents[1]);
                        byte         ucDeviceType       = respChID.messageContents[3];
                        byte         ucTransmissionType = respChID.messageContents[4];
                        Console.WriteLine("CHANNEL ID: (" + usDeviceNumber.ToString() + "," + ucDeviceType.ToString() + "," + ucTransmissionType.ToString() + ")");
                        break;
                    }

                    case "D":
                    case "d":
                    {
                        bDisplay = !bDisplay;
                        break;
                    }

                    case "U":
                    case "u":
                    {
                        // Print out information about the device we are connected to
                        Console.WriteLine("USB Device Description");

                        // Retrieve info
                        Console.WriteLine(String.Format("   VID: 0x{0:x}", device0.getDeviceUSBVID()));
                        Console.WriteLine(String.Format("   PID: 0x{0:x}", device0.getDeviceUSBPID()));
                        Console.WriteLine(String.Format("   Product Description: {0}", device0.getDeviceUSBInfo().printProductDescription()));
                        Console.WriteLine(String.Format("   Serial String: {0}", device0.getDeviceUSBInfo().printSerialString()));
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                    System.Threading.Thread.Sleep(0);
                }
                // Clean up ANT
                Console.WriteLine("Disconnecting module...");
                ANT_Device.shutdownDeviceInstance(ref device0);  // Close down the device completely and completely shut down all communication
                Console.WriteLine("Demo has completed successfully!");
                return;
            }
            catch (Exception ex)
            {
                throw new Exception("Demo failed: " + ex.Message + Environment.NewLine);
            }
        }
Esempio n. 10
0
        void startNextSearch()
        {
            if (searchChannel != null || antStick == null)  //Check if device is present and the channel is valid
            {
                //Ensure we are still connected
                try
                {
                    searchChannel.requestStatus(1000); //Check if we get an exception...means we are disconnected, otherwise continue
                }
                catch (Exception)
                {
                    try
                    {
                        //We get to this code almost always because the device is dead, so try to restart it
                        ANT_Device.shutdownDeviceInstance(ref antStick);
                        searchChannel = null;
                        foreach (AntPlus_Connection i in deviceList)
                        {
                            i.connectedChannel = null;
                        }

                        findUsableAntDevice();
                        //Now fall through and attempt to restart search
                    }
                    catch (Exception)
                    {
                        //System.Windows.MessageBox.Show("Opening Device Failed. Try removing then re-inserting the stick, then try again.");
                        return;
                    }
                }
            }   //end check if device and search channel (if there is one) are valid

            //Check if we still need to search or have all the equipment already
            List <int> usedChannels = new List <int>();

            foreach (AntPlus_Connection i in deviceList)
            {
                switch (i.getConnStatus())
                {
                case AntPlus_Connection.ConnState.Closed:
                case AntPlus_Connection.ConnState.Searching:
                    i.setConnStatus(AntPlus_Connection.ConnState.InSrchQueue);
                    break;

                case AntPlus_Connection.ConnState.InSrchQueue:
                    break;

                case AntPlus_Connection.ConnState.Connected:
                case AntPlus_Connection.ConnState.DrpdToSrch:
                    usedChannels.Add(i.connectedChannel.getChannelNum());
                    break;
                }
            }

            if (usedChannels.Count == deviceList.Count)
            {
                return;     //we have all the equipment already
            }
            //Get new search channel if neccesary
            if (searchChannel == null)
            {
                if (usedChannels.Count >= numChannelsForDevices)
                {
                    return;     //no free channels
                }
                //Find the first free channel and start the search
                for (int i = 0; i < numChannelsForDevices; ++i)
                {
                    if (!usedChannels.Contains(i))
                    {
                        searchChannel = antStick.getChannel(i);
                        searchChannel.channelResponse += new dChannelResponseHandler(antChannel_channelResponse_FeSearch);
                        break;
                    }
                }
            }

            //Search for a search period for given device parameters
            //Find the next device to search for

            while (true)  //We know there is at least one device we need to search for, because of the check above, so this will never loop infinitely
            {
                ++searchingDeviceIndex;
                if (searchingDeviceIndex >= deviceList.Count)
                {
                    searchingDeviceIndex = 0;
                }
                if (deviceList[searchingDeviceIndex].connectedChannel == null)
                {
                    break;
                }
            }

            //Now set the channel parameters to start the next search
            try
            {
                if (searchChannel == null)
                {
                    throw new ApplicationException("Couldn't allocate a channel for search");
                }

                ds_AntPlus.AntChannelProfile srch = deviceList[searchingDeviceIndex].dataSource.searchProfile;
                deviceList[searchingDeviceIndex].setConnStatus(AntPlus_Connection.ConnState.Searching);

                if (!searchChannel.assignChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, 0, 500))
                {
                    //Usually because the channel is in wrong state
                    searchChannel.closeChannel(500);
                    searchChannel.unassignChannel(500);
                    if (!searchChannel.assignChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, 0, 500))
                    {
                        throw new ApplicationException("Failed to assign channel");
                    }
                }

                //Handle setting the search timeout
                byte timeout = 4; //default 4*2.5=10 seconds for each device
                if (deviceList.Count - usedChannels.Count == 1)
                {
                    timeout = 255;  //search forever if we only have one device to find; If one of the other devices resets it will startNextSearch again so we won't get stuck
                }
                if (!searchChannel.setLowPrioritySearchTimeout(timeout, 500))
                {
                    throw new ApplicationException("Failed to set low-pri search timeout");
                }

                if (!searchChannel.setChannelSearchTimeout(0, 500))
                {
                    throw new ApplicationException("Failed to set search timeout");
                }

                if (!searchChannel.setChannelFreq(srch.rfOffset, 500))
                {
                    throw new ApplicationException("Failed to set channel frequency");
                }

                if (!searchChannel.setChannelPeriod(srch.messagePeriod, 500))
                {
                    throw new ApplicationException("Failed to set channel period");
                }

                if (!searchChannel.setChannelID(srch.deviceNumber, srch.pairingEnabled, srch.deviceType, srch.transType, 500))
                {
                    throw new ApplicationException("Failed to set channel ID");
                }

                if (!searchChannel.openChannel(500))
                {
                    throw new ApplicationException("Failed to open channel");
                }
            }
            catch (Exception ex)
            {
                //System.Windows.MessageBox.Show("Search Channel Open Failed: " + ex.Message +". If you still need to connect other fitness equipment, you may need to restart the application.");
            }
        }
        static void Start()
        {
            bDone = false;

            PrintMenu();

            try
            {
                ConfigureANT();

                while (!bDone)
                {
                    string command = Console.ReadLine();
                    switch (command)
                    {
                    case "M":
                    case "m":
                    {
                        PrintMenu();
                        break;
                    }

                    case "Q":
                    case "q":
                    {
                        // Quit
                        Console.WriteLine("Closing Channel");
                        channel0.closeChannel();
                        break;
                    }

                    case "A":
                    case "a":
                    {
                        byte[] myTxBuffer = { 1, 2, 3, 4, 5, 6, 7, 8 };
                        channel0.sendAcknowledgedData(myTxBuffer);
                        break;
                    }

                    case "B":
                    case "b":
                    {
                        byte[] myTxBuffer = new byte[8 * 10];
                        for (byte i = 0; i < 8 * 10; i++)
                        {
                            myTxBuffer[i] = i;
                        }
                        channel0.sendBurstTransfer(myTxBuffer);
                        break;
                    }

                    case "C":
                    case "c":
                    {
                        ANT_DeviceCapabilities devCapab0 = device0.getDeviceCapabilities(500);
                        Console.Write(devCapab0.printCapabilities() + Environment.NewLine);
                        break;
                    }

                    case "V":
                    case "v":
                    {
                        device0.requestMessage(ANT_ReferenceLibrary.RequestMessageID.VERSION_0x3E);
                        break;
                    }

                    case "I":
                    case "i":
                    {
                        ANT_Response respChID0           = device0.requestMessageAndResponse(ANT_ReferenceLibrary.RequestMessageID.CHANNEL_ID_0x51, 500);
                        ushort       usDeviceNumber0     = (ushort)((respChID0.messageContents[2] << 8) + respChID0.messageContents[1]);
                        byte         ucDeviceType0       = respChID0.messageContents[3];
                        byte         ucTransmissionType0 = respChID0.messageContents[4];
                        Console.WriteLine("CHANNEL ID: (" + usDeviceNumber0.ToString() + "," + ucDeviceType0.ToString() + "," + ucTransmissionType0.ToString() + ")");
                        ANT_Response respChID1           = device0.requestMessageAndResponse(ANT_ReferenceLibrary.RequestMessageID.CHANNEL_ID_0x51, 500);
                        ushort       usDeviceNumber1     = (ushort)((respChID1.messageContents[2] << 8) + respChID1.messageContents[1]);
                        byte         ucDeviceType1       = respChID1.messageContents[3];
                        byte         ucTransmissionType1 = respChID1.messageContents[4];
                        Console.WriteLine("CHANNEL ID: (" + usDeviceNumber1.ToString() + "," + ucDeviceType1.ToString() + "," + ucTransmissionType1.ToString() + ")");
                        break;
                    }

                    case "U":
                    case "u":
                    {
                        Console.WriteLine("USB Device Description");
                        Console.WriteLine(String.Format("   VID: 0x{0:x}", device0.getDeviceUSBVID()));
                        Console.WriteLine(String.Format("   PID: 0x{0:x}", device0.getDeviceUSBPID()));
                        Console.WriteLine(String.Format("   Product Description: {0}", device0.getDeviceUSBInfo().printProductDescription()));
                        Console.WriteLine(String.Format("   Serial String: {0}", device0.getDeviceUSBInfo().printSerialString()));
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                    System.Threading.Thread.Sleep(0);
                }
                Console.WriteLine("Disconnecting module...");
                ANT_Device.shutdownDeviceInstance(ref device0);
                return;
            }
            catch (Exception ex)
            {
                throw new Exception("Demo failed: " + ex.Message + Environment.NewLine);
            }
        }
Esempio n. 12
0
 private void OnDisable()
 {
     _channel?.closeChannel();
 }