Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Winsock TCP message

Status
Not open for further replies.

EinarT

Programmer
Oct 2, 2020
1
NO
I have a component with ip address that I like to send commands with TCP on a local Lan. It has to do with home automation. I like to use VFP9 since that is my programming language. I think the commands can be sent through Winsock. So I need an interface to Winsock. I very much like to have some help with connecting and handling the device from VFP.

Mote about the device (hub) and tcp traffic here:
 
Dear EinarT,

I am not sure if this will work for your device. But, this will give you some ideas I believe.
If you're using TCP/IP to connect, you can try below code snippets.

Code:
tcpClient = CREATEOBJECT('MSWinsock.Winsock.1')
tcpClient.RemoteHost = "192.168.100.123"   && replace with your IP of the device
tcpClient.RemotePort = 77777               && replace with your PORT (you will get this from device documentation)
tcpClient.Close()                          && Just to ensure that we are starting afresh.  I use like this.  Not sure if required.

tcpClient.Connect()                        && Connect to the device.

If connection is successful, tcpClient.State will have a value 7.

Please note that the connection may take some time in some devices or as per your connectivity setups. So, you may have to wait, after issuing the tcpClient.Connect(), until you get tcpClient.State = 7. While doing this, please ensure you're not getting stuck in an infinite loop (if the device fails to connect forever). So, you may have to setup a max time duration also for your wait. The below code should help in that.
Code:
lnStartSecs = SECONDS()
tcpClient.Connect()
DO WHILE tcpClient.State <> 7
    IF (SECONDS() - lnStartSecs) > 5 
        EXIT 
    ENDIF 
	ENDDO
Here it runs for a max of 5 seconds only irrespective of connection is success or not.

Now, if you got State = 7, you can start sending to the device as per your requirement.

Code:
tcpClient.SendData(lcDataToBeSent)      && Replace lcDataToBeSent with your command string.

Then you can receive the reply from the device using
Code:
lcResponseStr = SPACE(255)
tcpClient.GetData(@lcResponseStr)       && you may have to refer the documentation for how to define/initialise lcResponseStr variable.

And finally when you finish, close it.
Code:
tcpClient.Close()
RELEASE tcpClient

Hope this helps in some way.

Rajesh Karunakaran


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top