Data Sheet
34
ETHERIO24TCPDATASHEET
//********************************************************************
//TCP Read PortValue function Reads the icoming datastream from the device,
//the data returned is the Port and the Value
//********************************************************************
private byte[] ReadPortValue(NetworkStream clientStream)
{
//********************************************************
// Define variables that are to be used in routine
//********************************************************
byte[] inBuffer = new byte[2];
byte inBufferCount = 0;
System.Threading.Thread.Sleep(100); //wait
//********************************************************
// Read 2 bytes back from stream (Port Read Response)
//********************************************************
inBufferCount = 0;
do
{
inBuffer[inBufferCount++] = (byte)clientStream.ReadByte();
} while (clientStream.DataAvailable);
//********************************************************
// Return the data that has been read back from Device
// Should be Port and the Value
//********************************************************
return inBuffer;
}
ReceivingTCPCommandResponses
TCPSERVERAPPLICATIONTHATHANDLESALLINCOMINGTCPTRAFFIC.
//********************************************************************
// Form_Load Method: Instantiate and start the listening thread
//********************************************************************
private void Form1_Load(object sender, EventArgs e)
{
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
//********************************************************************
// ListenForClient() : This method runs in the listening thread.
// It will loop indefinately waiting for a TCP Connection.
//********************************************************************
private void ListenForClients()
{
try
{
this.tcpListener.Start();
}
catch
{
// Thread did not start.
}
while (true)
{
//************************************************************
// Blocks until a client has connected to the server
//************************************************************
TcpClient client = this.tcpListener.AcceptTcpClient();
System.Diagnostics.Debug.WriteLine("Client Connected");
//************************************************************
// Create a thread to handle communication with connected client
//************************************************************
Thread clientThread =
new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
©
2013ElexolPtyLtd Revision1.3