数据流开发神念科技提供了两个文件ThinkGearStreamParse.h和ThinkGearStreamParse.cpp两个文件,主要接口为:
宏定义:
/* Data CODE definitions */
#define PARSER_CODE_BATTERY 0x01
#define PARSER_CODE_POOR_QUALITY 0x02
#define PARSER_CODE_ATTENTION 0x04
#define PARSER_CODE_MEDITATION 0x05
#define PARSER_CODE_8BITRAW_SIGNAL 0x06
#define PARSER_CODE_RAW_MARKER 0x07
#define PARSER_CODE_RAW_SIGNAL 0x80
#define PARSER_CODE_EEG_POWERS 0x81
#define PARSER_CODE_ASIC_EEG_POWER_INT 0x83
表示数据类型
typedef struct _ThinkGearStreamParser {
unsigned char type;
unsigned char state;
unsigned char lastByte;
unsigned char payloadLength;
unsigned char payloadBytesReceived;
unsigned char payload[256];
unsigned char payloadSum;
unsigned char chksum;
void (*handleDataValue)( unsigned char extendedCodeLevel,
unsigned char code, unsigned char numBytes,
const unsigned char *value, void *customData );
void *customData;
} ThinkGearStreamParser;
分析结构体,保存着buffer和状态机
int
THINKGEAR_initParser( ThinkGearStreamParser *parser, unsigned char parserType,
void (*handleDataValueFunc)(
unsigned char extendedCodeLevel,
unsigned char code, unsigned char numBytes,
const unsigned char *value, void *customData),
void *customData );
初始化分析状态机
int
THINKGEAR_parseByte( ThinkGearStreamParser *parser, unsigned char byte );
分析数据,每收到一个字节,都送到状态机里去处理。
示例代码如下:
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "ThinkGearStreamParser.h" #define USE_COMPORT "com3" #if 0
/* Data CODE definitions */
#define PARSER_CODE_BATTERY 0x01
#define PARSER_CODE_POOR_QUALITY 0x02
#define PARSER_CODE_ATTENTION 0x04
#define PARSER_CODE_MEDITATION 0x05
#define PARSER_CODE_8BITRAW_SIGNAL 0x06
#define PARSER_CODE_RAW_MARKER 0x07 #define PARSER_CODE_RAW_SIGNAL 0x80
#define PARSER_CODE_EEG_POWERS 0x81
#define PARSER_CODE_ASIC_EEG_POWER_INT 0x83
#endif // 处理数据回调函数
void handleDataValueFunc( unsigned char extendedCodeLevel,
unsigned char code, // 数据类型
unsigned char numBytes, // 数据长度
const unsigned char *value, // 数据
void *customData ) // 自定义数据
{
static int meditation = 0;
static int attention = 0;
static int count = 0;
if(extendedCodeLevel == 0)
{
switch(code)
{
// 如果是信号质量,则打印信号质量
case PARSER_CODE_POOR_QUALITY:
printf("the PARSER_CODE_POOR_QUALITY is %d\n", value[0]&0xff);
break;
// 04是专注度数据
case 0x04:
count ++;
attention = value[0]&0xff;
printf("the attention is %d\n", attention);
break;
// 05是放松度数据
case 0x05:
count++;
meditation = value[0]& 0xff;
printf("the meditation is %d\n", value[0]& 0xff);
break;
}
} } int main()
{
// 定义神念数据分析流结构体
ThinkGearStreamParser parser;
int quitFlag = 0; // 初始化结构体
::THINKGEAR_initParser(&parser, PARSER_TYPE_PACKETS, handleDataValueFunc, &quitFlag); // 打开串口,蓝牙已经被转换为串口了
HANDLE hCom;
hCom = CreateFileA(USE_COMPORT,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if(hCom == INVALID_HANDLE_VALUE)
{
printf("Error open serial\n");
exit(1);
} // 一序列串口设置
COMMTIMEOUTS TimeOuts;
if(!SetupComm(hCom, 2048, 2048))
return false; //设定读超时
TimeOuts.ReadIntervalTimeout = MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 0; //设定写超时
TimeOuts.WriteTotalTimeoutMultiplier = 100;
TimeOuts.WriteTotalTimeoutConstant = 500;
if(!SetCommTimeouts(hCom, &TimeOuts))
return 0;//设置超时 DCB dcb;
GetCommState(hCom, &dcb);
dcb.BaudRate = 57600; //波特率为57600
dcb.ByteSize = 8; //每个字节有8位
dcb.Parity = NOPARITY; //无奇偶校验位
dcb.StopBits = ONESTOPBIT; //两个停止位
if(!SetCommState(hCom, &dcb))
return 0; if(!PurgeComm(hCom, PURGE_TXCLEAR|PURGE_RXCLEAR))
return 0; // 从串口下接收数据
unsigned char streamByte;
DWORD dwRead;
while(!quitFlag)
{
// 从串口读取一个字节,然后把字节送到神念分析接口去分析
if(ReadFile(hCom, &streamByte, 1, &dwRead, NULL))
THINKGEAR_parseByte(&parser, streamByte);
} CloseHandle(hCom);
}