Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sat May 27, 2017 3:02 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: TCPsocket question
PostPosted: Mon Jan 28, 2013 12:25 pm 
Offline

Joined: Sun Jan 27, 2013 9:50 pm
Posts: 2
I have a piece of equipment which can be controlled by ASCII commands. I have set up a TCPSocket to send data to the device with the following code:

Dim s as TCPSocket
s = New TCPSocket
s.port = val(portField.Text)
s.address = addressField.Text

//Make the call
const timeOut = 5000000
dim toStart as double

toStart = Microseconds
s.Connect

Do
s.Poll
Loop Until (s.IsConnected) or (Microseconds - toStart > timeOut)

If s.IsConnected Then
s.Write sendText.Text + (chr(13)+chr(10))

ListBox1.AddRow ("Connected - Remote Address: " + s.RemoteAddress)

ListBox1.AddRow ("Handle: " + str(s.Handle))

ListBox1.AddRow ("Socket Error: " + str(s.LastErrorCode))

else
Print "Call failed to connect within " + cstr(timeOut / 1000000) + " seconds."
ListBox1.AddRow ("Call failed to connect within " + cstr(timeOut / 1000000) + " seconds.")
spinner.Visible = false
connected = false
ListBox1.AddRow ("Socket Error: " + str(s.LastErrorCode))
s.Write "not connected..." + (chr(13)+chr(10))
s.Disconnect

End If


The code connects and sends the data and stays connected.

I have a disconnect button with the following code:

if s.IsConnected then
s.Disconnect
Print "Disconnecting"
else
Print "can't disconnect, not connected"
end if


When clicking the Disconnect button the program hangs on the line
if s.IsConnected then

on resuming throws a Nil object error.

Clearly I am unclear on the concept here.

Why can't I use methods for the s.object outside of the code that created it?
If the Socket is still connected does the object/socket exist?

How can I reference the TCPSocket object methods like s.Write or s.Disconnect?


Top
 Profile  
 
 Post subject: Re: TCPsocket question
PostPosted: Mon Jan 28, 2013 3:40 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Quote:
Dim s as TCPSocket

This creates an object that is local to the method it is in. It is not accessible from anywhere else, such as a disconnect button. Make the socket a property of the window so you can access it from multiple places in your code.


Top
 Profile  
 
 Post subject: Re: TCPsocket question
PostPosted: Mon Jan 28, 2013 4:07 pm 
Offline

Joined: Sun Jan 27, 2013 9:50 pm
Posts: 2
Thanks for the help, that worked!

What is the best practice for connecting/sending data to an external device?
Is it good or bad to leave the connection open?
This is a situation where commands need to be sent on demand and no other connections will likely be made to the device.
I realize this a rather vague question but any input will be appreciated.


Top
 Profile  
 
 Post subject: Re: TCPsocket question
PostPosted: Mon Jan 28, 2013 4:31 pm 
Offline

Joined: Tue Oct 04, 2005 10:55 am
Posts: 43
Location: Fort Myers, FL
jpkelly wrote:
Thanks for the help, that worked!

What is the best practice for connecting/sending data to an external device?
Is it good or bad to leave the connection open?
This is a situation where commands need to be sent on demand and no other connections will likely be made to the device.
I realize this a rather vague question but any input will be appreciated.


In general, it's neither good nor bad to leave a connection open. It's fine. You'll have to handle the situation where the device has closed the connection, and reconnect, but that's no problem.


Top
 Profile  
 
 Post subject: Re: TCPsocket question
PostPosted: Mon Jan 28, 2013 5:00 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
If you use a synchronous approach, as you have, then it's best to open and close the connection each time - handle all the details in one place. If you go async, and put your code in the socket's events, then it's better to leave the socket open. But that's just my opinion based on reliability vs. overhead.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group