null characters with TCPSocket

I have been testing Xojo 2014r1. Everything has been good up until now. I need to communicate with a networked gage. I have code in php that works.

Below are snippets of my code. php is working. xojo is not. I can tell from my network gage that the xojo code is making a connecting and not sending any data after (and including) the null character. If I have a long command without a null character, xojo works fine. If there is a null character in the middle of the command, the gage receives everything before the null character. If the null character is first (like my code below), the gage receives nothing other than the connection.

I have tried chrb, flushing the socket, delays,etc. I even created a generic winsock program in VB6 to show me what was being sent and it verifies the problem. Has anyone found a solution for this?

[code]php

$fp = fsockopen(“192.168.1.110”, 10001, $errno, $errstr, 1);
$out = chr(0) . chr(2) . “R” . chr(0);
fwrite($fp, $out);
fclose($fp);

xojo

socket = New TCPSocket

socket.Address = “192.168.1.110”
socket.Port = 10001
socket.Connect

While Not socket.IsConnected
socket.Poll
Wend

If socket.IsConnected Then
strOut = chr(0) + chr(2) + “R” + chrb(0)
socket.Write(strOut)
socket.Poll
End If

socket.Close
[/code]

Make sure for things like Chr(0) you use CHRB

If socket.IsConnected Then
strOut = CHRB(0) + CHRB(2) + “R” + CHRB(0)
socket.Write(strOut)
socket.Poll
End If

Thanks for the response. I tried chrb and it didn’t make a difference. All commands with chr and chrb as long as there is no chr(0) or chrb(0). Unfortunately, the most important command has null these.

Can you paste in a picture of what the hex values of the frame are in your IDE debug?

Forgive me. I am very new to Xojo. Where would I find this frame with hex values? Basically I created a form with a pushbutton that executes the socket code for testing purposes.

Here is a pic of the values:

That is what they should be sending.

Does it make any difference if you put a flush() immediately after the write()?

It is commented out in the picture but I have tried many combinations of flush() and poll().

I even have it working in python with the code below. I guess I will just have to use a shell command to call python scripts (linux).

[code]import socket

s = socket.socket()
s.connect((“192.168.1.151”, 10001))

reset

s.send(chr(0) + chr(2) + “R” + chr(0))

setaddr

s.send(chr(2) + chr(2) + chr(13) + “S” + chr(1) + “301B408B18” + chr(0))
r = s.recv(6)
print "set " + r[/code]

[code]import socket

s = socket.socket()
s.connect((“192.168.1.151”, 10001))

reset

s.send(chr(0) + chr(2) + “R” + chr(0)) <<<<<<< this should be using CHRB

setaddr

s.send(chr(2) + chr(2) + chr(13) + “S” + chr(1) + “301B408B18” + chr(0)) <<<<<<< this should be using CHRB
r = s.recv(6)
print "set " + r
[/code]

EDIT : DOH - missed this is python :stuck_out_tongue:

That is python code :slight_smile: No chrb function in python. I was just showing that it works in another language.

Missed that :stuck_out_tongue:
Its still early here
I can say that I have code thats been running for nearly 8 years now that reads & writes lots of binary data including chrb(0) etc

Can you post a snippet of code that sends data with chrb(0) and receives data back that includes chrb(0)?

I agree that it should work. I’m probably just missing something small that unique to Xojo.

I can’t
I’m certain I can work up some code in two small xojo apps that does this

OK a small receiver as a desktop app

  1. new project - desktop app
  2. add a text area to the window - it will be named textarea1
  3. drag a tcp socket onto the default window - it will by default be named TCPSocket1
  4. add the open event to Window1 as follows

TCPSocket1.Address = "127.0.0.1" TCPSocket1.Port = 9900 TCPSocket1.Listen
6) to the TCPSocket1 DataAvailable event add the following code

[code] dim s as string = me.ReadAll

for i as integer = 1 to lenb(s)

dim c as string = midb(s,i,1)
dim b as integer = ascb(c)
TextArea1.AppendText "[" + hex(b) + "]"

next[/code]

A small sender as a desktop project

  1. new desktop project
  2. add a button to the default window - it will be named button1
  3. drag a tcp socket onto the default window - it will by default be named TCPSocket1
  4. to button1 add the action event as follows

[code] TCPSocket1.Address = “127.0.0.1”
TCPSocket1.Port = 9900

TCPSocket1.Connect

While Not TCPSocket1.IsConnected
TCPSocket1.Poll
Wend

If TCPSocket1.IsConnected Then
dim strOut as string = chr(0) + chr(2) + “R” + chrb(0)
TCPSocket1.Write(strOut)
TCPSocket1.Poll
End If
[/code]

Run the small receiver
Then the small sender
Press the button in the small sender

Well, I could try your example exactly since I can’t run more than one instance of Xojo BUT I modified it send code to my gage. I cut and paste the send code I was using into action events of pushbuttons and changed to the tcpsocket1. It works!

The only differences are using the tcpsocket control and receiving data using its DataAvailable event instead of reading after the send. I don’t understand why the control works and creating the socket in code doesn’t but I guess it doesn’t matter :slight_smile:

Thanks again for the help! That was the last roadblock I had to purchasing Xojo Pro :slight_smile:

[quote=77819:@Aaron Murray]Well, I could try your example exactly since I can’t run more than one instance of Xojo
[/quote]
You only need one with two projects open :stuck_out_tongue: Thats how I tried it out

[quote=77819:@Aaron Murray]BUT I modified it send code to my gage. I cut and paste the send code I was using into action events of pushbuttons and changed to the tcpsocket1. It works!

The only differences are using the tcpsocket control and receiving data using its DataAvailable event instead of reading after the send. I don’t understand why the control works and creating the socket in code doesn’t but I guess it doesn’t matter :slight_smile:

Thanks again for the help! That was the last roadblock I had to purchasing Xojo Pro :)[/quote]
Reading immediately after sending may not have any data available
Sockets are most definitely event driven and dealing with them that way makes the most sense
The simplest way to do that is to subclass it and implement the events in your subclass and then just create & use instances of your subclass

Didn’t know you could have two projects open… Good to know.

I’m just glad I have a working method now :slight_smile: