
6/26/2010
920‐0032a3eSCLCommunicationReferenceManual Page18
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);
}
}
Thecallbackfunctionwill notbecalledunlessitis“registered”withtheUdpClientobjectusingthe
BeginReceivemethod,asshownbelow.StartRecvCallbackcanbecalledfromtheFormLoadevent.Itmust
alsobere‐registeredeachtimeitiscalled(thisistopreventrecursion),whichismosteasilya ccomplishedby
makingacalltoStartRecvCallbackeachtimeyousendapacket.
private void StartRecvCallback()
{
UdpState s = new UdpState();
s.e = new IPEndPoint(IPAddress.Any, 0);
s.u = udpClient;
udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), s);
}
ThisexamplerequiresthatyoudeclareaclasscalledUdpStateasdescribedbelow.
class UdpState
{
public UdpClient u;
public IPEndPoint e;
}
Asifthiseventdriventechniquewasn’tquirky enough,italsocreatesathreadingerrorunlessthefollowing
statementinincludedintheformloadevent:
CheckForIllegalCrossThreadCalls = false; // this must be so for callbacks which
operate in a different thread
AppendixC:ExampleTCPProgram
Allexampleprogramsareavailablefordownloadatwww.applied‐
motion.com/ampinfo/software/samplecode/.Youshouldstillreadthissectionsothatyouunderstandthekey
elementsofthecodeandwhattradeoffsyoumayencounter.
VisualBasic6
EventhoughVB6isanolderlanguage,itsrefreshingsimplicitymakesitacompellingchoiceforquickly
developinganEthernetapplication.
TocommunicateoverEthernetTCPfromVB6,you’llneedtheWinsockcontrol(MSWINSCK.OCX),whichis
includedintheProfessionalandEnterpriseeditionsofthelanguage.ToconfigureaninstanceofWinsock,you
Kommentare zu diesen Handbüchern