Data Sheet

31
ETHERIO24TCPDATASHEET
CodingExamples
TheexamplecodehasbeenwritteninVisualC#Expressandareavailable
fordownloadfromourwebsitewww.elexol.com
TCPINTERFACEEXAMPLES

OpeningTCPPortandConnecting
//********************************************************************
//TCP Connect function opens TCP Connection to the device
//********************************************************************
private Boolean TCPConnect()
{
if (client == null)
{
client = new TcpClient();
//********************************************************
//Define device PORT as 2424
//********************************************************
_devicePort = 2424;
//********************************************************
//Define IP address that is to be used from textbox
//********************************************************
_deviceIP = IPAddress.Parse(txtIPAddress.Text);
//********************************************************
//Set up IPEndpoint of Ether IO24 that you want to talk with
//********************************************************
IPEndPoint serverEndPoint = new IPEndPoint(_deviceIP,
_devicePort);
//********************************************************
//Try and connect with Ether IO24
//********************************************************
try
{
client.Connect(serverEndPoint); //Connect
TestTextBox.AppendText("\r\n" + "~~~" + "\r\n");
TestTextBox.AppendText("TCP Client Connected to " +
_deviceIP + "\r\n");
client.NoDelay = true;
}
catch (SocketException)
{
// Connection Failed
return false;
}
return true;
}
else //If not connecting
{
try
{
client.Close();
client = null;
TestTextBox.AppendText("TCP Client Disconnected" + "\r\n");
}
catch (SocketException)
{
// Dis-connection Failed
}
return false;
}
}

©
2013ElexolPtyLtd Revision1.3