Data Sheet

Ether I/O 24R Users Manual V1.1
Elexol Pty Ltd Version 1.1 Page 34 http://www.elexol.com
ELEXOL
ELECTRONIC SOL UT IONS
Basic Programming (continued)
4. Basic set up and writing to the ports
To set up the ports for output or basic input there are only 2 registers per port that need writing or
reading. The first of these is the Direction register and the second is the Port register that sets whether
the output pin is high or low. To set the direction register we write to the module with a Port Letter
Prefix of !, telling the module that the information is for the Direction Register, Then we tell the
module which port A, B or C we want to write to and finally we tell the module what value to place in
this register.
For example, to set Port A as all outputs we would write the following code.
Winsock1.SendData "!A" + Chr$(0)
To then Set all of the Port A lines low we would write.
Winsock1.SendData "A" + Chr$(0)
To save code space and network traffic we combine these two commands into a single command.
Winsock1.SendData "!A" + Chr$(0) + "A" + Chr$(0)
5. Reading the Ports
To read the ports is a little more complex as we have to send a read command to the module and then
process the reply. This can be done by the module waiting for the reply or by using the
Winsock1_DataArrival event similar to what we did in the Finding the IP address code. To read Port A
we transmit a Read Port A command and then process the response. The first example code does this
by waiting for the data to arrive back from the module.
Winsock1.SendData "a" ‘ Send the Read Port A Command
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
If N = 2 And Left$(A$, 1) = "A" Then ‘ Confirm that the Data packet is valid
PortA = Asc(Right$(A$, 1)) ‘ Store the value into a register
Debug.Print Str$(PortA) ‘ Tell the user the value read
End If
Using the Winsock1_DataArrival event to read a port.
Winsock1.SendData "a" ‘ Send the Read Port A Command
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData a$, vbString, bytesTotal ‘ Get the data from the buffer
If bytesTotal = 2 And Left$(A$, 1) = "A" Then ‘ Confirm that the Data packet is valid
PortA = Asc(Right$(A$, 1)) ‘ Store the value into a register
Debug.Print Str$(PortA) ‘ Tell the user the value read
End If
End Sub
Obviously in a real program we would not just print these results to a debug window, we would actually
use them to do something useful. To illustrate this point and the operation of the module and it’s
programming, we have produced a series of application demonstration programs on output, input,
EEPROM programming, using the AutoScan functions and interfacing to Clocked Serial devices.