Send command "string" over tcp/ip

Post your Gambas programming questions here.
agili
Posts: 6
Joined: Wednesday 21st February 2018 8:22pm

Send command "string" over tcp/ip

Post 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
Attachments
Screenshot_2018-02-21_21-29-34.png
Screenshot_2018-02-21_21-29-34.png (63.67 KiB) Viewed 11975 times
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Send command "string" over tcp/ip

Post 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 11965 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.
agili
Posts: 6
Joined: Wednesday 21st February 2018 8:22pm

Re: Send command "string" over tcp/ip

Post 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.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Send command "string" over tcp/ip

Post 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
agili
Posts: 6
Joined: Wednesday 21st February 2018 8:22pm

Re: Send command "string" over tcp/ip

Post by agili »

stevedee thanks I tried that but stil get error on compiling:
Attachments
Screenshot_2018-02-22_21-57-07.png
Screenshot_2018-02-22_21-57-07.png (38.71 KiB) Viewed 11945 times
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: Send command "string" over tcp/ip

Post by jornmo »

Just delete Ser ;)
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Send command "string" over tcp/ip

Post 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 11927 times
I'm sure it has to be something obvious.
agili
Posts: 6
Joined: Wednesday 21st February 2018 8:22pm

Re: Send command "string" over tcp/ip

Post 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.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Send command "string" over tcp/ip

Post 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.
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: Send command "string" over tcp/ip

Post 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.
Post Reply