TCPSocket Write Problem

I want to write a few bytes to a relay board via a TCPSocket. Using the TCP example, I created this simple piece of test code to try to click a relay on.
Here is the format from the documentation on the relay board.

For example:
0x20 - turn the relay on command
0x03 - relay 3
0x32 (50) - 5 seconds (50 * 100ms)
Board will return 0 for success, 1 for failure.
Note - All bytes in a command must be sent in one TCP/IP packet .

And here is my code, again, basically from the TCP Socket example:

[code]'Button is clicked. Send a command to the board
'Read the relay number from TextField1
'create memoryBlock to set function codes for relay write.
’ to turn on relay three bytes are required
dim mb as new memoryblock(3)
mb.littleendian = false

mb.byte(0) = &h20 'turn relay on
mb.byte(1) = &h03 ’ this is relay 3
mb.byte(2) = &h00 ’ Zero will leave it on until turned off

// connect the socket
TCPsocket1.Connect

// while the socket isn’t connected
While Not TCPsocket1.IsConnected
// check to see if the socket got an error
If TCPsocket1.LastErrorCode <> 0 Then
TextArea1.AppendText("Socket Error: " + Str(TCPsocket1.LastErrorCode))
Exit
End If
// poll the socket to let it do its thing
TCPSocket1.Poll
Wend

// if we broke the loop because we’re connected
If TCPsocket1.IsConnected Then
// here would be a great place to do a synchronous read operation…
’ write the data to the port
TCPSocket1.Write(mb)
TCPSocket1.Flush
Else
//The socket broke out of the loop due to an error
TextField1.Text = “Socket Error”
End If

// close the socket
TCPsocket1.Close[/code]

From a forum post I see that a memory block is a good data structure to format my three bites to send. Writing the three bytes to the IP/Port should close relay 3 and cause the board to send back a 0 if successful or a 1 if failed. I also have a Read setup in the DataAvailable Event for the socket, but I never get there. The code runs, I can see a flurry of data by monitoring the IP address with Wireshark, but I don’t see my three data bytes being written to the relay board and the relay doesn’t close.

What am I doing wrong here?

Regards,
Bob Cheek

Are you using the Events in the TCPSocket? What’s the code in each event?

Just connect then write the data from the connected event. And read the bytes in dataavailable.

Don’t loop and/or poll

Bob,
The only Event I am using on the TCPsocket is the DataAvailable to read the single byte reply.

dim rd as String While TCPSocket1.BytesAvailable > 0 ' read in one packet from the queue rd = TCPSocket1.ReadAll TextArea1.AppendText(Cstr(rd)) Wend

As I mentioned I never get there because the relay board isn’t seeing the 3 byte command.

I think you’re doing it wrong but without seeing more detail in a project I can’t tell.

Derk,
That works. Moved my write code to the Connected Event and it worked right off. Made a small change to the Read in the DataAvailable Event and that worked too. I guess the example is more complicated than it needed to be?

[quote=415855:@Robert Cheek]Derk,
That works. Moved my write code to the Connected Event and it worked right off. Made a small change to the Read in the DataAvailable Event and that worked too. I guess the example is more complicated than it needed to be?[/quote]

Most example code seems oldscool. Event based is the way to go.