Page 1 of 2

Send command "string" over tcp/ip

Posted: Wednesday 21st February 2018 8:42pm
by agili
I created a form with one button, my purpose is to send string over tcp/ip, I created this app in VB windows and it works fine but on gambas and linux mint I get some error compiling, plz correct me and thank in advance, the code is :

Public Sub Button1_Click()

Dim ipAddress As String = "192.168.100.237"
Dim port As Integer = 9100
Dim ZPLString As String =

"^XA" &
"~JA" &
"^XZ"
Try
Dim client As New System.Net.Sockets.TcpClient
client.Connect(ipAddress, port)
Dim writer As New System.IO.StreamWriter(client.GetStream())
writer.Write(ZPLString)
writer.Flush()
writer.Close()
client.Close()

Catch ex As Exception

End Try

End

Public Sub Form_Open()

End

Re: Send command "string" over tcp/ip

Posted: Wednesday 21st February 2018 9:59pm
by cogier
Hi agili and welcome to the site.

I am not an expert on VB but I have asked a friend of mine to look at your code, however I can point out a couple of things.

1/. Gambas requires that all 'Dim' statements are declared at the beginning of the routine, you can't just add one in the middle.
2/. 'Catch' does not require an argument. Click on 'Catch' in the IDE and press [F2] for help.
3/. 'Try' only works on one line. Click on 'Try' in the IDE and press [F2] for help.
4/. On this site you can add your code in between the 'gb' tags to get a better display.
code1.png
code1.png (227.85 KiB) Viewed 12112 times
See here viewtopic.php?f=9&t=502

My friend has replied to me as I write this and suggests you look here for help with this http://gambaswiki.org/wiki/comp/gb/stream?nh

Gambas is similar to VB but it is not a clone.

If this does, or does not, help please come back to us and lets us know how you got on.

Re: Send command "string" over tcp/ip

Posted: Thursday 22nd February 2018 10:08am
by agili
Cogier: thank you alot, as I wrote this is my first time using gambas I needed to convert my vb code to gambas so I can use it with ubuntu. honnestly it seemed complicated I stil recieve error when compiling. My purpose is to send these string to a zpl printer using Tcp/ip protocol, I tried to find some example writen in gambas in google but no luck.

strings:
^XA
~JA
^XZ
These are commands usally sent throw terminal and must be a string per line as I wrote.

Re: Send command "string" over tcp/ip

Posted: Thursday 22nd February 2018 12:14pm
by stevedee
agili wrote: Wednesday 21st February 2018 8:42pm I created a form with one button, my purpose is to send string over tcp/ip...
You probably need to start again by creating a Gambas project including the gb.Net component. Then you can add a Button and a Socket to the Form.

Rename the Socket: ClientSocket

Use properties to set IP address and Port:-
ClientSocket.Host = "192.168.100.237"
ClientSocket.Port = "9001"
Add some button code, maybe something like this:-
Public Sub Button1_Click()
  Dim strMessage as String
  
  strMessage = "^XA" & gb.Lf & "~JA" & gb.Lf & "^XZ" & gb.Lf
  ClientSocket.Connect()

  If ClientSocket.Status > Net.Inactive Then 
    Wait 1
    If ClientSocket.Status = Net.Connected Then
      Write #ClientSocket, strMessage, Len(strMessage)
    Else
      Close #ClientSocket
      Me.Text = "Error: Timeout"
    End If
  End If
  
End
Take a look at my post for more details: https://captainbodgit.blogspot.co.uk/20 ... ambas.html

Re: Send command "string" over tcp/ip

Posted: Thursday 22nd February 2018 9:01pm
by agili
stevedee thanks I tried that but stil get error on compiling:

Re: Send command "string" over tcp/ip

Posted: Thursday 22nd February 2018 10:34pm
by jornmo
Just delete Ser ;)

Re: Send command "string" over tcp/ip

Posted: Friday 23rd February 2018 9:16am
by stevedee
Hey Jornmo, any idea why the "If" in the first instance of "End If" in my post above is coloured pink not blue?
EndIf.png
EndIf.png (10.25 KiB) Viewed 12074 times
I'm sure it has to be something obvious.

Re: Send command "string" over tcp/ip

Posted: Friday 23rd February 2018 10:49am
by agili
stevedee , cogier and jornmo many thanks for effort I finally got rid of errors while compiling I also getting used to gambas project. I wil test the code with the host printer and will tell the result. I used this method by stevedee:

Public Sub Button1_Click()

Dim strMessage As String

strMessage = "^XA" & gb.Lf & "~JA" & gb.Lf & "^XZ" & gb.Lf
ClientSocket.Connect()

If ClientSocket.Status > Net.Inactive Then
Wait 1
If ClientSocket.Status = Net.Connected Then
Write #ClientSocket, strMessage, Len(strMessage)
Else
Close #ClientSocket
Me.Text = "Error: Timeout"
End If
End If

End
....
ofcourse I added a gb.net componet to project and a socket to the form.

Re: Send command "string" over tcp/ip

Posted: Friday 23rd February 2018 4:13pm
by cogier
If you are still having problems I see there is an example in the Gambas Farm called 'ClientSocket'. You can access the 'Farm' from the 'Tools' menu in the IDE.

Re: Send command "string" over tcp/ip

Posted: Sunday 25th February 2018 8:17am
by jornmo
stevedee wrote: Friday 23rd February 2018 9:16am Hey Jornmo, any idea why the "If" in the first instance of "End If" in my post above is coloured pink not blue?

EndIf.png

I'm sure it has to be something obvious.
Its the javascript on this site that takes care of the syntax highlighting that is a bit rough on the edges. I will see later if I can fix it. We got it from the gambas-club.de forum.