Send command "string" over tcp/ip
Posted
#1
(In Topic #157)
Trainee
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
Posted
Guru

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.
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.
Posted
Trainee
strings:
^XA
~JA
^XZ
These are commands usally sent throw terminal and must be a string per line as I wrote.
Posted
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)
- ClientSocket.Host = "192.168.100.237"
- ClientSocket.Port = "9001"
Add some button code, maybe something like this:-
Code (gambas)
Take a look at my post for more details: Captain Bodgit: Using Sockets with Gambas
Posted
Trainee
Code (gambas)
Posted
Regular

Posted
Regular

I'm sure it has to be something obvious.
Posted
Trainee
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.
Posted
Guru

Posted
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.
Posted
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.
Posted
Trainee
Posted
Guru

Posted
Trainee
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
Posted
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:-
…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.
Posted
Trainee
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:
- sudo iptables –flush
- sudo iptables -X
- 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
Matt
1 guest and 0 members have just viewed this.






