void StartThread() { //Initialize COM object lock (threadInitialized) { try { session = CreateComInstance(); encoder = GetEncoder(session); } catch (Exception) { connectException = true; } Monitor.Pulse(threadInitialized); if (connectException) //!!Close connections? { ReleaseComInstance(session); return; } } //Use thread to gather data from COM object DataLoop(); //!!End channels and queues /* for (int i = 0; i < channels.Length; i++) { queues[i].SignalExit(); queues. }*/ session.CloseConnections(); ReleaseComInstance(session); return; }
TTLEncoder[] FindEncoders() { //Open connections to find all encoders connected to computer //NOTE: If we get an exception "COM object that has been separated from its underlying RCW cannot be used." it might be thrown because session is being called on a different thread that has a different thread mode (STA/MTA) // Also happens when you dispose the liveSessionTtl and then try calling this. int scanned = 0; int detected = 0; session.OpenConnections( (int)TTLLiveCtrlLib.TTLAPI_OPENCONNECTIONS_CMD_BITS.TTLAPI_OCCMD_AUTODETECT //This re-enumerates all encoder handles! , 1000 //a value, in milliseconds, specifying the maximum expected interval between reads on any channel. This affects the amount of memory the API reserves as buffer space for each channel. , ref scanned , ref detected ); //Put encoders into a nice array int encoderCount = session.EncoderCount; TTLEncoder[] encoders = new TTLEncoder[encoderCount]; for (int i = 0; i < encoderCount; i++) { encoders[i] = session.Encoder[i]; } return encoders; }
TTLChannel GetChannel(TTLEncoder encoder, Channel channel) { /* //Set up all channels automatically session.AutoSetupChannels(); if (session.ChannelCount <= 0) { throw new Exception("Could not find any channels."); } */ //Get the first channel on the first encoder int channel1Handle = -1; //passing in -1 means AddChannel() will just return the first available channel //encoder.AddChannel(TTLAPI_CHANNELS.TTLAPI_CHANNEL_A, ref channel1Handle); encoder.AddChannel((TTLAPI_CHANNELS)channel, ref channel1Handle); TTLChannel ch1 = session.get_Channel(channel1Handle); return ch1; }