The ThinkGear SDK for .NET's API is made available to your application via the NeuroSky.ThinkGear namespace. Once you have added the ThinkGear.dll file to your project, you can then add the following code to the top of your application to access the NeuroSky.ThinkGear namespace:using NeuroSky.ThinkGear;Using the NeuroSky.ThinkGear NamespaceThe NeuroSky.ThinkGear namespace consists of two classes:Connector - Connects to the computer's serial COM port and reads in the port's serial stream of data as DataRowArrays.TGParser - Parses a DataRowArray into recognizable ThinkGear Data Types that your application can use.To use the classes, first declare a Connector instance and initialize it:private Connector connector;connector = new Connector();Next, create EventHandlers to handle each type of Connector Event, and link those handlers to the Connector events.connector.DeviceConnected += new EventHandler( OnDeviceConnected );connector.DeviceFound += new EventHandler( OnDeviceFound );connector.DeviceNotFound += new EventHandler( OnDeviceNotFound );connector.DeviceConnectFail += new EventHandler( OnDeviceNotFound );connector.DeviceDisconnected += new EventHandler( OnDeviceDisconnected );connector.DeviceValidating += new EventHandler( OnDeviceValidating );In the handler for the DeviceConnected event, you should create another EventHandler to handle DataReceived events from the Device, like this:void OnDeviceConnected( object sender, EventArgs e ) { Connector.DeviceEventArgs deviceEventArgs = (Connector.DeviceEventArgs)e; Console.WriteLine( "New Headset Created." + deviceEventArgs.Device.DevicePortName ); deviceEventArgs.Device.DataReceived += new EventHandler( OnDataReceived );}Now, whenever data is received from the device, the DataReceived handler will process that data. Here is an example OnDeviceReceived() that shows how it can do this, using a TGParser to parse the DataRow[]:void OnDataReceived( object sender, EventArgs e ){ /* Cast the event sender as a Device object, and e as the Device's DataEventArgs */ Device d = (Device)sender; Device.DataEventArgs de = (Device.DataEventArgs)e; /* Create a TGParser to parse the Device's DataRowArray[] */ TGParser tgParser = new TGParser(); tgParser.Read( de.DataRowArray ); /* Loop through parsed data TGParser for its parsed data... */ for( int i=0; i // See the Data Types documentation for valid keys such // as "Raw", "PoorSignal", "Attention", etc. if( tgParser.ParsedData[i].ContainsKey("Raw") ){ Console.WriteLine( "Raw Value:" + tgParser.ParsedData[i]["Raw"] ); } if( tgParser.ParsedData[i].ContainsKey("PoorSignal") ){ Console.WriteLine( "PQ Value:" + tgParser.ParsedData[i]["PoorSignal"] ); } if( tgParser.ParsedData[i].ContainsKey("Attention") ) { Console.WriteLine( "Att Value:" + tgParser.ParsedData[i]["Attention"] ); } if( tgParser.ParsedData[i].ContainsKey("Meditation") ) { Console.WriteLine( "Med Value:" + tgParser.ParsedData[i]["Meditation"] ); } }}When you would like to begin the Mental Effort and/or Familiarity 1) calculations, use the connector to enable them:connector.setMentalEffortEnable(true);connector.setTaskFamiliarityEnable(true);Once you have the handlers set up as described above, you can have your Connector actually connect to a device/headset/COM port by using one of the Connect methods described in Connect to a device below. If the portName is valid and the connection is successful, then your OnDataReceived() method will automatically be called and executed whenever data arrives from the headset.Before exiting, your application must close the Connector's open connections by calling the Connector's close() method.connector.close();If close() is not called on an open connection, and that connection's process is still alive (i.e. a background thread, or a process that only closed the GUI window without terminating the process itself), then the headset will still be connected to the process, and no other process will be able to connect to the headset until it is disconnected.EventsIf you choose to connect by stating a specific COM port, it will take the following steps:connector.Connect(portName);connector.Connect in turn validates the COM port. So the DeviceValidating event is triggeredif the COM port was valid, it connects to the device. The DeviceFound event is never triggeredif the COM port was invalid, the DeviceNotFound event is triggered.If you choose to connect by using the AUTO approach, it will take the following steps:connector.Find();if it is able to find a COM port with valid ThinkGear Packets, it triggers DeviceFound. Otherwise, the Device
การแปล กรุณารอสักครู่..
