Appendix c: example tcp program – Applied Motion SV7-Q-EE User Manual
Page 18

6/26/2010
920‐0032a3 eSCL Communication Reference Manual
Page 18
Byte
[] SCLstring =
new
Byte
[receiveBytes.Length - 2];
//
remove
opcode
System.
Array
.Copy(receiveBytes, 2, SCLstring, 0, SCLstring.Length);
receiveString
=
Encoding
.ASCII.GetString(SCLstring);
AddToHistory(receiveString);
}
else
if
(opcode == 99)
// ping response
{
MessageBox
.Show(
"Ping!"
,
"eSCL Utility"
,
MessageBoxButtons
.OK,
MessageBoxIcon
.Information);
}
}
The call back function will not be called unless it is “registered” with the UdpClient object using the
BeginReceive method, as shown below. StartRecvCallback can be called from the Form Load event. It must
also be re‐registered each time it is called (this is to prevent recursion), which is most easily accomplished by
making a call to StartRecvCallback each time you send a packet.
private
void
StartRecvCallback()
{
UdpState
s =
new
UdpState
();
s.e
=
new
IPEndPoint
(
IPAddress
.Any, 0);
s.u = udpClient;
udpClient.BeginReceive(
new
AsyncCallback
(ReceiveCallback), s);
}
This example requires that you declare a class called UdpState as described below.
class
UdpState
{
public
UdpClient
u;
public
IPEndPoint
e;
}
As if this event driven technique wasn’t quirky enough, it also creates a threading error unless the following
statement in included in the form load event:
CheckForIllegalCrossThreadCalls =
false
;
// this must be so for callbacks which
operate in a different thread
Appendix C: Example TCP Program
All example programs are available for download at
elements of the code and what tradeoffs you may encounter.
Visual Basic 6
Even though VB6 is an older language, its refreshing simplicity makes it a compelling choice for quickly
developing an Ethernet application.
To communicate over Ethernet TCP from VB6, you’ll need the Winsock control (MSWINSCK.OCX), which is
included in the Professional and Enterprise editions of the language. To configure an instance of Winsock, you