I am connecting 10 ND+2 together and they all communicate with each other via Enet. I have all of them set static. My problem is that at random times I can't communicate with any one of the ND+2's. The only way I can get the device to communicate is to reset the controller by unplugging it.
Is there a way to prevent this?
Is there a way to open a socket connection and try to send to it but if the connection isn't available then just ignore the connection? (I tried putting a "try-catch" in the code but the controller still hangs if there is no endpoint)
I'm new to sockets so if you know of a better way to do this I would greatly appreciate the advice.
Here is the way I'm doing the connections:
string rawData = "";
using (Socket commSocket = socket.Accept())
{
while (!commSocket.Poll(10, SelectMode.SelectRead)) ;
led.Write(true);
int bytesSent = commSocket.Available;
if (bytesSent > 0)
{
byte[] bytes = new byte[bytesSent];
int bytesReceived = commSocket.Receive(bytes, bytesSent, SocketFlags.None);
if (bytesReceived == bytesSent)
{
rawData = (new string(Encoding.UTF8.GetChars(bytes)));
}
}
char delimiter = ' ';
string[] words;
words = rawData.Split(delimiter);
sequence = words[0];
red = byte.Parse(words[1]);
green = byte.Parse(words[2]);
blue = byte.Parse(words[3]);
LEDChain = words[4];
//speednMS = int.Parse(words[5]);
led.Write(false);
try
{
using (Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
sendSocket.SendTimeout = 1000;
sendSocket.Connect(EPCanIP);
sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
sendSocket.Close();
}
using (Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
sendSocket.SendTimeout = 1000;
sendSocket.Connect(EPFloorIP);
sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
sendSocket.Close();
}
using (Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
sendSocket.SendTimeout = 1000;
sendSocket.Connect(EPRibsIP);
sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
sendSocket.Close();
}
using (Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
sendSocket.SendTimeout = 1000;
sendSocket.Connect(EPRevealIP);
sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
sendSocket.Close();
}
using (Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
sendSocket.SendTimeout = 1000;
sendSocket.Connect(EPGreenScreenIP);
sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
sendSocket.Close();
}
using (Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
sendSocket.SendTimeout = 1000;
sendSocket.Connect(EPWelcomeIP);
sendSocket.Send(Encoding.UTF8.GetBytes("SetColorAll");
sendSocket.Close();
}
}
catch (Exception)
{
Debug.Print("Socket did not connect");
}