by c.li-1 » 03 May 2015, 15:46
Hi Anders,
Thanks for your reply. Unfortunately, it doesn't help.
I downloaded the examples several days ago. There are calibration, scroll and mouse control examples. But what I need is only the gaze data.
I created a new project in visual studio, included the SDK in references. I copied the "getting data" example on the website into a main( ) program. However, I still got an error that "} expected" at the first "{" in the main() program, though I did close the main().
Actually, I read every topic in the forum. Most of the topics are the same question as mine. But they were always answered with a link to your examples or a simple sentence. I do believe if you could please give more details about how to get the gaze data, it will help a lot of beginners like me. Thank you very much in advance.
Regards,
Chandler
The code I copied from your website is as follows. Please let me know if there is anything wrong.
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Runtime.CompilerServices;
using TETCSharpClient;
using TETCSharpClient.Data;
namespace showgazedata
{
class Program
{
static void Main()
{
private TcpClient socket;
private Thread incomingThread;
private System.Timers.Timer timerHeartbeat;
public bool Connect(string host, int port)
{
try
{
socket = new TcpClient ("localhost", 6555);
}
catch (Exception ex)
{
Console.Out.WriteLine("Error connecting: " + ex.Message);
return false;
}
// Send the obligatory connect request message
string REQ_CONNECT = "{\"values\":{\"push\":true,\"version\":1},\"category\":\"tracker\",\"request\":\"set\"}";
Send(REQ_CONNECT);
// Lauch a seperate thread to parse incoming data
incomingThread = new Thread(ListenerLoop);
incomingThread.Start();
// Start a timer that sends a heartbeat every 250ms.
// The minimum interval required by the server can be read out
// in the response to the initial connect request.
string REQ_HEATBEAT = "{\"category\":\"heartbeat\",\"request\":null}";
timerHeartbeat = new System.Timers.Timer(250);
timerHeartbeat.Elapsed += delegate { Send(REQ_HEATBEAT); };
timerHeartbeat.Start();
return true;
}
private void Send(string message)
{
if (socket != null && socket.Connected)
{
StreamWriter writer = new StreamWriter(socket.GetStream());
writer.WriteLine(message);
writer.Flush();
}
}
public event EventHandler<ReceivedDataEventArgs> OnData;
private void ListenerLoop()
{
StreamReader reader = new StreamReader(socket.GetStream());
bool isRunning = true;
while (isRunning)
{
string response = string.Empty;
try
{
response = reader.ReadLine();
JObject jObject = JObject.Parse(response);
Packet p = new Packet();
//p.rawdata = json;
p.category = (string)jObject["category"];
p.request = (string)jObject["request"];
p.statuscode = (string)jObject["statuscode"];
JToken values = jObject.GetValue("values");
if (values != null)
{
/*
We can further parse the Key-Value pairs from the values here.
For example using a switch on the Category and/or Request
to create Gaze Data or CalibrationResult objects and pass these
via separate events.
To get the estimated gaze coordinate (on-screen pixels):
JObject gaze = JObject.Parse(jFrame.SelectToken("avg").ToString());
double gazeX = (double) gaze.Property("x").Value;
double gazeY = (double) gaze.Property("y").Value;*/
}
// Raise event with the data
if(OnData != null)
OnData(this, new ReceivedDataEventArgs(p));
}
catch (Exception ex)
{
Console.Out.WriteLine("Error while reading response: " + ex.Message);
}
}
}
public class Packet
{
public string time = DateTime.UtcNow.Ticks.ToString();
public string category = string.Empty;
public string request = string.Empty;
public string statuscode = string.Empty;
public string values = string.Empty;
public string rawData = string.Empty;
public Packet() { }
}
public class ReceivedDataEventArgs : EventArgs
{
private Packet packet;
public ReceivedDataEventArgs(Packet _packet)
{
this.packet = _packet;
}
public Packet Packet
{
get { return packet; }
}
}
}
}
}