C# Telnet into PLC-Motion Controller
Hi- I'd appreciate some advice related to C# Coding of a Telnet Session to a Motion Controller. The Motion Controller allows for a Telnet connection, however these devices give priority to other tasks (namely controlling motor position, velocity, etc.) ahead of servicing Communications. It also requires a login with password.
I have the code generated, and working when I step thru it in the debugger. However, when it runs full throttle, it never gets thru the login process. Are there settings in my C# Code that can "dumb it down" or open it up to be more forgiving?? Here is the test code---
public void StartClient()
{
// data buffer
byte[] bytes = new byte[1024];
try
{
// Connect setup...
IPAddress ipA = IPAddress.Parse("192.168.2.18");
IPEndPoint remoteEP = new IPEndPoint(ipA, 23);
Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect...
try
{
// sender.Blocking = true;
// sender.ReceiveTimeout.Equals(2000);
sender.Connect(remoteEP);
Console.WriteLine("Socket Connection... {0}",
sender.RemoteEndPoint.ToString());
// Start Login Process...
do
{
bytesRec = sender.Receive(bytes);
s = Encoding.ASCII.GetString(bytes, 0, bytesRec);
}
while (!s.Contains("Login: "));
connection += s;
s = null;
// encode & send login msg...
msg = Encoding.ASCII.GetBytes("admin\r");
bytesSent = sender.Send(msg);
// connection += ("admin\r");
msg = null;
do
{
bytesRec = sender.Receive(bytes);
s = Encoding.ASCII.GetString(bytes, 0, bytesRec);
} while (!s.Contains("Password: "));
connection += s;
s = null;
// encode & send msg...
msg = Encoding.ASCII.GetBytes("BlueFusion\r");
bytesSent = sender.Send(msg);
// connection += ("BlueFusion\r");
// receive and process response...
bytesRec = sender.Receive(bytes);
s = Encoding.ASCII.GetString(bytes, 0, bytesRec);
connection += s;
s = null;
// Send Data Request for Registers 500-525...
msg = Encoding.ASCII.GetBytes("500-525\r");
bytesSent = sender.Send(msg);
// receive and process response...
bytesRec = sender.Receive(bytes);
s = Encoding.ASCII.GetString(bytes, 0, bytesRec);
connection += s;
s = null;
// release socket connection...
sender.Shutdown(SocketShutdown.Both);
sender.Close();
richTextBox1.AppendText(connection);
}
catch (ArgumentNullException ane)
{
richTextBox1.AppendText(ane.ToString());
}
catch (SocketException se)
{
richTextBox1.AppendText(se.ToString());
}
catch (Exception e)
{
richTextBox1.AppendText(e.ToString());
}
}
catch (Exception e)
{
richTextBox1.AppendText(e.ToString());
}
}
Thanks in advance...
Lee Clore
Onyx Industries
Makers of Industrial Signal Systems