示例#1
0
        /// <summary>
        /// Constructs <see cref="AntChannel"/>.
        /// </summary>
        /// <param name="channel">Underlying <see cref="ANT_Channel"/> object from official ANT managed API.</param>
        internal AntChannel(ANT_Channel channel)
        {
            this.channel = channel;

            // todo: for now assigned to network 0 and channel type BASE_Slave_Receive_0x00 automatically, other hardcoded defaults
            channel.assignChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, 0);
            channel.setChannelSearchTimeout(255);
        }
示例#2
0
文件: ANT_Device.cs 项目: Cycli/Cycli
        /// <summary>
        /// returns the next clear channel, or null if none available
        /// </summary>
        public ANT_Channel getNextChannel()
        {
            if (!initializedUSB)
                throw new ObjectDisposedException("ANTDevice object has been disposed");

            int num = 0;
            while (antChannels[num] != null && num < antChannels.Length)
            {
                num++;
            }

            if (num < antChannels.Length)
            {
                antChannels[num] = new ANT_Channel(this, (byte)num);
            }
            else
            {
                return null;
            }
            return antChannels[num];
        }
示例#3
0
文件: ANT_Device.cs 项目: Cycli/Cycli
        /// <summary>
        /// Returns the requested ANTChannel or throws an exception if channel doesn't exist.
        /// </summary>
        /// <param name="num">Channel number requested</param>
        public ANT_Channel getChannel(int num)
        {
            if (!initializedUSB)
                throw new ObjectDisposedException("ANTDevice object has been disposed");

            if(num > antChannels.Length-1 || num < 0)
                throw new ANT_Exception("Channel number invalid");

            if(antChannels[num] == null)
                antChannels[num] = new ANT_Channel(this, (byte)num);

            return antChannels[num];
        }
示例#4
0
        public void Search()
        {
            if (Status == SensorStatus.Disconnected)
              {
            try
            {

              _Channel = Device.ANT_Device.getChannel(_Sensor.ChannelNumber);    // Get channel from ANT device
              _Channel.channelResponse += new dChannelResponseHandler(ChannelResponse);  // Add channel response function to receive channel event messages
            }
            catch (Exception ex)
            {
              if (Device.ANT_Device == null)    // Unable to connect to ANT
              {
                throw new Exception("Could not connect to any device.\n" +
                "Details: \n   " + ex.Message);
              }
              else
              {
                throw new Exception("Error connecting to ANT: " + ex.Message);
              }
            }

            Console.WriteLine("Assigning channel...");
            if (_Channel.assignChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, Device.NetworkNumber, 500))
              Console.WriteLine("Channel assigned");
            else
              throw new Exception("Error assigning channel");

            Console.WriteLine("Setting Channel ID...");
            if (_Channel.setChannelID(_Sensor.DeviceNumber, false, _Sensor.DeviceType, _Sensor.TransmissionType, 500))  // Not using pairing bit
              Console.WriteLine("Channel ID set");
            else
              throw new Exception("Error configuring Channel ID");

            Console.WriteLine("Setting Channel Period...");
            if (_Channel.setChannelPeriod(_Sensor.Period, 500))
              Console.WriteLine("Channel Period set");
            else
              throw new Exception("Error configuring Channel Period");

            Console.WriteLine("Setting Radio Frequency...");
            if (_Channel.setChannelFreq(_Sensor.RadioFrequency, 500))
              Console.WriteLine("Radio Frequency set");
            else
              throw new Exception("Error configuring Radio Frequency");

            Console.WriteLine("Opening channel...");
            if (_Channel.openChannel(500))
            {
              Console.WriteLine("Channel opened");
              Status = SensorStatus.Searching;
            }
            else
            {
              throw new Exception("Error opening channel");
            }

            #if (ENABLE_EXTENDED_MESSAGES)
            // Extended messages are not supported in all ANT devices, so
            // we will not wait for the response here, and instead will monitor
            // the protocol events
            Console.WriteLine("Enabling extended messages...");
            device0.enableRxExtendedMessages(true);
            #endif
              }
        }
示例#5
0
        private bool OpenGarmin2()
        {

            //Initialise the ANT library and connect to ANT module
            if (device != null)
            {
                ANT_Device.shutdownDeviceInstance(ref device);
                device = null;
            }

            device = new ANT_Device();         

            if (device == null)
            {
                MessageBox.Show("Error initialising ANT module. Ensure the Garmin ANT agent is not running.");
                return false;
            }
            device.ResetSystem();

            channel = device.getChannel(0);
            
            //Reset wireless transceiver
            device.ResetSystem();
            Thread.Sleep(50);

            //Pass the callback functions to the ANT_DLL library
            device.deviceResponse += new ANT_Device.DeviceResponseHandler(DeviceResponse);
            channel.channelResponse += new ANT_Channel.ChannelResponseHandler(ChannelResponse);

            //Set network key for Garmin HRM
            //The garmin HRM key is "B9A521FBBD72C345"
            byte[] GarminKey = { 0xb9, 0xa5, 0x21, 0xfb, 0xbd, 0x72, 0xc3, 0x45 };
            device.setNetworkKey(0, GarminKey);
            Thread.Sleep(50);

            //Assign the channel
            //Receive on channel 0, network #0
            channel.assignChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, 0);

            //Congifure Channel ID - set up which devices to transmit-receive data from
            //Set to receive from any device it finds
            ushort device_id = ushort.Parse(txtDeviceID.Text);
            channel.setChannelID((ushort)device_id, false, 0, 0);
            Thread.Sleep(50);

            //Set the receiver search timeout limit
            channel.setChannelSearchTimeout(0xff);
            Thread.Sleep(50);

            //Set the messaging period (corresponding to the max number of messages per second)
            //Messaging period for Garmin HRM is 0x1f86
            channel.setChannelPeriod(0x1f86);
            Thread.Sleep(50);

            //Set the radio frequency corresponding to the Garmin watch (frequency 0x39)
            channel.setChannelFreq(0x39);
            Thread.Sleep(50);

            //Open the channel to receive data !
            channel.openChannel();

            return true;
        }
示例#6
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);
        }
示例#7
0
        public void Start()
        {
            if (state != AntState.NotStarted)
            {
                Debug.LogWarningFormat("[AntStick] Start called a second time (ignored).");
                return;
            }

            UpdateState(AntState.Starting);

            ushort deviceId = DEVICE_ID;

            try
            {
                string line = null;
                StreamReader reader = new StreamReader(ANT_DEVICE_ID_FILE_PATH, Encoding.Default);
                using (reader)
                {
                    line = reader.ReadLine();
                    reader.Close();
                }

                if (line == null)
                {
                    Debug.LogWarningFormat("[AntStick] Could not get Ant Device ID from {0}. File exists but is empty.", ANT_DEVICE_ID_FILE_PATH);
                }
                else
                {
                    deviceId = UInt16.Parse(line);
                }
            }
            catch (FileNotFoundException ex)
            {
                Debug.LogWarningFormat("[AntStick] Could not get Ant Device ID from {0}. File not found. {1}", ANT_DEVICE_ID_FILE_PATH, ex.Message);
            }
            catch(FormatException ex)
            {
                Debug.LogWarningFormat("[AntStick] Could not get Ant Device ID from {0}. Could not parse first line as ushort. {1}", ANT_DEVICE_ID_FILE_PATH, ex.Message);
            }
            catch (Exception ex)
            {
                Debug.LogWarningFormat("[AntStick] Could not get Ant Device ID from {0}. Exception occurred. {1}", ANT_DEVICE_ID_FILE_PATH, ex.Message);
            }

            Debug.LogFormat("[AntStick] Using Device ID {0}.", deviceId);

            Stats = new AntStats();

            try
            {
                device = new ANT_Device();
            }
            catch (ANT_Exception ex)
            {
                Debug.LogWarningFormat("[AntStick] Could not open device (perhaps something else is using it?).\n{0}", ex.Message);
                UpdateState(AntState.StartFail);
                Stop();
                return;
            }

            try
            {
                channel = device.getChannel(CHANNEL);
            }
            catch (ANT_Exception ex)
            {
                Debug.LogWarningFormat("[AntStick] Could not get channel {0}.\n{1}", CHANNEL, ex.Message);
                UpdateState(AntState.StartFail);
                Stop();
                return;
            }

            device.deviceResponse += new ANT_Device.dDeviceResponseHandler(DeviceResponse);
            channel.channelResponse += new dChannelResponseHandler(ChannelResponse);

            try
            {
                if (!device.setNetworkKey(NETWORK_NUMBER, NETWORK_KEY, RESPONSE_WAIT_TIME))
                {
                    Debug.LogWarning("[AntStick] Failed to set network key.");
                    UpdateState(AntState.StartFail);
                    Stop();
                    return;
                }

                if (!channel.assignChannel(CHANNEL_TYPE, NETWORK_NUMBER, RESPONSE_WAIT_TIME))
                {
                    Debug.LogWarning("[AntStick] Failed to assign channel.");
                    UpdateState(AntState.StartFail);
                    Stop();
                    return;
                }

                if (!channel.setChannelID(deviceId, PAIRING_ENABLED, DEVICE_TYPE, TRANSMISSION_TYPE, RESPONSE_WAIT_TIME))
                {
                    Debug.LogWarning("[AntStick] Failed to set channel Id.");
                    UpdateState(AntState.StartFail);
                    Stop();
                    return;
                }

                if (!channel.setChannelPeriod(CHANNEL_PERIOD, RESPONSE_WAIT_TIME))
                {
                    Debug.LogWarning("[AntStick] Failed to set channel period.");
                    UpdateState(AntState.StartFail);
                    Stop();
                    return;
                }

                if (!channel.setChannelFreq(CHANNEL_FREQUENCY, RESPONSE_WAIT_TIME))
                {
                    Debug.LogWarning("[AntStick] Failed to set channel frequency.");
                    UpdateState(AntState.StartFail);
                    Stop();
                    return;
                }

                if (!channel.openChannel(RESPONSE_WAIT_TIME))
                {
                    Debug.LogWarning("[AntStick] Failed to open the channel.");
                    UpdateState(AntState.StartFail);
                    Stop();
                    return;
                }

            }
            catch (ANT_Exception ex)
            {
                Debug.LogWarningFormat("[AntStick] Could not configure channel.\n{0}", ex.Message);
                UpdateState(AntState.StartFail);
                Stop();
                return;
            }

            StartConnectTimeout();
        }
示例#8
0
 ////////////////////////////////////////////////////////////////////////////////
 // Init
 //
 // Initialize demo parameters.
 //
 ////////////////////////////////////////////////////////////////////////////////
 void Init()
 {
     try
     {
         Console.WriteLine("Attempting to connect to an ANT USB device...");
         device0 = new ANT_Device();   // Create a device instance using the automatic constructor (automatic detection of USB device number and baud rate)
         device0.deviceResponse += new ANT_Device.dDeviceResponseHandler(DeviceResponse);    // Add device response function to receive protocol event messages
         channel0 = device0.getChannel(user_ant_channel);    // Get channel from ANT device
         channel0.channelResponse += new dChannelResponseHandler(ChannelResponse);  // Add channel response function to receive channel event messages
         Console.WriteLine("Initialization was successful!");
     }
     catch (Exception ex)
     {
         if (device0 == null)    // Unable to connect to ANT
         {
             throw new Exception("Could not connect to any device.\n" + 
             "Details: \n   " + ex.Message);
         }
         else
         {
             throw new Exception("Error connecting to ANT: " + ex.Message);
         }
     }
 }
示例#9
0
        static void Init()
        {
            try
            {
                Console.WriteLine("Attempting to connect to an ANT USB device...");
                device0 = new ANT_Device();   // Create a device instance using the automatic constructor (automatic detection of USB device number and baud rate)
                device0.deviceResponse += new ANT_Device.dDeviceResponseHandler(DeviceResponse);    // Add device response function to receive protocol event messages

                channel0 = device0.getChannel(USER_ANT_CHANNEL);    // Get channel from ANT device
                channel0.channelResponse += new dChannelResponseHandler(ChannelResponse);  // Add channel response function to receive channel event messages
                Console.WriteLine("Initialization was successful!");
            }
            catch (Exception ex)
            {
                //This part is likely to crash, but we want to catch that somewhere else
                //We keep the try catch anyway as a reminder.
                throw ex;
            }
        }
示例#10
0
 public AntDevice(ANT_Device device, AntChannels channel = AntChannels.Channel0, byte networkNum = 0)
 {
     _antDevice = device;
     _channel = _antDevice.getChannel((int)channel);
     NetworkNumber = networkNum;
 }