使用神念科技的脑电头带进行脑电信号的获取,用于记录学习者学习过程中的专注度放松度等指标
脑电头带:
购买头带后带有的一系列开发用的资料:
脑电头带使用蓝牙进行信号传输,首先安装千月蓝牙助手并与脑电头带建立连接
购买头戴时带有一系列的开发工具,比如一个已经集成好的图形化脑电信号的程序,下图是佩戴脑电头带后测试得到的数据图
开发包中包含一个HelloEEG程序,为一个与脑电头带进行信号传输的demo,自动进行串口查询与信号检测
源代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using System.IO;
using System.IO.Ports;
using NeuroSky.ThinkGear;
using NeuroSky.ThinkGear.Algorithms;
using System.Net.Sockets;
using System.Net;
namespace testprogram {
class Program {
static Connector connector;
static bool golfZoneDemo = false;
static double task_famil_baseline, task_famil_cur, task_famil_change;
static bool task_famil_first;
static double mental_eff_baseline, mental_eff_cur, mental_eff_change;
static bool mental_eff_first;
//*******************
//socket通信,客户端
//*******************
byte[] data = new byte[1024];
static Socket newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static IPEndPoint ie = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000);//服务器的IP和端口
public static void Main(string[] args) {
while (true)
{
try
{
//因为客户端只是用来向特定的服务器发送信息,所以不需要绑定本机的IP和端口。不需要监听。
newclient.Connect(ie);
break;
}
catch (SocketException e)
{
Console.WriteLine("*******************unable to connect to server*****************");
Console.WriteLine(e.ToString());
return;
}
}
//接收消息
//int recv = newclient.Receive(data);
//string stringdata = Encoding.ASCII.GetString(data, 0, recv);
//Console.WriteLine(stringdata);
//while (true)
//{
// string input ="";
// newclient.Send(Encoding.ASCII.GetBytes(input));
//接收消息
//recv = newclient.Receive(data);
//stringdata = Encoding.ASCII.GetString(data, 0, recv);
//Console.WriteLine(stringdata);
//}
//Console.WriteLine("disconnect from sercer");
//newclient.Shutdown(SocketShutdown.Both);
//newclient.Close();
Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
if (assembly != null)
{
object[] customAttribute1 = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if ((customAttribute1 != null) && (customAttribute1.Length > 0))
Console.WriteLine(((AssemblyTitleAttribute)customAttribute1[0]).Title);
object[] customAttribute2 = assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
if ((customAttribute2 != null) && (customAttribute2.Length > 0))
Console.WriteLine(((AssemblyCompanyAttribute)customAttribute2[0]).Company);
Console.WriteLine(assembly.GetName().Version.ToString());
}
AppDomain MyDomain = AppDomain.CurrentDomain;
Assembly[] AssembliesLoaded = MyDomain.GetAssemblies();
foreach (Assembly MyAssembly in AssembliesLoaded)
{
if (MyAssembly.FullName.Contains("ThinkGear"))
Console.WriteLine(MyAssembly.FullName);
}
Console.WriteLine("----------");
if (golfZoneDemo) Console.WriteLine("Hello Golfer!");
else Console.WriteLine("Hello EEG!");
Console.WriteLine("----------");
// Initialize a new Connector and add event handlers
connector = new Connector();
connector.DeviceConnected += new EventHandler(OnDeviceConnected);
connector.DeviceConnectFail += new EventHandler(OnDeviceFail);
connector.DeviceValidating += new EventHandler(OnDeviceValidating);
// Scan for devices
// add this one to scan 1st
//connector.ConnectScan("COM40");
connector.ConnectScan("COM17");
//start the mental effort and task familiarity calculations
if (golfZoneDemo) {
connector.setMentalEffortEnable(false);
connector.setTaskFamiliarityEnable(false);
connector.setBlinkDetectionEnabled(false);
}
else {
connector.enableTaskDifficulty(); //depricated
connector.enableTaskFamiliarity(); //depricated
connector.setMentalEffortRunContinuous(true);
connector.setMentalEffortEnable(true);
connector.setTaskFamiliarityRunContinuous(true);
connector.setTaskFamiliarityEnable(true);
connector.setBlinkDetectionEnabled(true);
}
task_famil_baseline = task_famil_cur = task_famil_change = 0.0;
task_famil_first = true;
mental_eff_baseline = mental_eff_cur = mental_eff_change = 0.0;
mental_eff_first = true;
Thread.Sleep(8 * 60 * 1000); // time to live for this program (8 min * 60 sec * 1000 ms)
Console.WriteLine("----------");
if (golfZoneDemo) Console.WriteLine("Time is up. Goodbye from Golf Zone sample program!");
else Console.WriteLine("Time is up. Goodbye from EEG sample program!");
Console.WriteLine("----------");
// Close all open connections
connector.Close();
Thread.Sleep(10 * 1000); // delay long enough for a human to see the message (10 sec * 1000 ms)
Environment.Exit(0);
}
/**
* Called when a device is connected
*/
static void OnDeviceConnected(object sender, EventArgs e) {
Connector.DeviceEventArgs de = (Connector.DeviceEventArgs)e;
Console.WriteLine("Device found on: " + de.Device.PortName);
de.Device.DataReceived += new EventHandler(OnDataReceived);
}
/**
* Called when scanning fails
*/
static void OnDeviceFail(object sender, EventArgs e) {
Console.WriteLine("No devices found! :(");
}
/**
* Called when each port is being validated
*/
static void OnDeviceValidating(object sender, EventArgs e) {
Console.WriteLine("Validating: ");
}
static byte rcv_poorSignal_last = 255; // start with impossible value
static byte rcv_poorSignal;
static byte rcv_poorSig_cnt = 0;
/**
* Called when data is received from a device
*/
static void OnDataReceived(object sender, EventArgs e) {
//Device d = (Device)sender;
Device.DataEventArgs de = (Device.DataEventArgs)e;
DataRow[] tempDataRowArray = de.DataRowArray;
TGParser tgParser = new TGParser();
tgParser.Read(de.DataRowArray);
/* Loop through new parsed data */
for (int i = 0; i < tgParser.ParsedData.Length; i++)
{
if (tgParser.ParsedData[i].ContainsKey("MSG_MODEL_IDENTI