12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Management;
- namespace ANT_Managed_Library
- {
-
-
-
-
- public class ANT_Device : IDisposable
- {
- #region variables
- ANT_ReferenceLibrary.FramerType frameType;
- ANT_ReferenceLibrary.PortType portType;
- IntPtr unmanagedANTSerialPtr = IntPtr.Zero;
- IntPtr unmanagedANTFramerPtr = IntPtr.Zero;
- IntPtr unmanagedCancelBool = IntPtr.Zero;
- bool? cancelTxFlag
- {
- get
- {
- if(unmanagedCancelBool == IntPtr.Zero)
- return null;
- return Convert.ToBoolean(Marshal.ReadInt32(unmanagedCancelBool));
- }
- set
- {
- if (unmanagedCancelBool != IntPtr.Zero)
- Marshal.WriteInt32(unmanagedCancelBool, Convert.ToInt32(value));
- }
- }
- bool initializedUSB = false;
- byte USBDeviceNum = 255;
- uint USBBaudRate = 0;
- ANT_DeviceCapabilities capabilities = null;
- bool pollingOn;
- int numDeviceChannels;
- ANT_Channel[] antChannels;
- #endregion
- #region DeviceCallback variables
-
- [StructLayout(LayoutKind.Sequential)]
- struct ANTMessageItem
- {
- public byte dataSize;
- public ANTMessage antMsgData;
- }
-
-
-
- [StructLayout(LayoutKind.Sequential)]
- public struct ANTMessage
- {
-
-
-
- public byte msgID;
-
-
-
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = ANT_ReferenceLibrary.MAX_MESG_SIZE)]
- public byte[] ucharBuf;
- }
-
-
-
- public enum DeviceNotificationCode : byte
- {
-
-
-
- Reset = 0x01,
-
-
-
- Shutdown = 0x02,
- }
- System.Threading.Thread responsePoller;
-
-
-
-
- public delegate void dDeviceResponseHandler(ANT_Response response);
-
-
-
-
- public event dDeviceResponseHandler deviceResponse;
-
-
-
-
-
-
- public delegate void dSerialErrorHandler(ANT_Device sender, serialErrorCode error, bool isCritical);
-
-
-
-
-
-
- public event dSerialErrorHandler serialError;
-
-
-
- public enum serialErrorCode
- {
-
-
-
-
- SerialWriteError,
-
-
-
- SerialReadError,
-
-
-
- DeviceConnectionLost,
-
-
-
- MessageLost_CrcError,
-
-
-
- MessageLost_QueueOverflow,
-
-
-
-
- MessageLost_TooLarge,
-
-
-
- MessageLost_InvalidChannel,
-
-
-
- Unknown
- }
- #endregion
- #region ANT_DLL Imports
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_Init(byte ucUSBDeviceNum, UInt32 usBaudrate, ref IntPtr returnSerialPtr, ref IntPtr returnFramerPtr, byte ucPortType, byte ucHCIFrameTpye);
-
-
-
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern void ANT_Close(IntPtr SerialPtr, IntPtr FramerPtr);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_USBReset(IntPtr SerialPtr);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_ResetSystem(IntPtr FramerPtr, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern void ANT_SetCancelParameter(IntPtr FramerPtr, IntPtr pbCancel);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_SetNetworkKey_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_SetNetworkKey(IntPtr FramerPtr, byte ucNetNumber, byte[] pucKey, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_EnableLED_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_EnableLED(IntPtr FramerPtr, byte ucEnable, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_GetDeviceUSBPID(IntPtr FramerPtr, ref UInt16 usbPID);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_GetDeviceUSBVID(IntPtr FramerPtr, ref UInt16 usbVID);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_ConfigureSplitAdvancedBursts(IntPtr FramerPtr, int bEnabelSplitBursts);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_ConfigureAdvancedBurst_RTO", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_ConfigureAdvancedBurst(IntPtr FramerPtr, int enable, byte ucMaxPacketLength, UInt32 ulRequiredFields, UInt32 ulOptionalFields, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_ConfigureAdvancedBurst_ext_RTO", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_ConfigureAdvancedBurst_ext(IntPtr FramerPtr, int enable, byte ucMaxPacketLength, UInt32 ulRequiredFields, UInt32 ulOptionalFields, UInt16 usStallCount, byte ucRetryCount, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_SetCryptoKey_RTO", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_SetCryptoKey(IntPtr FramerPtr, byte ucVolatileKeyIndex, byte[] pucKey, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_SetCryptoID_RTO", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_SetCryptoID(IntPtr FramerPtr, byte[] pucData, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_SetCryptoUserInfo_RTO", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_SetCryptoUserInfo(IntPtr FramerPtr, byte[] pucData, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_SetCryptoRNGSeed_RTO", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_SetCryptoRNGSeed(IntPtr FramerPtr, byte[] pucData, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_SetCryptoInfo_RTO", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_SetCryptoInfo(IntPtr FramerPtr, byte ucParameter, byte[] pucData, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_LoadCryptoKeyNVMOp_RTO", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_LoadCryptoKeyNVMOp(IntPtr FramerPtr, byte ucNVMKeyIndex, byte ucVolatileKeyIndex, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_StoreCryptoKeyNVMOp_RTO", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_StoreCryptoKeyNVMOp(IntPtr FramerPtr, byte ucNVMKeyIndex, byte[] pucKey, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_CryptoKeyNVMOp_RTO", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_CryptoKeyNVMOp(IntPtr FramerPtr, byte ucOperation, byte ucNVMKeyIndex, byte[] pucData, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern uint ANT_GetDeviceSerialNumber(IntPtr SerialPtr);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_GetDeviceUSBInfo(IntPtr FramerPtr, byte ucDeviceNum_,
- [MarshalAs(UnmanagedType.LPArray)] byte[] pucProductString_,
- [MarshalAs(UnmanagedType.LPArray)] byte[] pucSerialString_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_SetTransmitPower_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_SetTransmitPower(IntPtr FramerPtr, byte ucTransmitPower_, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_ConfigEventFilter(IntPtr FramerPtr, UInt16 usFilter_, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_ConfigEventBuffer(IntPtr FramerPtr, byte ucConfig_, UInt16 usSize_, UInt16 usTime_, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_ConfigHighDutySearch(IntPtr FramerPtr, byte ucEnable_, byte ucSuppressionCycles_, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_SetSelectiveDataUpdateMask(IntPtr FramerPtr, byte ucMaskNumber_, byte[] ucSduMask_, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_ConfigUserNVM(IntPtr FramerPtr, UInt16 usAddress_, byte[] pucData_, byte ucSize_, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_RequestMessage(IntPtr FramerPtr, byte ucANTChannel, byte ucMessageID, ref ANTMessageItem ANT_MESSAGE_ITEM_response, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_RequestUserNvmMessage(IntPtr FramerPtr, byte ucANTChannel, byte ucMessageID, ref ANTMessageItem ANT_MESSAGE_ITEM_response, UInt16 usAddress, byte ucSize, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern UInt16 ANT_WaitForMessage(IntPtr FramerPtr, UInt32 ulMilliseconds_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern UInt16 ANT_GetMessage(IntPtr FramerPtr, ref ANTMessage ANT_MESSAGE_response);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern byte ANT_GetChannelNumber(IntPtr FramerPtr, ref ANTMessage pstANTMessage);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_GetCapabilities(IntPtr FramerPtr,
- [MarshalAs(UnmanagedType.LPArray)] byte[] pucCapabilities_,
- UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_InitCWTestMode_RTO", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ANT_InitCWTestMode(IntPtr FramerPtr, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_SetCWTestMode_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_SetCWTestMode(IntPtr FramerPtr, byte ucTransmitPower_, byte ucRFChannel_, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_OpenRxScanMode_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_OpenRxScanMode(IntPtr FramerPtr, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_Script_Write_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_Script_Write(IntPtr FramerPtr, byte ucSize_, byte[] pucData_, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_Script_Clear_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_Script_Clear(IntPtr FramerPtr, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_Script_SetDefaultSector_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_Script_SetDefaultSector(IntPtr FramerPtr, byte ucSectNumber_, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_Script_EndSector_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_Script_EndSector(IntPtr FramerPtr, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_Script_Dump_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_Script_Dump(IntPtr FramerPtr, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_Script_Lock_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_Script_Lock(IntPtr FramerPtr, UInt32 ulResponseTimeout_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "ANT_RxExtMesgsEnable_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_RxExtMesgsEnable(IntPtr FramerPtr, byte ucEnable_, UInt32 ulResponseTimeout_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_CrystalEnable(IntPtr FramerPtr, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_WriteMessage(IntPtr FramerPtr, ANTMessage pstANTMessage, UInt16 usMessageSize);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, CallingConvention=CallingConvention.Cdecl)]
- private static extern int ANT_SetLibConfig(IntPtr FramerPtr, byte ucLibConfigFlags_, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "FIT_SetFEState_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int FIT_SetFEState(IntPtr FramerPtr, byte ucFEState_, UInt32 ulResponseTime_);
- [DllImport(ANT_Common.ANT_UNMANAGED_WRAPPER, EntryPoint = "FIT_AdjustPairingSettings_RTO", CallingConvention=CallingConvention.Cdecl)]
- private static extern int FIT_AdjustPairingSettings(IntPtr FramerPtr, byte ucSearchLv_, byte ucPairLv_, byte ucTrackLv_, UInt32 ulResponseTime_);
- #endregion
- #region Constructors and Destructor
-
-
-
-
-
-
-
-
-
-
- public ANT_Device(byte USBDeviceNum, uint baudRate)
- :this(ANT_ReferenceLibrary.PortType.USB, USBDeviceNum, baudRate, ANT_ReferenceLibrary.FramerType.basicANT)
- {
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public ANT_Device(ANT_ReferenceLibrary.PortType portType, byte USBDeviceNum, uint baudRate, ANT_ReferenceLibrary.FramerType frameType)
- {
- try
- {
- ANT_Common.checkUnmanagedLibrary();
- startUp(USBDeviceNum, baudRate, frameType, portType, false);
- }
- catch
- {
-
- this.Dispose();
- throw;
- }
- }
-
-
-
-
-
- public ANT_Device()
- {
- ANT_Common.checkUnmanagedLibrary();
- ulong numDevices = ANT_Common.getNumDetectedUSBDevices();
- if (numDevices == 0)
- {
- String detail = ": ensure an ANT device is connected to your system and try again";
- try
- {
-
- ANT_Common.checkUSBLibraries();
- }
- catch(ANT_Exception ex)
- {
- detail = ": " + ex.Message.Remove(0,21);
- }
- throw new ANT_Exception("No ANT devices detected" + detail);
- }
- bool fail = true;
- for (byte i = 0; i < numDevices && fail; ++i)
- {
- try
- {
-
- startUp(i, 57600, ANT_ReferenceLibrary.FramerType.basicANT, ANT_ReferenceLibrary.PortType.USB, true);
- fail = false;
- }
- catch (Exception)
- {
-
- try
- {
- startUp(i, 50000, ANT_ReferenceLibrary.FramerType.basicANT, ANT_ReferenceLibrary.PortType.USB, true);
- fail = false;
- }
- catch (Exception)
- {
- fail = true;
- }
- }
- }
- if (fail)
- {
-
- this.Dispose();
- throw new ANT_Exception("Failed to connect to any ANT devices");
- }
- }
-
- private void startUp(byte USBDeviceNum, uint baudRate, ANT_ReferenceLibrary.FramerType frameType, ANT_ReferenceLibrary.PortType portType, bool calledByAutoInit)
- {
-
-
- int ret = ANT_Init(USBDeviceNum, baudRate, ref unmanagedANTSerialPtr, ref unmanagedANTFramerPtr, (byte)portType, (byte)frameType);
- if (ret != 0)
- {
- switch(ret)
- {
- case -1:
- throw new ANT_Exception("Unexpected init library error. This is typically a problem with the c++ library");
- case -2:
- throw new ANT_Exception("Unrecognized type parameters");
- case -3:
- if (!calledByAutoInit)
- ANT_Common.checkUSBLibraries();
- throw new ANT_Exception("Unable to initialize USB:" + USBDeviceNum + " at Baud:" + baudRate + ", probably device not present or already in use, or drivers not installed");
- default:
- throw new ANT_Exception("Unrecognized error code received from c++ library");
- }
- }
- initializedUSB = true;
- this.USBDeviceNum = USBDeviceNum;
- this.USBBaudRate = baudRate;
- this.frameType = frameType;
- this.portType = portType;
- #if (ANTFS_DEBUGGING)
-
- ANT_Common.initDebugLogThread("Device" + USBDeviceNum + "_Application");
- ANT_Common.ANT_DebugResetTime();
- ANT_Common.writeToDebugLog("ANT_NET.DLL " + ANT_VersionInfo.getManagedLibraryVersion() + " with ANT_WrappedLib.DLL " + ANT_VersionInfo.getUnmanagedLibraryVersion() + " - current unix time: " + (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
- #endif
- try
- {
- responsePoller = new System.Threading.Thread(responsePollFunc);
- responsePoller.Name = this.ToString() + " Receive Thread";
- responsePoller.IsBackground = true;
- responsePoller.Start();
-
-
- try
- {
- getDeviceCapabilities(true, 200);
- }
- catch (ANT_Exception ex)
- {
- throw new ANT_Exception(ex.Message.Remove(0, 22) + ", probably connecting at wrong baud rate");
- }
- if(ANT_Common.autoResetIsEnabled)
- ANT_ResetSystem(unmanagedANTFramerPtr, 200);
- numDeviceChannels = capabilities.maxANTChannels;
-
- antChannels = new ANT_Channel[numDeviceChannels];
- unmanagedCancelBool = Marshal.AllocHGlobal(4);
- cancelTxFlag = false;
- ANT_SetCancelParameter(unmanagedANTFramerPtr, unmanagedCancelBool);
- }
- catch (Exception)
- {
- shutdown();
- throw;
- }
- }
-
-
-
- ~ANT_Device()
- {
- shutdown();
- }
-
-
-
- public void Dispose()
- {
- shutdown();
- GC.SuppressFinalize(this);
- }
-
-
- private void shutdown()
- {
- try
- {
- lock (this)
- {
- if (initializedUSB)
- {
-
- if (antChannels != null)
- {
- NotifyDeviceEvent(DeviceNotificationCode.Shutdown, null);
- foreach (ANT_Channel i in antChannels)
- {
-
-
- if (i != null)
- i.Dispose();
- }
- }
-
-
- cancelTxFlag = true;
-
- pollingOn = false;
- if (!responsePoller.Join(1500))
- {
- responsePoller.Abort();
- System.Diagnostics.Debug.Fail("Response poll thread did not join in timeout and was aborted. Was the application slow/stalling, or should we increase the join timeout?");
- }
-
-
- if (capabilities != null && ANT_Common.autoResetIsEnabled)
- ANT_ResetSystem(unmanagedANTFramerPtr, 0);
-
- ANT_Close(unmanagedANTSerialPtr, unmanagedANTFramerPtr);
- if (unmanagedCancelBool != IntPtr.Zero)
- {
- Marshal.FreeHGlobal(unmanagedCancelBool);
- unmanagedCancelBool = IntPtr.Zero;
- }
-
- initializedUSB = false;
- }
- }
- }
- catch (Exception ex)
- {
-
- System.Diagnostics.Debug.Fail("Exception on shutdown: " + ex.Message);
- }
- }
-
-
-
-
- public static void shutdownDeviceInstance(ref ANT_Device deviceToShutdown)
- {
- if (deviceToShutdown != null)
- {
- deviceToShutdown.Dispose();
- deviceToShutdown = null;
- }
- }
-
- internal void channelDisposed(byte channelNumber)
- {
- antChannels[channelNumber] = null;
- }
- #endregion
- #region non-ANTdll Functions
-
-
-
- public override string ToString()
- {
- return base.ToString()+ " on USBdeviceNum: " + USBDeviceNum.ToString();
- }
-
-
-
-
- internal IntPtr getFramerPtr()
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return unmanagedANTFramerPtr;
- }
-
-
-
-
- public int getOpenedUSBDeviceNum()
- {
- return USBDeviceNum;
- }
-
-
-
- public uint getOpenedUSBBaudRate()
- {
- return USBBaudRate;
- }
-
-
-
- public ANT_ReferenceLibrary.FramerType getOpenedFrameType()
- {
- return frameType;
- }
-
-
-
- public ANT_ReferenceLibrary.PortType getOpenedPortType()
- {
- return portType;
- }
-
-
-
-
- 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];
- }
-
-
-
-
- public int getNumChannels()
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return antChannels.Length;
- }
- void NotifyDeviceEvent(DeviceNotificationCode notification, Object notificationInfo)
- {
- foreach (ANT_Channel i in antChannels)
- {
- if(i != null)
- i.NotifyDeviceEvent(notification, notificationInfo);
- }
- }
- private void responsePollFunc()
- {
- ushort messageSize = 0;
- byte channelNum;
- bool isMsgForChannel;
- pollingOn = true;
- #if (ANTFS_DEBUGGING)
- ANT_Common.initDebugLogThread("Device" + USBDeviceNum + "_ANTReceive");
- #endif
- while (initializedUSB && pollingOn)
- {
-
-
- messageSize = ANT_WaitForMessage(unmanagedANTFramerPtr, 100);
- if (messageSize == (ushort)0xFFFE)
- continue;
- ANTMessage newMessage = new ANTMessage();
- messageSize = ANT_GetMessage(unmanagedANTFramerPtr, ref newMessage);
- if (messageSize == (ushort)0xFFFF)
- {
- serialErrorCode error;
- bool isCritical = false;
- switch(newMessage.msgID)
- {
- case 0x02:
- switch(newMessage.ucharBuf[0])
- {
- case 0x04:
- error = serialErrorCode.MessageLost_CrcError;
- break;
- case 0x02:
- error = serialErrorCode.SerialWriteError;
- break;
- case 0x03:
- error = serialErrorCode.SerialReadError;
- isCritical = true;
- break;
- case 0x01:
- error = serialErrorCode.DeviceConnectionLost;
- isCritical = true;
- break;
- default:
- error = serialErrorCode.Unknown;
- System.Diagnostics.Debug.Fail("Unknown serial failure, why isn't this known?");
- break;
- }
- break;
- case 0x01:
- error = serialErrorCode.MessageLost_QueueOverflow;
- break;
- case 0x03:
- error = serialErrorCode.MessageLost_TooLarge;
- break;
- default:
- error = serialErrorCode.Unknown;
- System.Diagnostics.Debug.Fail("Unknown serial failure, why isn't this known?");
- break;
- }
- if(isCritical)
- {
-
- pollingOn = false;
- }
-
- if (serialError != null)
- serialError(this, error, isCritical);
- else
- System.Diagnostics.Debug.Fail("Device Serial Communication Failure, HandleSerialError not handled by application");
- if (isCritical)
- break;
- else
- continue;
- }
- isMsgForChannel = false;
- channelNum = Byte.MaxValue;
- switch (newMessage.msgID)
- {
-
- case (byte)ANT_ReferenceLibrary.ANTMessageID.RESPONSE_EVENT_0x40:
- if (newMessage.ucharBuf[1] == (byte)ANT_ReferenceLibrary.ANTMessageID.EVENT_0x01)
- isMsgForChannel = true;
- break;
-
- case (byte)ANT_ReferenceLibrary.ANTMessageID.BROADCAST_DATA_0x4E:
- case (byte)ANT_ReferenceLibrary.ANTMessageID.ACKNOWLEDGED_DATA_0x4F:
- case (byte)ANT_ReferenceLibrary.ANTMessageID.EXT_BROADCAST_DATA_0x5D:
- case (byte)ANT_ReferenceLibrary.ANTMessageID.EXT_ACKNOWLEDGED_DATA_0x5E:
- case (byte)ANT_ReferenceLibrary.ANTMessageID.BURST_DATA_0x50:
- case (byte)ANT_ReferenceLibrary.ANTMessageID.EXT_BURST_DATA_0x5F:
- case (byte)ANT_ReferenceLibrary.ANTMessageID.RSSI_BROADCAST_DATA_0xC1:
- case (byte)ANT_ReferenceLibrary.ANTMessageID.RSSI_ACKNOWLEDGED_DATA_0xC2:
- case (byte)ANT_ReferenceLibrary.ANTMessageID.RSSI_BURST_DATA_0xC3:
- isMsgForChannel = true;
- break;
- }
- ANT_Response newResponse;
-
-
-
- if (isMsgForChannel)
- {
- channelNum = (byte)(newMessage.ucharBuf[0] & 0x1F);
- if (antChannels != null && channelNum < antChannels.Length)
- {
- if (antChannels[channelNum] != null)
- antChannels[channelNum].MessageReceived(newMessage, messageSize);
- }
- else
- {
- if (serialError != null)
- serialError(this, serialErrorCode.MessageLost_InvalidChannel, false);
- }
- }
- else
- {
- newResponse = new ANT_Response(this, newMessage.ucharBuf[0], DateTime.Now, newMessage.msgID, newMessage.ucharBuf.Take(messageSize).ToArray());
- if (deviceResponse != null)
- deviceResponse(newResponse);
- }
- }
- }
-
-
-
-
-
- public void cancelTransfers(int cancelWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- cancelTxFlag = true;
- System.Threading.Thread.Sleep(cancelWaitTime);
- cancelTxFlag = false;
- }
-
-
-
-
-
-
-
-
- public ANT_DeviceCapabilities getDeviceCapabilities(bool forceNewCopy, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- if (forceNewCopy || capabilities == null)
- {
- try
- {
- ANT_DeviceCapabilities newCaps = null;
- ANT_Response response = requestMessageAndResponse(ANT_ReferenceLibrary.RequestMessageID.CAPABILITIES_0x54, responseWaitTime);
- if (response != null && response.responseID == (byte)ANT_ReferenceLibrary.RequestMessageID.CAPABILITIES_0x54)
- {
- byte[] padding = new byte[16 - response.messageContents.Length];
- newCaps = new ANT_DeviceCapabilities(response.messageContents.Concat(padding).ToArray());
- }
- capabilities = newCaps;
- }
- catch (Exception)
- {
-
- throw new ANT_Exception("Retrieving Device Capabilities Failed");
- }
- }
- return capabilities;
- }
-
-
-
-
- public ANT_DeviceCapabilities getDeviceCapabilities() { return getDeviceCapabilities(false, 1500); }
-
-
-
-
- public ANT_DeviceCapabilities getDeviceCapabilities(UInt32 responseWaitTime) { return getDeviceCapabilities(false, responseWaitTime); }
- #endregion
- #region ANT Device Functions
-
-
-
-
- public void ResetUSB()
- {
- if (portType != ANT_ReferenceLibrary.PortType.USB)
- throw new ANT_Exception("Can't call ResetUSB on non-USB devices");
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- ANT_USBReset(unmanagedANTSerialPtr);
- }
-
-
-
-
-
-
- public bool ResetSystem(UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
-
- NotifyDeviceEvent(DeviceNotificationCode.Reset, null);
- return (ANT_ResetSystem(unmanagedANTFramerPtr, responseWaitTime) == 1);
- }
-
-
-
- public void ResetSystem(){ ResetSystem(500);}
-
-
-
-
-
-
-
-
-
- public bool setNetworkKey(byte netNumber, byte[] networkKey, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- if (capabilities != null)
- {
- if (netNumber >= capabilities.maxNetworks)
- throw new ANT_Exception("Network number must be less than the maximum capable networks of the device");
- }
- if (networkKey.Length != 8)
- throw new ANT_Exception("Network key must be 8 bytes");
- bool result = (ANT_SetNetworkKey(unmanagedANTFramerPtr, netNumber, networkKey, responseWaitTime) == 1);
- return result;
- }
-
-
-
-
-
-
- public void setNetworkKey(byte netNumber, byte[] networkKey) { setNetworkKey(netNumber, networkKey, 0); }
-
-
-
-
-
-
-
- public bool setTransmitPowerForAllChannels(ANT_ReferenceLibrary.TransmitPower transmitPower, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_SetTransmitPower(unmanagedANTFramerPtr, (byte)transmitPower, responseWaitTime) == 1);
- }
-
-
-
-
- public void setTransmitPowerForAllChannels(ANT_ReferenceLibrary.TransmitPower transmitPower) { setTransmitPowerForAllChannels(transmitPower, 0); }
-
-
-
-
-
-
- public bool configureAdvancedBurstSplitting(bool splitBursts)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_ConfigureSplitAdvancedBursts(unmanagedANTFramerPtr, (splitBursts ? 1 : 0)) == 1);
- }
-
-
-
-
-
-
-
-
-
- public bool configureAdvancedBursting(bool enable, byte maxPacketLength, ANT_ReferenceLibrary.AdvancedBurstConfigFlags requiredFields,
- ANT_ReferenceLibrary.AdvancedBurstConfigFlags optionalFields, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_ConfigureAdvancedBurst(unmanagedANTFramerPtr, (enable ? 1 : 0), maxPacketLength,
- (UInt32)requiredFields, (UInt32)optionalFields, responseWaitTime) == 1);
- }
-
-
-
-
-
-
-
- public void configureAdvancedBursting(bool enable, byte maxPacketLength, ANT_ReferenceLibrary.AdvancedBurstConfigFlags requiredFields,
- ANT_ReferenceLibrary.AdvancedBurstConfigFlags optionalFields)
- {
- configureAdvancedBursting(enable, maxPacketLength, requiredFields, optionalFields, (UInt32)0);
- }
-
-
-
-
-
-
-
-
-
-
-
- public bool configureAdvancedBursting_ext(bool enable, byte maxPacketLength, ANT_ReferenceLibrary.AdvancedBurstConfigFlags requiredFields,
- ANT_ReferenceLibrary.AdvancedBurstConfigFlags optionalFields, UInt16 stallCount, byte retryCount, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_ConfigureAdvancedBurst_ext(unmanagedANTFramerPtr, (enable ? 1 : 0), maxPacketLength,
- (UInt32)requiredFields, (UInt32)optionalFields, stallCount, retryCount, responseWaitTime) == 1);
- }
-
-
-
-
-
-
-
-
-
- public void configureAdvancedBursting_ext(bool enable, byte maxPacketLength, ANT_ReferenceLibrary.AdvancedBurstConfigFlags requiredFields,
- ANT_ReferenceLibrary.AdvancedBurstConfigFlags optionalFields, UInt16 stallCount, byte retryCount)
- {
- configureAdvancedBursting_ext(enable, maxPacketLength, requiredFields, optionalFields, stallCount, retryCount, 0);
- }
-
-
-
-
-
-
-
- public bool setCryptoKey(byte volatileKeyIndex, byte[] encryptionKey, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_SetCryptoKey(unmanagedANTFramerPtr, volatileKeyIndex, encryptionKey, responseWaitTime) == 1);
- }
-
-
-
-
-
- public void setCryptoKey(byte volatileKeyIndex, byte[] encryptionKey)
- {
- setCryptoKey(volatileKeyIndex, encryptionKey, 0);
- }
-
-
-
-
-
-
- public bool setCryptoID(byte[] encryptionID, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_SetCryptoID(unmanagedANTFramerPtr, encryptionID, responseWaitTime) == 1);
- }
-
-
-
-
- public void setCryptoID(byte[] encryptionID)
- {
- setCryptoID(encryptionID, 0);
- }
-
-
-
-
-
-
- public bool setCryptoUserInfo(byte[] userInfoString, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_SetCryptoUserInfo(unmanagedANTFramerPtr, userInfoString, responseWaitTime) == 1);
- }
-
-
-
-
- public void setCryptoUserInfo(byte[] userInfoString)
- {
- setCryptoUserInfo(userInfoString, 0);
- }
-
-
-
-
-
-
-
- public bool setCryptoRNGSeed(byte[] cryptoRNGSeed, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_SetCryptoRNGSeed(unmanagedANTFramerPtr, cryptoRNGSeed, responseWaitTime) == 1);
- }
-
-
-
-
-
- public void setCryptoRNGSeed(byte[] cryptoRNGSeed)
- {
- setCryptoRNGSeed(cryptoRNGSeed, 0);
- }
-
-
-
-
-
-
-
- public bool setCryptoInfo(ANT_ReferenceLibrary.EncryptionInfo encryptionParameter, byte[] parameterData, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_SetCryptoInfo(unmanagedANTFramerPtr, (byte)encryptionParameter, parameterData, responseWaitTime) == 1);
- }
-
-
-
-
-
- public void setCryptoInfo(ANT_ReferenceLibrary.EncryptionInfo encryptionParameter, byte[] parameterData)
- {
- setCryptoInfo(encryptionParameter, parameterData, 0);
- }
-
-
-
-
-
-
-
- public bool loadCryptoKeyFromNVM(byte nonVolatileKeyIndex, byte volatileKeyIndex, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_LoadCryptoKeyNVMOp(unmanagedANTFramerPtr, nonVolatileKeyIndex, volatileKeyIndex, responseWaitTime) == 1);
- }
-
-
-
-
-
- public void loadCryptoKeyFromNVM(byte nonVolatileKeyIndex, byte volatileKeyIndex)
- {
- loadCryptoKeyFromNVM(nonVolatileKeyIndex, volatileKeyIndex, 0);
- }
-
-
-
-
-
-
-
- public bool storeCryptoKeyToNVM(byte nonVolatileKeyIndex, byte[] encryptionKey, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_StoreCryptoKeyNVMOp(unmanagedANTFramerPtr, nonVolatileKeyIndex, encryptionKey, responseWaitTime) == 1);
- }
-
-
-
-
-
- public void storeCryptoKeyToNVM(byte nonVolatileKeyIndex, byte[] encryptionKey)
- {
- storeCryptoKeyToNVM(nonVolatileKeyIndex, encryptionKey, 0);
- }
-
-
-
-
-
-
-
-
-
-
- public bool cryptoKeyNVMOp(ANT_ReferenceLibrary.EncryptionNVMOp memoryOperation, byte nonVolatileKeyIndex, byte[] operationData, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_CryptoKeyNVMOp(unmanagedANTFramerPtr, (byte)memoryOperation, nonVolatileKeyIndex, operationData, responseWaitTime) == 1);
- }
-
-
-
-
-
-
-
-
- public void cryptoKeyNVMOp(ANT_ReferenceLibrary.EncryptionNVMOp memoryOperation, byte nonVolatileKeyIndex, byte[] operationData)
- {
- cryptoKeyNVMOp(memoryOperation, nonVolatileKeyIndex, operationData, 0);
- }
-
-
-
-
-
-
-
- public bool EnableLED(bool IsEnabled, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return (ANT_EnableLED(unmanagedANTFramerPtr, Convert.ToByte(IsEnabled), responseWaitTime) == 1);
- }
-
-
-
-
- public void EnableLED(bool IsEnabled) { EnableLED(IsEnabled, 0); }
-
-
-
-
-
-
-
-
-
-
-
- public bool configureEventBuffer(ANT_ReferenceLibrary.EventBufferConfig config, UInt16 size, UInt16 time, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_ConfigEventBuffer(unmanagedANTFramerPtr, (byte)config, size, time, responseWaitTime) == 1;
- }
-
-
-
-
-
-
-
-
- public void configureEventBuffer(ANT_ReferenceLibrary.EventBufferConfig config, UInt16 size, UInt16 time) { configureEventBuffer(config, size, time, 0); }
-
-
-
-
-
-
-
- public bool configureEventFilter(UInt16 eventFilter, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_ConfigEventFilter(unmanagedANTFramerPtr, eventFilter, responseWaitTime) == 1;
- }
-
-
-
-
- public void configureEventFilter(UInt16 eventFilter) { configureEventFilter(eventFilter, 0); }
-
-
-
-
-
-
-
-
- public bool configureHighDutySearch(bool enable, byte suppressionCycles, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_ConfigHighDutySearch(unmanagedANTFramerPtr, (enable ? (byte)1 : (byte)0), suppressionCycles, responseWaitTime) == 1;
- }
-
-
-
-
-
- public void configureHighDutySearch(bool enable, byte suppressionCycles) { configureHighDutySearch(enable, suppressionCycles, 0); }
-
-
-
-
-
-
-
-
- public bool setSduMask(byte maskNumber, byte[] mask, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_SetSelectiveDataUpdateMask(unmanagedANTFramerPtr, maskNumber, mask, responseWaitTime) == 1;
- }
-
-
-
-
-
- public void setSduMask(byte maskNumber, byte[] mask) { setSduMask(maskNumber, mask, 0); }
-
-
-
-
-
-
-
-
-
- public bool configureUserNvm(UInt16 address, byte[] data, byte size, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_ConfigUserNVM(unmanagedANTFramerPtr, address, data, size, responseWaitTime) == 1;
- }
-
-
-
-
-
-
- public void configureUserNvm(UInt16 address, byte[] data, byte size) { configureUserNvm(address, data, size, 0); }
-
-
-
-
-
-
-
-
- public ANT_Response readUserNvm(UInt16 address, byte size, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- ANTMessageItem response = new ANTMessageItem();
- if (ANT_RequestUserNvmMessage(unmanagedANTFramerPtr, 0, (byte)ANT_ReferenceLibrary.RequestMessageID.USER_NVM_0x7C, ref response, address, size, responseWaitTime) == 0)
- throw new ANT_Exception("Timed out waiting for requested message");
- ANT_Response retVal = new ANT_Response(this, 0, DateTime.Now, response.antMsgData.msgID, response.antMsgData.ucharBuf.Take(response.dataSize).ToArray());
- return retVal;
- }
-
-
-
-
-
-
- public ANT_Response readUserNvm(UInt16 address, byte size)
- {
- return readUserNvm(address, size, 500);
- }
-
-
-
-
-
- public UInt16 getDeviceUSBPID()
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- UInt16 returnPID = 0;
- if(ANT_GetDeviceUSBPID(unmanagedANTFramerPtr, ref returnPID) != 1)
- throw new ANT_Exception("Retrieving Device USB PID failed");
- return (returnPID);
- }
-
-
-
-
- public UInt16 getDeviceUSBVID()
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- UInt16 returnVID = 0;
- if (ANT_GetDeviceUSBVID(unmanagedANTFramerPtr, ref returnVID) != 1)
- throw new ANT_Exception("Retrieving Device USB VID failed");
- return (returnVID);
- }
-
-
-
-
-
-
- public uint getSerialNumber()
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_GetDeviceSerialNumber(unmanagedANTSerialPtr);
- }
-
-
-
-
-
-
-
- public ANT_DeviceInfo getDeviceUSBInfo(byte deviceNum)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- byte[] returnProdDescription = new byte[256];
- byte[] returnSerialString = new byte[256];
- if (ANT_GetDeviceUSBInfo(unmanagedANTFramerPtr, deviceNum, returnProdDescription, returnSerialString) != 1)
- throw new ANT_Exception("Retrieving USB device information failed");
- return (new ANT_DeviceInfo(returnProdDescription, returnSerialString));
- }
-
-
-
-
-
- public ANT_DeviceInfo getDeviceUSBInfo()
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return getDeviceUSBInfo(USBDeviceNum);
- }
-
-
-
-
-
-
-
-
- public ANT_Response requestMessageAndResponse(byte channelNum, ANT_ReferenceLibrary.RequestMessageID messageID, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- ANTMessageItem response = new ANTMessageItem();
- if (ANT_RequestMessage(unmanagedANTFramerPtr, channelNum, (byte)messageID, ref response, responseWaitTime) == 0)
- throw new ANT_Exception("Timed out waiting for requested message");
- ANT_Response retVal = new ANT_Response(this, channelNum, DateTime.Now, response.antMsgData.msgID, response.antMsgData.ucharBuf.Take(response.dataSize).ToArray());
- return retVal;
- }
-
-
-
-
-
-
- public ANT_Response requestMessageAndResponse(ANT_ReferenceLibrary.RequestMessageID messageID, UInt32 responseWaitTime)
- {
- return requestMessageAndResponse(0, messageID, responseWaitTime);
- }
-
-
-
-
-
-
- public void requestMessage(byte channelNum, ANT_ReferenceLibrary.RequestMessageID messageID)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- ANTMessageItem dummystruct = new ANTMessageItem();
- ANT_RequestMessage(unmanagedANTFramerPtr, channelNum, (byte)messageID, ref dummystruct, 0);
- }
-
-
-
-
- public void requestMessage(ANT_ReferenceLibrary.RequestMessageID messageID)
- {
- requestMessage(0, messageID);
- }
-
-
-
-
-
-
-
-
- public bool openRxScanMode(UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_OpenRxScanMode(unmanagedANTFramerPtr, responseWaitTime) == 1;
- }
-
-
-
-
- public void openRxScanMode() { openRxScanMode(0); }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public bool startCWTest(ANT_ReferenceLibrary.TransmitPower transmitPower, byte RFFreqOffset, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- bool retVal = true;
- ResetSystem();
- if (ANT_InitCWTestMode(unmanagedANTFramerPtr, responseWaitTime) != 1)
- retVal = false;
- if (ANT_SetCWTestMode(unmanagedANTFramerPtr, (byte)transmitPower, RFFreqOffset, responseWaitTime) != 1)
- retVal = false;
- if (retVal == false && ANT_Common.autoResetIsEnabled)
- ResetSystem(500);
- return retVal;
- }
-
-
-
-
-
-
-
- public bool enableRxExtendedMessages(bool IsEnabled, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_RxExtMesgsEnable(unmanagedANTFramerPtr, Convert.ToByte(IsEnabled), responseWaitTime) == 1;
- }
-
-
-
-
- public void enableRxExtendedMessages(bool IsEnabled) { enableRxExtendedMessages(IsEnabled, 0); }
-
-
-
-
-
-
-
- public bool crystalEnable(UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_CrystalEnable(unmanagedANTFramerPtr, responseWaitTime) == 1;
- }
-
-
-
-
- public void crystalEnable() { crystalEnable(0); }
-
-
-
-
-
-
- public bool writeRawMessageToDevice(byte msgID, byte[] msgData)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
-
- ANTMessage aMsg = new ANTMessage();
- aMsg.msgID = msgID;
-
- int padNum = ANT_ReferenceLibrary.MAX_MESG_SIZE - msgData.Length;
- if(padNum < 0)
- throw new ANT_Exception("msgData max length is " + ANT_ReferenceLibrary.MAX_MESG_SIZE + " bytes");
- aMsg.ucharBuf = msgData.Concat(new byte[padNum]).ToArray();
- return ANT_WriteMessage(unmanagedANTFramerPtr, aMsg, (UInt16)msgData.Length) == 1;
- }
-
-
-
-
-
-
-
- public bool setLibConfig(ANT_ReferenceLibrary.LibConfigFlags libConfigFlags, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_SetLibConfig(unmanagedANTFramerPtr, (byte)libConfigFlags, responseWaitTime) == 1;
- }
-
-
-
-
- public void setLibConfig(ANT_ReferenceLibrary.LibConfigFlags libConfigFlags) { setLibConfig(libConfigFlags, 0); }
- #region SensRcore NVM script commands
-
-
-
-
-
-
-
-
- public bool script_Write(byte[] commandString, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- if(!(commandString.Length < 256))
- throw new ANT_Exception("commandString max size is 255");
- return ANT_Script_Write(unmanagedANTFramerPtr, (byte)commandString.Length, commandString, responseWaitTime) == 1;
- }
-
-
-
-
-
- public void script_Write(byte[] commandString) { script_Write(commandString, 0); }
-
-
-
-
-
-
- public bool script_Clear(UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_Script_Clear(unmanagedANTFramerPtr, responseWaitTime) == 1;
- }
-
-
-
- public void script_Clear() { script_Clear(0); }
-
-
-
-
-
-
-
-
- public bool script_setDefaultSector(byte sectorNum, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_Script_SetDefaultSector(unmanagedANTFramerPtr, sectorNum, responseWaitTime) == 1;
- }
-
-
-
-
-
- public void script_setDefaultSector(byte sectorNum) { script_setDefaultSector(sectorNum, 0); }
-
-
-
-
-
-
- public bool script_endSector(UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_Script_EndSector(unmanagedANTFramerPtr, responseWaitTime) == 1;
- }
-
-
-
- public void script_endSector() { script_endSector(0); }
-
-
-
-
-
-
-
-
- public bool script_requestNVMDump(UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_Script_Dump(unmanagedANTFramerPtr, responseWaitTime) == 1;
- }
-
-
-
-
-
- public void script_requestNVMDump() { script_requestNVMDump(0); }
-
-
-
-
-
-
-
- public bool script_lockNVM(UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- return ANT_Script_Lock(unmanagedANTFramerPtr, responseWaitTime) == 1;
- }
-
-
-
-
- public void script_lockNVM() { script_lockNVM(0); }
-
-
-
-
-
-
-
-
- public bool fitSetFEState(byte feState, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- if (!capabilities.FIT)
- return false;
- return FIT_SetFEState(unmanagedANTFramerPtr, feState, responseWaitTime) == 1;
- }
-
-
-
-
-
- public void fitSetFEState(byte feState) { fitSetFEState(feState, 0);}
-
-
-
-
-
-
-
-
-
-
- public bool fitAdjustPairingSettings(byte searchLv, byte pairLv, byte trackLv, UInt32 responseWaitTime)
- {
- if (!initializedUSB)
- throw new ObjectDisposedException("ANTDevice object has been disposed");
- if (!capabilities.FIT)
- return false;
- return FIT_AdjustPairingSettings(unmanagedANTFramerPtr, searchLv, pairLv, trackLv, responseWaitTime) == 1;
- }
-
-
-
-
-
-
-
-
- public void fitAdjustPairingSettings(byte searchLv, byte pairLv, byte trackLv)
- {
- fitAdjustPairingSettings(searchLv, pairLv, trackLv, 0);
- }
- #endregion
- #endregion
- }
- }
|