19
•
Change the
Protocol
property of the
WinSock
control to “0- sckTCPProtocol” (you have already
switched the DS100/EM100 to TCP/IP earlier)
•
Change the
Enabled
property of the CmdSend button to “Disabled” (because the data cannot be
sent unless the connection is established)
•
The TCP/IP is a connection-oriented protocol so we will need to add a button to connect to/
disconnect from the remote host. Add
CmdConnect
CommandButton to your form, set its
Caption
to “Connect” (see
Fig. 4
)
•
Also add another Label named
LblState
- this will be used to track the change in TCP/IP
connection states. Set this Label’s
Caption
to “-------”
•
Add one Timer,
Timer1
, to the form too. Set the Timer’s
Interval
to 100 (i.e. 10 times/second)
•
Add the code below to the
Timer
event of
Timer1
This code does three things. 1) It periodically
updates the status of the TCP/IP connection. 2) Dynamically changes the Caption of the
CmdConnect
button: when the connection is closed (
WinSock.State=0
) the
Caption
reads
“Connect”, when the connection is
not
closed the
Caption
reads “Disconnect”. This way the
button can be used as a “toggle switch”. 3) The code also disables or enables the
CmdSend
button- the data can be sent only when connection is established
Private Sub Timer1_Timer()
'display current connection state
Select Case WinSock.State
Case 0: LblState.Caption = "Closed"
Case 1: LblState.Caption = "Open"
Case 2: LblState.Caption = "Listening"
Case 3: LblState.Caption = "Connection pending"
Case 4: LblState.Caption = "Resolving host"
Case 5: LblState.Caption = "Host resolved"
Case 6: LblState.Caption = "Connecting"
Case 7: LblState.Caption = "Connected"