Welcome Guest,Register Now
Log In

ANT Forum

Welcome guest, please Login or Register

   

Detecting ANT+ usb stick

Avatar
RankRankRankRank

Total Posts: 123

Joined 2013-10-07

PM

Hi,

I would like my application to detect the first ANT+ usb stick from the computer.
Basically just a button press that would :
-scan all usb port (0..12) try with baud 57000 first, then 500000 stop when one usb key has been sucessfully init
I have tried the code below, but pclSerialObject->Init seems to always return true
Is there a convenience function that I'm missing? Is what I'm asking possible ? I would like to not modify ANT_LIB for future update so my code stay compatible.
Thank you!


for (int deviceNumber=0deviceNumber<12deviceNumber++) {

        printf
("Looking usb device on port : %d..."deviceNumber);

//        UCHAR nbDevice = pclSerialObject->GetNumberOfDevices();

        
bStatus pclSerialObject->Init(USER_BAUDRATE_USB2deviceNumber);
        if (
bStatus)  //always return true even with wrong deviceNumber..
            
break;
    
     

Signature

——————————————————
Free Indoor Cycling Software - https://maximumtrainer.com

Avatar
RankRankRankRank

Total Posts: 745

Joined 2012-09-14

PM

Assuming your "pclSerialObject" is still referencing an instance of the "DSISerialGeneric" class, that class does contain a convenience function called "AutoInit". It will automatically connect to any available ANT USB stick, although at the moment it only returns device number 0 for any devices it connects to.

There is also the ANT_Device() constructor in the ANT_Managed_Library which implements an automated USB device initialization scheme as another reference.      
Avatar
RankRankRankRank

Total Posts: 123

Joined 2013-10-07

PM

I need to learn to read source code!
AutoInit is all I need, thank you Mr. Harrison      

Signature

——————————————————
Free Indoor Cycling Software - https://maximumtrainer.com

Avatar
RankRankRankRank

Total Posts: 123

Joined 2013-10-07

PM

Using AutoInit() work fine when the ANT+ Stick is not in use by other program.

Is it possible to detect if there is an ANT+ Stick connected, but already used by another program
Could modifiy the AutoInit() method in dsi_serial_generic.cpp if so
Thank you!

///////////////////////////////////////////////////////////////////////
// Initializes and opens the object.
///////////////////////////////////////////////////////////////////////
//defines used by AutoInit
#define ANT_USB_STICK_PID     0x1004
#define ANT_USB_DEV_BOARD_PID 0x1006
#define ANT_USB_STICK_BAUD    ((USHORT)50000)
#define ANT_DEFAULT_BAUD      ((USHORT)57600)

BOOL DSISerialGeneric::AutoInit()
{
   Close
();
   if (
pclDevice)
      
delete pclDevice;
   
pclDevice NULL;
   
ucDeviceNumber 0xFF;   

   const 
ANTDeviceList clDeviceList USBDeviceHandle::GetAvailableDevices();  //saves a copy of the list and all of the elements
   
   
if(clDeviceList.GetSize() == 0)
   
{
      
//When this is being polled repeatedly, as in the ANTFS host class, the USBReset calls to WinDevice Enable/Disable take a large amount if CPU, so we limit the
      //reset here to only be called a maximum of once every 30s
      
time_t curTime time(NULL);
      if(
curTime lastUsbResetTime 30)
      
{
         lastUsbResetTime 
curTime;
         
this->USBReset();
      
}
      
return FALSE;
   
}   

//--- Custom code Added by blaisminator
   
if(clDeviceList.GetSize() > 0{
  
/// Detect if we have busy ANT ke
}

   
   USBDeviceHandle
::CopyANTDevice(pclDeviceclDeviceList[0]); 
   
ucDeviceNumber 0;            //initialize it to 0 for now (all devices that use autoinit will have a 0 device number) //TODO this devicenumber is useless because it can't be used to access the list again
   
switch (pclDevice->GetPid())
   
{
      
case ANT_USB_STICK_PID:      
         
ulBaud ANT_USB_STICK_BAUD;
         break;
      case 
ANT_USB_DEV_BOARD_PID:
      default:
         
ulBaud ANT_DEFAULT_BAUD;
         break;
   
}

   
return TRUE;
     

Signature

——————————————————
Free Indoor Cycling Software - https://maximumtrainer.com