How to decode binary coming from the neurosky module?

hello everyone. I have a neurosky TGAM module that I need to receive data through Serial1 of the arduino Mega, however, they are in this format AA AA 04 80 02 00 34
and I can't plot or use.
How do I get the arduino to receive this data via Serial1 and convert it to DEC?
IE how to decode this AA AA 04 80 02 00 34 ?

Tried some libraries but missing some values.

I'll leave the reference links I've been researching.
Anyone who can help with a link, libraries or sket I really appreciate it!!!

thinkgear_communications_protocol [NeuroSky Developer - Docs]Processando: TGAM数据流格式说明.pdf...

Can you post your code in code tags </> and a schematic of your setup?

not sure how much of this you'll understand.

binary data is always transmitted as binary bits usually in groups of 8-bit bytes. it's very common to send text information using ASCII character coding. but it's more efficient to send non-text data (e.g. integer, float, graphics, audio) as a bit sequence representing those value in memory.

the issues are knowing where the data begins, how much follows and what it represents.

the above represents 7 bytes using hex notation where 4-bits are represented by a char 0-9A-F, and 2 hex characters represent an 8-bit byte.

your link describes the format of a packet.

  • the first two bytes are described as SYNC, 0xAA
  • the 3rd byte is the # of bytes that follow, 4 in this packet
  • the 4th byte is the code describing the data, 0x80, which in your link is a 2-byte raw wave value
  • the 5th byte is the # of data bytes, 2
  • the remaining byte are the data 0x00 0x34 in big endian format which means the 16-bit value is 0x0034 or 52 DEC

i would just define an 16-bit integer variable and copy the bytes into the multi-byte variable and then it can be operated on like any other integer.


the program needs to wait for the 2 SYNC bytes and then the packet length and read that many additional bytes into a buffer.

there could be multiple data in the packet each with a data -code and length if needed. each data-code and corresponding data need to be captured and processed independently until the entire packet is processed

the program needs to interpret the data based on the data-code. looks like there are 4 multi-byte data codes and 6 single byte data codes

somewhere i saw mention of a checksum which i think is missing in your posting.

i have code that reads a binary packet from an Automess radiation meter that must similarly wait for a start byte and process a packet. it handles a more complicated form a data

start by reading a complete packet, then disassembling it, interpreting the data and finally doing something with the data

1 Like

The link you referenced tells you how to "parse" the returned packet.

A packet of information is a series of values arranged so each value has some significance.

The example below says there will be / could be 11 values.

If you read each value into an array[ ] you could look at each piece of the "packet"
for instance if you wanted to battery level you would look at array location 6. etc.

I understand this is not a simple task for a beginner. Usually this type of stuff is performed in a library some code capable kind sole wrote. However if you can't find a library you will have to do it yourself.

Try not to let the format of the code through you. Windows 10 has a calculator with a programmers option.

In the code the decimal number 18 will be:

decimal 18
hex 0x12
binary 0b10010

They are all the same number.

1 Like

dear, I was happy and sad at the same time. I thought it was simpler to get the data.
I honestly don't know how to thank you for taking the time to answer me. I greatly appreciate your help.
There's a code I'm using that manages to capture data from the Tgam module but it's not that complicated. Could you analyze it please? could you comment it? It doesn't even use a specific library.

/*
通过UART串口显示信号值、注意力及放松度的值
*/
#define BAUDRATE 57600
#define DEBUGOUTPUT 0
#define PARSER_RAW_CODE 0x80

//校验相关变量
int generatedChecksum = 0;
byte checksum = 0;

//接收数据长度和数据数组
byte payloadLength = 0;
byte payloadData[32] = {0};//总共接收32个自己的数据

//需要读取的信息变量
byte signalquality = 0;//信号质量
byte attention = 0; //注意力值
byte meditation = 0; //放松度值

//初始化
void setup()
{
Serial.begin(BAUDRATE);
Serial1.begin(57600);
}

//从串口读取一个字节数据
byte ReadOneByte()
{
int ByteRead;
while(!Serial1.available());
ByteRead = Serial1.read();
return ByteRead;//返回读到的字节
}

//读取串口数据
void read_serial_data()
{
//寻找数据包起始同步字节,2个
if(ReadOneByte() == 0xAA)//先读一个
{
if(ReadOneByte() == 0xAA)//读第二个
{
payloadLength = ReadOneByte();//读取第三个,数据包字节的长度
if(payloadLength == 0x20)//如果接收到的是大包数据才继续读取,小包数据则舍弃不读取
{
generatedChecksum = 0; //校验变量清0
for(int i = 0; i < payloadLength; i++)//连续读取32个字节
{
payloadData[i] = ReadOneByte();//读取指定长度数据包中的数据
generatedChecksum += payloadData[i];//计算数据累加和
}
checksum = ReadOneByte();//读取校验字节
//校验
generatedChecksum = (~generatedChecksum)&0xff;
//比较校验字节
if(checksum == generatedChecksum)//数据接收正确,继续处理
{
signalquality = 0;//信号质量变量
attention = 0; //注意力值变量
//赋值数据
signalquality = payloadData[1];//信号值
attention = payloadData[29];//注意力值
meditation = payloadData[31];//放松度值
#if !DEBUGOUTPUT
//打印信号质量
Serial.print("SignalQuality: ");
Serial.print(signalquality, DEC);
//打印注意力值
Serial.print("Attation: ");
Serial.print(attention, DEC);
//打印放松度值
Serial.print("Meditation: ");
Serial.print(meditation, DEC);
//换行
Serial.print("\n");
Serial.print(PARSER_RAW_CODE, HEX);
#endif
}
}
}
}
}

//主循环
void loop()
{

read_serial_data();//读取串口数据
}

dear, thank you so much for your attention and help.
Could you exemplify how I would do this procedure?
I'm honestly lost.

Hi, @josuefss
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Have you Googled;

TGAM module arduino

Tom.... :smiley: :+1: :coffee: :australia:

I googled "arduino library for neurosky module?" and found 3 hits. You should check each out.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.