Applied Motion RS-232 User Manual

Page 278

Advertising
background image

278

920-0002 Rev. I
2/2013

Host Command Reference

To receive a response, you will need to place some code in the

Winsock_DataArrival event. This

event is automatically declared as soon as you add a Winsock control to your form. The DataArrival event will
automatically trigger each time a packet is received. The code below extracts the SCL response from the UDP
payload and displays it in a message box.

Private Sub

Winsock1_DataArrival(ByVal bytesTotal As

Long

)

Dim udpData() As

Byte

, n As

Integer

Dim hexbyte As String, packetID As

Long

, SCLrx As

String

Winsock1.GetData udpData

‘ remotehost gets clobbered when packet rec’d,

‘ next line fixes it

Winsock1.RemoteHost = Winsock1.RemoteHostIP

‘ first 16 bits of packet are the ID (opcode)

If

UBound(udpData) >= 1

Then

packetID = 256 * udpData(0) + udpData(1)

If

packetID = 7

then

‘ SCL response

SCLrx = “”

For

n = 2 To UBound(udpData)

SCLrx = SCLrx & Chr(udpData(n))

Next

n

MsgBox SCLrx

End If

End If

End Sub

C#.NET

The .NET languages are Microsoft’s modern, object oriented Windows application building tools and include

robust Ethernet support. We present this example in C#.

Make sure your project includes this line, providing access to an Ethernet socket:

using

System.Net.Sockets;

In your form header you must declare a UdpClient object and create an instance, which can be done in

the same line. The local port number is included in the “new UdpClient” call. This is the port number that will be
reserved on the PC for your application.

static

UdpClient

udpClient =

new

UdpClient

(7777);

To open the connection, invoke the Connect method, specifying the drive’s IP address and port number:

udpClient

.Connect(“192.168.0.130”, 7775);

To send “RV” to the drive:

//create a string loaded with the SCL command

Byte

[] SCLstring =

Encoding

.ASCII.GetBytes(“RV”);

// create a byte array that will be used for the actual

// transmission

Byte

[] sendBytes =

new

Byte

[SCLstring.Length + 3];

// insert opcode (07 is used for all SCL commands)

sendBytes[0] = 0;

sendBytes[1] = 7;

// copy string to the byte array

System.

Array

.Copy(SCLstring, 0, sendBytes, 2, SCLstring.Length);

// insert terminator

sendBytes[sendBytes.Length - 1] = 13;

// CR

Advertising
This manual is related to the following products: