Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Added socket client to receive eye tracking data
Browse files Browse the repository at this point in the history
  • Loading branch information
MPIBR-kretschmerf committed Jan 31, 2017
1 parent fa1d47b commit 8870fd4
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 61 deletions.
124 changes: 63 additions & 61 deletions AudioGameGUI.pro
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
#-------------------------------------------------
#
# Project created by QtCreator 2016-07-26T13:26:15
#
#-------------------------------------------------

# NOTE: COMPILES UNDER WINDOWS ONLY BECAUSE OF THE
# ACTIVE-X COM OBJECT TDT REQUIRES!! :-(

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
DEFINES += APP_VERSION=\\\"0.1\\\"
RESOURCES = application.qrc
RC_FILE = app.rc

QT += axcontainer
TYPELIBS = $$system(dumpcpp -getfile {D323A622-1D13-11D4-8858-444553540000})

isEmpty(TYPELIBS) {
message("TDT library not found!")
} else {
TYPELIBS = $$system(dumpcpp {D323A622-1D13-11D4-8858-444553540000})
HEADERS += rpcoxlib.h
SOURCES += rpcoxlib.cpp
}

LIBS += -L"C:\\Program Files (x86)\\National Instruments\\Shared\\ExternalCompilerSupport\\C\\lib64\\msvc" \
-lNIDAQmx

TARGET = AudioGameGUI
TEMPLATE = app

SOURCES += main.cpp\
NIDAQmxInterface.cpp \
StateMachineController.cpp \
Mainwindow.cpp \
TimerDialog.cpp \
TDTInterface.cpp \
trialseq.cpp \
LogFileWriter.cpp \
AboutDialog.cpp


HEADERS += \
NIDAQmxInterface.h \
StateMachineController.h \
TimerDialog.h \
Mainwindow.h \
SettingStructures.h \
TDTInterface.h \
trialseq.h \
LogFileWriter.h \
AboutDialog.h

FORMS += mainwindow.ui \
timerdialog.ui \
AboutDialog.ui

DISTFILES +=
#-------------------------------------------------
#
# Project created by QtCreator 2016-07-26T13:26:15
#
#-------------------------------------------------

# NOTE: COMPILES UNDER WINDOWS ONLY BECAUSE OF THE
# ACTIVE-X COM OBJECT TDT REQUIRES!! :-(

QT += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
DEFINES += APP_VERSION=\\\"0.1\\\"
RESOURCES = application.qrc
RC_FILE = app.rc

QT += axcontainer
TYPELIBS = $$system(dumpcpp -getfile {D323A622-1D13-11D4-8858-444553540000})

isEmpty(TYPELIBS) {
message("TDT library not found!")
} else {
TYPELIBS = $$system(dumpcpp {D323A622-1D13-11D4-8858-444553540000})
HEADERS += rpcoxlib.h
SOURCES += rpcoxlib.cpp
}

LIBS += -L"C:\\Program Files (x86)\\National Instruments\\Shared\\ExternalCompilerSupport\\C\\lib64\\msvc" \
-lNIDAQmx

TARGET = AudioGameGUI
TEMPLATE = app

SOURCES += main.cpp\
NIDAQmxInterface.cpp \
StateMachineController.cpp \
Mainwindow.cpp \
TimerDialog.cpp \
TDTInterface.cpp \
trialseq.cpp \
LogFileWriter.cpp \
AboutDialog.cpp \
SocketClient.cpp


HEADERS += \
NIDAQmxInterface.h \
StateMachineController.h \
TimerDialog.h \
Mainwindow.h \
SettingStructures.h \
TDTInterface.h \
trialseq.h \
LogFileWriter.h \
AboutDialog.h \
SocketClient.h

FORMS += mainwindow.ui \
timerdialog.ui \
AboutDialog.ui

DISTFILES +=
12 changes: 12 additions & 0 deletions LogFileWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ void LogFileWriter::onWrite(QString text, uint id, uint counter, QString timeStr
out << text << "\t" << id << "\t" << counter << "\t" << timeString << "\n";
}
}

void LogFileWriter::onWriteTrackingResult(QVector<double> trackingResult)
{
if(logFile.isOpen()){
QTextStream out(&logFile);
out << "Tracking";
for(double r: trackingResult){
out << "\t" << r;
}
out << "\n";
}
}
2 changes: 2 additions & 0 deletions LogFileWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QDataStream>
#include <QDebug>
#include <QTime>
#include <QVector>

class LogFileWriter : public QObject
{
Expand All @@ -21,6 +22,7 @@ private slots:
void onOpen(QString fileName);
void onClose();
void onWrite(QString text, uint id, uint counter, QString timeString);
void onWriteTrackingResult(QVector <double>);
};

#endif // LOGFILEWRITER_H
121 changes: 121 additions & 0 deletions SocketClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "SocketClient.h"

SocketClient::SocketClient(QWidget *parent)
{
tcpSocket = new QTcpSocket();

inBuffer.open(QIODevice::ReadWrite);
inStream.setDevice(&inBuffer);
inStream.setByteOrder(QDataStream::LittleEndian);
inStream.setVersion(QDataStream::Qt_5_2);

port = 20001;
hostname = "127.0.0.1";
this->connectToHost();
}

void SocketClient::onReadData()
{
quint32 packetSize;
quint32 packetType;

inBuffer.seek(0);

/*Read packet size*/
readMax(tcpSocket, sizeof(packetSize));
inBuffer.seek(0);
inStream >> packetSize;

/*Read packet type*/
readMax(tcpSocket, sizeof(packetSize)+sizeof(packetType));
inBuffer.seek(sizeof(packetSize));
inStream >> packetType;

/*Read content*/
readMax(tcpSocket, packetSize);
inBuffer.seek(sizeof(packetSize)+sizeof(packetType));

int dataSize = packetSize-(sizeof(packetSize) + sizeof(packetType));

switch(packetType){
case SOCKET_FILENAME: {
char* data = new char[dataSize];
inStream >> data;
QString fileName(data);
emit(socketFileName(fileName));
qDebug()<<"Socket: filename" << fileName;
}
break;
case SOCKET_START: {
quint64 timeStamp;
inStream >> timeStamp;
emit(socketStartTimeStamp(timeStamp));
qDebug()<< "Socket: start timeStamp: " << timeStamp;
}
break;
case SOCKET_STOP: {
quint64 timeStamp;
inStream >> timeStamp;
emit(socketStopTimeStamp(timeStamp));
qDebug()<< "Socket: stop timeStamp: " << timeStamp;
}
break;
case SOCKET_TIMESTAMP: {
quint64 timeStamp;
inStream >> timeStamp;
emit(socketTimeStamp(timeStamp));
qDebug()<< "Socket: timeStamp: " << timeStamp;
}
break;
case SOCKET_TRACKINGRESULT: {
std::vector<double> data;
data.resize(dataSize / sizeof(double));
for(int i = 0; i< dataSize / sizeof(double); i++){
inStream >> data[i];
}
QVector<double> trackingResult = QVector<double>::fromStdVector(data);
emit(socketTrackingResult(trackingResult));
}
break;
}
inBuffer.buffer().clear();
}

void SocketClient::connectToHost()
{
tcpSocket->abort();
tcpSocket->connectToHost(hostname,
port);
if(!tcpSocket->waitForConnected(5000)){
qDebug() << "Socket: Connection error: " << tcpSocket->errorString();
}else{
connect(tcpSocket, SIGNAL(connected()), this, SLOT(onConnected()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onReadData()), Qt::QueuedConnection);
}
}

void SocketClient::onConnected()
{
qDebug()<< "Socket: Connected";
}

void SocketClient::onDisconnected()
{
qDebug()<< "Socket: Disconnected";
}

void SocketClient::onSocketError()
{
qDebug()<< "Socket: Error";
}

void SocketClient::readMax(QIODevice *io, int n)
{
while(inBuffer.size() < n){
if(!io->bytesAvailable()){
io->waitForReadyRead(30000);
}
inBuffer.write(io->read(n - inBuffer.size()));
}
}
58 changes: 58 additions & 0 deletions SocketClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef SOCKETCLIENT_H
#define SOCKETCLIENT_H
#include <QDialog>
#include <QTcpSocket>
#include <QDataStream>
#include <QtWidgets>
#include <QtNetwork>
#include <QTimer>

class QComboBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QTcpSocket;
class QNetworkSession;

/*Parameters used as Type in outgoing packets*/
#define SOCKET_TIMESTAMP 0
#define SOCKET_START 1
#define SOCKET_STOP 2
#define SOCKET_FILENAME 3
#define SOCKET_TRACKINGRESULT 4

class SocketClient : public QDialog
{
Q_OBJECT

public:
explicit SocketClient(QWidget *parent = Q_NULLPTR);
void connectToHost();

private slots:
void onReadData();
void onConnected();
void onDisconnected();

void onSocketError();

private:
QTcpSocket *tcpSocket;
QString hostname;
int port;
void readMax(QIODevice *io, int n);
QBuffer outBuffer;
QBuffer inBuffer;
QDataStream outStream; /*To serialize outgoing data*/
QDataStream inStream; /*To serialize incoming data*/

signals:
void socketTimeStamp(quint64 timeStamp);
void socketStartTimeStamp(quint64 timeStamp);
void socketStopTimeStamp(quint64 timeStamp);
void socketFileName(QString fileName);
void socketTrackingResult(QVector<double> trackingResult);

};

#endif // SOCKETCLIENT_H

0 comments on commit 8870fd4

Please sign in to comment.