How to read and understand serial data?

edited January 2017 in Library Questions

I am trying to connect my Neurosky EEG sensor with processing code without using ThinkGear connector. ThinkGear connector is an application which read serial data and transmit over WebSocket so that other application can listen to that WebSocket and use the data. It transmit data like eSense, raw, eyeblink (single) as JSON.

Problem - Using websocket I can connect with the device and receive JSON data but this JSON doesn't contain double eye blink data which is really crucial for me hence I want to write my own ThinkGear like emulator in processing.

PROBLEM PART 1

I have been tying to read serial data using processing and I was successful but now I don't know how to interpret this data. I have been trying to read ThinkGear communication protocol but everything was going over my head. I hope someone will help me to fill this :) :)
There are two important things I need to help to understand are these -

A. Packet Structure B. Packet Parsing Guide

PROBLEM PART 2

I don't understand the use of myPort.buffer(12);. Also what is the point of setting byte[] inBuffer = new byte[8] if it is going to take buffer size as set in the beginning with myPort. What is the point of using myPort.readBytes(inBuffer); when inBuffer give the same values as without using readBytes(). Why myPort.buffer(size) affect the byte[] size even though I hardcoded it to 8 and how come it is taking 12 values even after hardcoding byte[] size to 8?

CODE

    import processing.serial.*;
    Serial myPort;  // Create object from Serial class
    String myString="";  
    String arrayval="";  

    byte[] inBufferfinal = new byte[12]; 
    void setup() {
      size(600, 400); 
      String[] portName = Serial.list();
      for (int i=0; i<portName.length; i++) {
        if (portName[i]!=null) {
          myPort = new Serial(this, portName[i], 9600); // baudrate: 57600, 9600, 115400
          myPort.buffer(12);
          println(portName[i]  + " " + i);
        }
      }
      //frameRate(4);
    }

    void draw() {  
      background(255);
      visualizer(12, inBufferfinal );
      fill(0);
      //textAlign(CENTER);
      //text(myString, width/2, height/2);
    }

    void serialEvent(Serial myPort) {
      myString = "";
      arrayval = ""; 
      byte[] inBuffer = new byte[8];
      while (myPort.available () > 0) {
        inBuffer = myPort.readBytes();
        myPort.readBytes(inBuffer);
        inBufferfinal = inBuffer;
        println(inBuffer, inBuffer.length);
        if (inBuffer != null) {
          for (int i=0; i<inBuffer.length; i++) {
            myString = myString + hex(inBuffer[i])+" , " ;
            arrayval = arrayval+ i +  " , ";
          }
        }
      }
    }

    void visualizer(int lengthofbyte, byte[] val) {
      noStroke();
      fill(0);
      for (int i=0; i<lengthofbyte; i++) {
        int h = val[i];  
        rect(230, i*20+20, h, 20); 
        text(h, 20, i*20+40);
      }
    }

Answers

  • @GoToLoop thanks for the reply but it really didn't help me. The data I am receviing is structured in some given format which I am not able to underestand it. It would be great if you can help me there. In order to understand, they have given some guide which I am not quite able to understand. Please click the links below to see the guides

    1. Packet Structure
    2. Packet Parsing Guide
  • The readBytes function from the serial library gives a good explanation of how to read data, which are the lines you have impkemented in your code, lines 33-35. However, since your data stream does not have a character or flag indicating the end of your packet (as described in the documentation you provided) there is more work you will have to do on your side. Looking at your packet structure, you have to read a larger buffer, larger than 169+4 bytes, the max expected. Then the process to retrieve the data consist in looking for two consecutive bytes that matches 0xAA 0xAA. Then you read the next byte which tells you how many bytes to extract which would contain the data of interest (doi). Then you must read one more byte which contains a checksum value. The checksum value is a value use to check the integrity of your recent read operation.

    So step 1 consist in writing a program that downloads the doi and to verify/cross reference the checksum value to this data.

    After you are done step 1, you can proceed to parsing your doi data, step 2.

    Kf

  • @kfrajer thanks for the reply. I have been trying this but honestly I am still where I started. I think I should close this discussion until I have something concrete to discuss here.

Sign In or Register to comment.