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.
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
// 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))
// poll the socket to let it do its thing
// 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
//The socket broke out of the loop due to an error
TextField1.Text = “Socket Error”
// close the socket
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?