public void SendState(X360State state)
{
byte[] data = state.GetState();
if (null != this.netduinoPlusAddress)
{
Debug.WriteLine("Sending:" + state.ToString());
#if WINDOWS
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
socketArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(this.netduinoPlusAddress), 9999);
socketArgs.SetBuffer(data, 0, data.Length);
socketArgs.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) { });
socket.SendToAsync(socketArgs);
#endif
}
else
{
#if WINDOWS
try
{
if ((false == IsTheSame(data)) || (null == this.port))
{
if (null == this.port)
{
if (this.framesToIgnore-- <= 0)
{
// if (Array.Exists<String>(SerialPort.GetPortNames(), s => s == this.portName))
{
this.port = new SerialPort(this.portName, 115200/* this.config.ArduinoPortBaud*/, Parity.None, 8, StopBits.One);
this.port.DataReceived += this.DataReceived;
this.port.Open();
Thread.Sleep(2000); // wait for bloody arduino nano.
}
}
}
if (null != this.port)
{
this.port.Write(data, 0, data.Length);
this.lastState = data;
}
}
}
catch (Exception ex)
{
this.port = null;
Debug.WriteLine(ex.Message);
this.framesToIgnore = 60; // too lazy to do some tread sleep magic.
}
#endif
}
}