Tuesday, February 5, 2008

Day One - Getting the gaze position

The SMI IViewX RED eye tracker steams the gaze coordinates on a UPD stream configured on port 4444. The following code is how I managed to hook the C# client up to access the stream and simply print it to the console window. This is the initial version that concluded the first day. I got the gaze coordinates from the tracker into my C# code. Good enough.

First output from the UPD Eye Tracker client/server program

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace GazeStar
{

class EyeTrackerServer
{
private const int udpPort = 4444;
public Thread UDPThread;

public EyeTrackerServer()
{
try
{
UDPThread = new Thread(new ThreadStart(StartListen));
UDPThread.Start();
Console.WriteLine("Started thread...");
}
catch (Exception e)
{
Console.WriteLine("An UDP error occured.." + e.ToString());
UDPThread.Abort();
}
}


public void StartListen()
{
IPHostEntry localHost;

try
{

Socket soUdp = new Socket(
AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);

try
{
Byte[] localIp = { 127, 0, 0, 1 };
IPAddress localHostIp = new IPAddress(localIp);
localHost = Dns.GetHostEntry(localHostIp);
}

catch (Exception e)
{
Console.WriteLine("Localhost not found!" + e.ToString());
return;
}

IPEndPoint localIpEndPoint = new IPEndPoint(
localHost.AddressList[0],
udpPort);
soUdp.Bind(localIpEndPoint);

String datareceived = "";


while ( true )
{
Byte[] received = new byte[256];
IPEndPoint tmpIpEndPoint = new IPEndPoint(localHost.AddressList[0],
udpPort);

EndPoint remoteEp = (tmpIpEndPoint);
int bytesReceived = soUdp.ReceiveFrom(received, ref remoteEp);

datareceived = System.Text.Encoding.ASCII.GetString(received);
Console.WriteLine("Sample client is connected via UDP!");

if (datareceived.Length > 0)
Console.WriteLine(datareceived);
}
}
catch (SocketException se)
{
Console.WriteLine("A socket error has occured: " + se.ToString());
}
}


static void Main(string[] args)
{
EyeTrackerServer eyesrv = new EyeTrackerServer();
}

}
}

No comments: