Ether I/O 24 Users Manual V1.3
Elexol Pty Ltd Version 1.3
Page 31
http://www.elexol.com
E L E
X O L
E L E C T R O N I C
S O L U T I O N S
Basic Programming (continued)
3.
Finding a module’s IP address
If we aren’t using a Fixed IP scheme then we need to find the IP address that the DHCP server has
allocated to the Ether I/O 24 module. To find the module’s IP address on our local network we must
open a UDP socket and listen on that socket for a reply from the module, then broadcast an
Identify
Ether IO 24 Units
command over the network and wait for any Ether I/O 24 modules to respond. To
accomplish this we must add a Winsock component to our program’s form and then at the start of our
program we need to place this code in the Form Load() module. This code will broadcast the IO24
,
Identify Ether IO 24 Units
command over the network and wait for the first Ether I/O 24 module to
respond, following which the Data is read and the IP Address is printed to the debug window.
Private Sub Form_Load()
Winsock1.Protocol = sckUDPProtocol ' Set the protocol to UDP
Winsock1.RemoteHost = "255.255.255.255" ' Set the remote address to broadcast
Winsock1.RemotePort = 2424 ' Set the port to 2424 for the Ether I/O 24 module
Winsock1.Bind ' Bind the local port to receive data
Winsock1.SendData "IO24" ' Send the Identify command to all IO24 units
While Winsock1.BytesReceived = 0 ' Wait for data to Arrive
Wend ' Loop the Wait
N = Winsock1.BytesReceived ' How many bytes arrived
Winsock1.GetData a$, vbString, N ' Read them into a buffer
Debug.Print Winsock1.RemoteHostIP ' Tell the user the IP Address
End Sub
If using the Interrupt mode, rather than waiting for the data to arrive back from the module, we use the
Winsock1_DataArrival Module to process the data when it arrives thereby freeing the program to do
other tasks while waiting for a response. Because we have bound the port with the Winsock1.bind
property, when data arrives it will automatically run the Winsock1_DataArrival event module. To do
something useful with this we put a small piece of code there to print the module’s IP address to the
debug window.
Private Sub Form_Load()
Winsock1.Protocol = sckUDPProtocol ' Set the protocol to UDP
Winsock1.RemoteHost = "255.255.255.255" ‘ Set the remote address to broadcast
Winsock1.RemotePort = 2424 ' Set the port to 2424 for the Ether I/O 24 module
Winsock1.Bind ' Bind the local port to receive data
Winsock1.SendData "IO24" ' Send the Identify command to all IO24 units
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData a$, vbString, bytesTotal ‘ Get the data from the buffer
Debug.Print Winsock1.RemoteHostIP
‘ Show the IP address of the unit
End Sub
Now we know the IP address of our Ether I/O 24 module we can execute other commands to set up the
ports for Input or Output of signals.