TuxDroidServer English
Status: Alpha
Brought to you by:
joelmatteotti
**C# program exmeple **
Warning !! The programing of this new project is often modified, it's possible that's this sample don't work with the last version of SVN.color red
It's only a simple X program skeleton to comunicate with TuxDroidServer. With this code, you may be able to create a complete IHM to drive your TuxDroid.
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TuxClient
{
class Program
{
/* connection information */
private static IPEndPoint server = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9595); /*you may change ip adress and listener */
/* create socket */
private static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
/* push on left wing */
private static void onLeftButtonPressed()
{
Console.WriteLine("left wing");
}
/* push on right wing */
private static void onRightButtonPressed()
{
Console.WriteLine("right wing");
}
/* push on head button */
private static void onHeadButtonPressed()
{
Console.WriteLine("push on head button");
}
/* push on remote controle */
private static void onRemoteButtonPressed(string button)
{
Console.WriteLine("remote control: {0}", button);
}
/* battery charger is plug */
private static void onChargerPlugged()
{
Console.WriteLine("battery charger plugged");
}
/* battery charger unplugged */
private static void onChargerUnPlugged()
{
Console.WriteLine("battery charger unplugged");
}
/*dongle connected */
private static void onDongleConnected()
{
Console.WriteLine("Dongle connected");
}
/* dongle disconnected */
private static void onDongleDisConnected()
{
Console.WriteLine("Dongle disonnected");
}
/* read message from server */
private static void ReadServer()
{
while (true)
{
byte[] buffer = new byte[1024];
int iResult = socket.Receive(buffer);
if (iResult > 0)
{
string message = Encoding.ASCII.GetString(buffer, 0, iResult); /* convert bytes to string */
message = message.Replace("\r", "");
message = message.Replace(((char)0).ToString(), ""); /* delete <null> character */
/* message is like #TUXBUTTON or #TUXREMOTE */
if (message.StartsWith("#TUXBUTTON") || message.StartsWith("#TUXREMOTE"))
{
string[] sp = message.Split(':');
if (sp[1].Equals("LEFT"))
onLeftButtonPressed();
else if (sp[1].Equals("RIGHT"))
onRightButtonPressed();
else if (sp[1].Equals("HEAD"))
onHeadButtonPressed();
else
onRemoteButtonPressed(sp[1]);
}
else
{
/* handle another event */
if (message.StartsWith("#") && message.Contains(":"))
{
string[] sp = message.Split(':');
switch (sp[0])
{
case "#TUXCHARGER":
{
if (sp[1].Equals("PLUGGED"))
onChargerPlugged();
if (sp[1].Equals("UNPLUGGED"))
onChargerUnPlugged();
}
break;
case "#TUXDONGLE":
{
if (sp[1].Equals("CONNECTED"))
onDongleConnected();
if (sp[1].Equals("DISCONNECTED"))
onDongleDisConnected();
}
break;
}
}
else
{
Console.WriteLine("server: {0}", message);
}
}
}
}
}
/* send message to server */
private static void sendMessage(string message)
{
socket.Send(Encoding.ASCII.GetBytes(message));
}
public static void Main(string[] args)
{
try
{
socket.Connect(server);
/* register to TuxDroidServer*/
sendMessage("Tux_Key(test)"); /* for more information look at the wiki */
Thread T = new Thread(new ThreadStart(ReadServer));
T.IsBackground = true;
T.Start();
/* Exemple */
string stdin;
while (!(stdin = Console.ReadLine()).Equals("quit"))
{
if (stdin.Equals("flippers up"))
sendMessage("Tux_Flippers(Up)");
if (stdin.Equals("flippers down"))
sendMessage("Tux_Flippers(Down)");
}
}
catch (SocketException se)
{
Console.WriteLine("Impossible de se connecter: " + se.ToString());
}
}
}
}