Send command "string" over tcp/ip

Post

Posted
Rating:
#1 (In Topic #157)
Trainee
 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

Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.
Image

(Click to enlarge)

See here http://forum.gambas.on…e/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.
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
 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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
stevedee is in the usergroup ‘Regular’

agili said

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:-

Code (gambas)

  1. ClientSocket.Host = "192.168.100.237"
  2. ClientSocket.Port = "9001"
  3.  

Add some button code, maybe something like this:-

Code (gambas)

  1. Public Sub Button1_Click()
  2.   Dim strMessage as String
  3.  
  4.   strMessage = "^XA" & gb.Lf & "~JA" & gb.Lf & "^XZ" & gb.Lf
  5.   ClientSocket.Connect()
  6.  
  7.   If ClientSocket.Status > Net.Inactive Then
  8.     Wait 1
  9.     If ClientSocket.Status = Net.Connected Then
  10.       Write #ClientSocket, strMessage, Len(strMessage)
  11.     Else
  12.       Close #ClientSocket
  13.       Me.Text = "Error: Timeout"
  14.     End If
  15.   End If
  16.  
  17.  

Take a look at my post for more details: Captain Bodgit: Using Sockets with Gambas
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee
stevedee thanks I tried that but stil get error on compiling:

Code (gambas)

  1.  

Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
jornmo is in the usergroup ‘Regular’
Just delete Ser  ;)

Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
stevedee is in the usergroup ‘Regular’
Hey Jornmo, any idea why the "If" in the first instance of "End If" in my post above is coloured pink not blue?

Image

(Click to enlarge)


I'm sure it has to be something obvious.
Online now: No Back to the top

Post

Posted
Rating:
#8
Trainee
 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.
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Guru
cogier is in the usergroup ‘Guru’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Regular
jornmo is in the usergroup ‘Regular’

stevedee said

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.

Online now: No Back to the top

Post

Posted
Rating:
#11
Avatar
Regular
stevedee is in the usergroup ‘Regular’

jornmo said

…Its the javascript on this site that takes care of the syntax highlighting…

Thanks Jornmo, don't waste any time on it, I was just curious. I couldn't see any non-printing characters or anything unusual that might cause it within the text block.
Online now: No Back to the top

Post

Posted
Rating:
#12
Trainee
Cogier: I compiled the code with no errors but got time out and no reaction from print server, I started the app and started terminal , used the command : netstat -at , my specified port did not appear at all no communication. By the way if I use the app I created in windows with VB (run with wine on linux mint 18) I get response with the print server and can send commands successfully. I am stuck.
Online now: No Back to the top

Post

Posted
Rating:
#13
Avatar
Guru
cogier is in the usergroup ‘Guru’
Can you post the VB code you are using with Wine that works please.
Online now: No Back to the top

Post

Posted
Rating:
#14
Trainee
Cogier: This code is writen in visual Basic 2017 on windows 10 and perfectly works:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim ipAddress As String = "192.168.100.237"
        Dim port As Integer = 9100
        Dim ZPLString As String =
        "^XA" &
        "~JA" &
        "^XZ"
        Try
   'Open Connection
            Dim client As New System.Net.Sockets.TcpClient
            client.Connect(ipAddress, port)
 'Write ZPL String to Connection
            Dim writer As New System.IO.StreamWriter(client.GetStream())
            writer.Write(ZPLString)
            writer.Flush()
   'Close Connection
            writer.Close()
            client.Close()
        Catch ex As Exception
  End Try

  End Sub
Online now: No Back to the top

Post

Posted
Rating:
#15
Avatar
Regular
stevedee is in the usergroup ‘Regular’

agili said

…I started the app and started terminal , used the command : netstat -at , my specified port did not appear at all no communication…

Please put a breakpoint at the line:-

Code (gambas)

  1. If ClientSocket.Status > Net.Inactive Then
…and then run the code in the Gambas IDE.

If the code does not stop at the breakpoint, then there is an error and you should see info in the lower window in the IDE.

If the code stops at the breakpoint, use netstat -at in a terminal and see if the port is open.

The example I gave you is very basic. In my blog post is does say that you should add error detection. Also, in my example above, the string commands may not be correct, so the socket may just be opening and then closing (as expected), so you wont see it on netstat unless you pause the code.

The command string may need to be changed (e.g. to include CR + LF). But the first step is to ensure the socket is working as expected.

EDIT: you don't need to compile the code each time, just click the Run button in the IDE to test your code.
Online now: No Back to the top

Post

Posted
Rating:
#16
Trainee
Hi Agili,

Myself and Charlie have done some testing for you, the issue is the iptables firewall on linux mint.

Try these steps:
1) Turn Off ufw firewall > Status: Off (can be done from gufw "Firewall Configuration")

In Terminal:
  1. sudo iptables –flush
  2. sudo iptables -X
  3. sudo iptables –list (3 sections should be shown all saying: Accept)

5) Restart PC

For more information about the iptables and ufw firewall, see this link:
https://help.ubuntu.com/community/IptablesHowTo

 Cheers
Matt
Online now: No Back to the top
1 guest and 0 members have just viewed this.