IEnumerator Reconnect(uint serial) { Debug.Log("looking for usb device " + serial.ToString()); // polling to try and find the USB device while (true) { if (ANT_Common.getNumDetectedUSBDevices() > nDeviceConnected) { ANT_Device device = new ANT_Device(); if (device.getSerialNumber() == serial) { Debug.Log("usb found!"); AntManager.Instance.Reconnect(device); foreach (AntChannel channel in AntManager.Instance.channelList) { channel.ReOpen(device); } yield break; } else { device.Dispose(); } } yield return(new WaitForSeconds(0.1f)); } }
/// <summary> /// Configure the client beacon parameters, and start transmitting/beaconing /// </summary> /// <param name="antDevice">ANT USB device</param> public void ConfigureClient(ANT_Device antDevice) { // Configure ANT Channel parameters antfsClient.SetClientNetworkKey(0, Demo.NetworkKey); antfsClient.SetChannelID(Demo.DeviceType, Demo.TransmissionType); antfsClient.SetChannelPeriod(Demo.ChannelPeriod); // Setup client configuration parameters. // ANTFS_ClientParameters is a struct, so make sure to initialize ALL parameters ANTFS_ClientParameters clientConfig = new ANTFS_ClientParameters(); clientConfig.BeaconDeviceType = Demo.ClientDeviceType; clientConfig.BeaconManufacturerID = Demo.ClientManufacturerID; clientConfig.BeaconRadioFrequency = Demo.SearchRF; clientConfig.LinkPeriod = Demo.LinkPeriod; clientConfig.AuthenticationType = ClientAuthenticationType; clientConfig.IsDataAvailable = DataAvailable; clientConfig.IsPairingEnabled = PairingEnabled; clientConfig.IsUploadEnabled = UploadEnabled; clientConfig.BeaconTimeout = BeaconTimeout; clientConfig.PairingTimeout = PairingTimeout; clientConfig.SerialNumber = antDevice.getSerialNumber(); // Use the serial number of the USB stick to identify the client // Apply configuration antfsClient.Configure(clientConfig); // Configure friendly name and passkey (optional) antfsClient.SetFriendlyName(ClientFriendlyName); antfsClient.SetPassKey(PassKey); // Create directory, an add a single entry, of the configured size dirFS = new ANTFS_Directory(); ANTFS_Directory.Entry fileEntry; fileEntry.FileIndex = 1; fileEntry.FileSize = TestFileSize; fileEntry.FileDataType = 1; fileEntry.FileNumber = 1; fileEntry.FileSubType = 0; fileEntry.GeneralFlags = (byte)(ANTFS_Directory.GeneralFlags.Read | ANTFS_Directory.GeneralFlags.Write | ANTFS_Directory.GeneralFlags.Erase); fileEntry.SpecificFlags = 0; fileEntry.TimeStamp = 0; // TODO: Encode the timestamp properly dirFS.AddEntry(fileEntry); if (Demo.AntfsBroadcast) { // If we want to use ANT-FS broadcast mode, and start the channel in broadcast mode, // setup callback to handle responses for channel 0 while in broadcast mode channel0.channelResponse += new dChannelResponseHandler(HandleChannelResponses); // Configure the channel as a master, and look for the request message in the channel callback // to switch to ANT-FS mode if (!antDevice.setNetworkKey(Demo.NetworkNumber, Demo.NetworkKey, 500)) { throw new Exception("Error configuring network key"); } if (!channel0.assignChannel(ANT_ReferenceLibrary.ChannelType.BASE_Master_Transmit_0x10, Demo.NetworkNumber, 500)) { throw new Exception("Error assigning channel"); } if (!channel0.setChannelID((ushort)clientConfig.SerialNumber, false, Demo.DeviceType, Demo.TransmissionType, 500)) { throw new Exception("Error configuring Channel ID"); } if (!channel0.setChannelFreq(Demo.SearchRF, 500)) { throw new Exception("Error configuring radio frequency"); } if (!channel0.setChannelPeriod(Demo.ChannelPeriod, 500)) { throw new Exception("Error configuring channel period"); } if (!channel0.openChannel(500)) { throw new Exception("Error opening channel"); } } else { // If we want to start in ANT-FS mode, just open the beacon antfsClient.OpenBeacon(); } }
static async void Init() { try { WriteLog("Connecting to ANT USB Dongle..."); //device0 = new ANT_Device(); // Create a device instance using the automatic constructor (automatic detection of USB device number and baud rate) device0.serialError += new ANT_Device.dSerialErrorHandler(SerialError); 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 WriteLog("ANT USB Dongle initialization successful"); WriteLog("ANT USB Dongle Device Number: " + device0.getOpenedUSBDeviceNum()); WriteLog("ANT USB Dongle Serial Number: " + device0.getSerialNumber()); USER_DEVICENUM = (ushort)device0.getOpenedUSBDeviceNum(); bReset = false; List <Task> tasks = new List <Task>(); tasks.Add(Task.Factory.StartNew(() => { while (!bReset) { WriteLog("Checking ANT USB Dongle..."); Byte[] bytes = new byte[8]; try { if (channel0.sendBroadcastData(bytes)) { WriteLog("ANT USB Dongle is operationnal"); } else { WriteLog("ANT USB Dongle is not operationnal"); } } catch (Exception ex) { WriteLog("Problem with ANT USB Dongle..."); } System.Threading.Thread.Sleep(5000); } })); await Task.WhenAll(tasks); } catch (Exception ex) { Console.WriteLine("Exception: " + ex); } /* * if (device0 == null) // Unable to connect to ANT * { * throw new Exception("Could not connect to any ANT device.\n" + * "Details: \n " + ex.Message); * } * else * { * throw new Exception("Error connecting to ANT device: " + ex.Message); * } * } */ }