TCP/IP examples Help?

Hello,
I am trying to get my feet wet on using XOJO to connect to Lantronix UDS1100 ethernet to serial servers.

Here is a link to the device:
https://www.lantronix.com/products/uds1100-uds1100-poe/

According to Lantronix one must create a direct TCP socket connection via Winsock, and they provided a link to an example here:

https://docs.microsoft.com/en-us/windows/win32/winsock/using-winsock?redirectedfrom=MSDN

This is all pretty new to me, but the UDS essentially has an IP address and a port(10000). I am looking at the Controller options in XOJO and I think I need the TCP connection, but how to use it is where I am getting confused.

I am hoping that someone here could point me in teh right direction as to what controller to use, and maybe an Example in XOJO that would get me communicating with the UDS that I can learn from.

Thanks
JIm

Xojo’s TCPSocket class uses Winsock on Windows.

To establish a connection you create a new socket and assign the IP address/port to the Address/Port properties of the socket:

Dim mysock As New TCPSocket mysock.Port = 10000 mysock.Address = "192.168.1.234" mysock.Connect()

Once the connection is established the TCPSocket will raise its Connected event and you can start reading/writing from the socket.

Great thank you! I will try this out and report back

Thanks. I am going to be doing the actual programming on this, and JimM’s question was what I was just going to post.

This device seems to appear as a switch (not sure if that is the right term) with an ethernet port and two serial ports. It is going to be necessary to direct TCP messages from the LAN side to the serial port side. Not sure which port number the connection is directed to and if it is all done in one step or two (to the TCP port first, then the serial port). The LanTronix docs (which I have not fully reviewed yet) probably has some details.

Jim Wagner

James - This Lantronics device supports TELNET and doesn’t look like it has a REST API nor any API. You may need to TELNET (TCP23) to it, run commands, and return the results. Then you can parse the results into structured data if you need to. Here is a link to my open source TELNET project I created years ago.

HTH

https://github.com/IntelligentVisibility/TELNET_Class_Xojo

[quote=477544:@Andrew Lambert]Xojo’s TCPSocket class uses Winsock on Windows.

To establish a connection you create a new socket and assign the IP address/port to the Address/Port properties of the socket:

Dim mysock As New TCPSocket mysock.Port = 10000 mysock.Address = "192.168.1.234" mysock.Connect()

Once the connection is established the TCPSocket will raise its Connected event and you can start reading/writing from the socket.[/quote]
Sorry for taking so long on this. Where are the PROPERTIES located in the TCP Socket/control? I am trying to set up the above example, but I cannot find where to place this information

JIm

I have been poking around trying to find a step by step tutorial on how to set this up and thought this link was teh answer:
http://www.bkeeney.com/XojoTraining/xojotraining.cgi?video=339

But 404 error - page not found

Is there a tutorial that I can do as a step by step on this? IT’s getting frustrating

Jim

its hard to write a step by step tutorial because WHAT you want to connect to really influences how it has to be set up etc

Fair enough.

Then lets try this tack…How do I set up this:

Dim mysock As New TCPSocket mysock.Port = 10000 mysock.Address = "192.168.1.234" mysock.Connect()

I have 'WINDOW1" on my screen and at the bottom I have ‘TCPSocket1’ Where do I put the above inormation? Where do I put the event? etc…

The rest I think I can figure out

I guess the question is WHEN do you want that socket to try & connect to something ?
When you push a button maybe ?
Lets do that

Add a Generic Button and add the ACTION event to that button (drag it out from the right hand library and the nwhen you have it on the layout right click and add event handler)

If you have an instance that you already dragged onto the layout you do not need the first line

// Dim mysock As New TCPSocket // <<< not needed if you dragged an instance onto the window layout

This line would have created a new socket but you already have one on the layout

In the following lines replace MYSOCK with TCPSocket1 since that is the name of the instance on your layout

TCPSocket1.Port = 10000
TCPSocket1.Address = "192.168.1.234"
TCPSocket1.Connect()

Now, believe it or not … you are mostly done although you wont see anything really happen
But when you click that button the socket instance on the layout will

  1. set the port it is going to try to connect TO // TCPSocket1.Port = 10000
  2. set the address that it is going to try to connect TO // TCPSocket1.Address = “192.168.1.234”
  3. try to connect // TCPSocket1.Connect()

Responding to any data or other errors would be done by adding the EVENT HANDLERS to TCPSocket1
There are several you can add

  • Connected
  • Error
  • SendProgress
  • DataAvailable
  • SendComplete

What specific code should go in these will depend entirely on how the device / computer / etc you’re trying to connect TO expects to communicate

Thanks for teh great starter stuff.

Here is the device I am looking to connect to:
https://www.lantronix.com/products/uds1100-uds1100-poe/

I have teh device configured with an IP address of 10.10.5.188 and it responds on port 10000
According to the manufacturer its pretty straightforward to connect to these servers and pointed me to this:
https://docs.microsoft.com/en-us/windows/win32/winsock/using-winsock?redirectedfrom=MSDN

I dragged the TCPSocket into the workspace and its sitting at teh bottom of the screen. If I right click on it, and select INSPECT I get a panel on the right side where in the BEHAVIOR section I can put the IP address and PORT as opposed to the lines in the button action you have listed above -----Is this ok?

Ok. So I have created a button on Window 1 and added an action. In that action I have the following:

TCPSocket1.Connect()

I would like to put in the window some sort of indicator that I can, say change the color from RED to GREEN based on the connect status.

I have added a CONNECT handler to teh TCPSocket, but heres where I get lost…

How do I:

  1. control teh indicator on the main window to show the CONNECT status?
  2. what kind of indicator should I use for this status?

Thanks
Jim

[quote=485242:@Jim Marquardt]I dragged the TCPSocket into the workspace and its sitting at teh bottom of the screen. If I right click on it, and select INSPECT I get a panel on the right side where in the BEHAVIOR section I can put the IP address and PORT as opposed to the lines in the button action you have listed above -----Is this ok?
[/quote]
Yes

OK good start :slight_smile:

In the code for the CONNECTED event you may get a response (this is the part that I cant tell / test since I dont have one)
In that event you can change the color of “something” or set a status that can be used to control the color of something

Just about anything will do but lets start simple

Drag a CANVAS control out onto the window
dont make it too big
we’ll use this to indicate the status

Add a PROPERTY to your window
Called “Connected” and make it a boolean

in the CANVAS Paint event put

   if self.connected then
      g.forecolor = &c00FF00 // depending on what version youre using this may need to be g.drawingcolor
   else
      g.forecolor = &cFF0000 // depending on what version youre using this may need to be g.drawingcolor
   end if

The in TCPSOCKET1 Connected event put

       self.connected = true

and in TCPSocket1 Error event put

       self.connected = false // this might not strictly be true but this illustrates the point

OK, I mde some progress! It looks like I was able to connect to the Lantronix server! Yipee.

Now to write a simple “Hello World” to the socket to come out the RS232 port on the other side, and also to receive data from the device.

I was trying to display the error codes on a text box, but was unable to do this. How do I upload my project for review? Or to point me in teh right direction in writing and reading

PROGRESS!
Jim

EDIT:
I see Norman replied as I hit POST. I was able to use a button and the buttons ENABLE to show if I connected or not. I will try the canvas button in the morning.

the canvas will just either be red or green - nothing fancy

Yep, thats what I was thinking. The big hurdle is sending and receiving a text string.

Thanks for the help Norman. It’s appreciated

JIm

I tried your example and I see nothing on teh screen where I placed teh canvas. I was expecting to see a little box colored RED or GREEN, but there is nothing in that area.

heh … missed one line in the PAINT event for the canvas (what I get for writing code just in the forums text editor field)

   if self.connected then
      g.forecolor = &c00FF00 // depending on what version youre using this may need to be g.drawingcolor
   else
      g.forecolor = &cFF0000 // depending on what version youre using this may need to be g.drawingcolor
   end if

   g.fillrect 0,0,g.width, g.height

it only SET the color but never used it to fill the canvas with that color :stuck_out_tongue:

AHHH!! I now have a red square in teh spot, but it does not change when the socket connects or disconnects

The self.connect confuses me. Where do you get ‘self’ from?

When debugging, how can I view teh state of the self.connected boolean?

for any of these controls SELF refers to “the window I am on”