Trouble sending binary file with TCPClient()

I am attempting to build a Plugin to communicate with my Kasa HS110 Energy plugs. I originally developed a Java program I can use and it works fine. I have been converting it into JS for HR. For the HS110 the command string must be encoded and sent with a binary file. My Plugin function to encode the command string works, I can send strings to it and compare the returned byte[] Array with the byte[] Array generated from my Java version and they are identical.

In my Java version I open a socket and use I/O streams without any issues but when I send the same data with the TCPClient() I get the error “No public methods with the specified arguments were found.”

This is a snippet of how it should work:
// Send command string to function to encode into a byte[]Array, then send
Packed=Pack("{“system”:{“set_relay_state”:{“state”:0}}}");
tcp.send(Packed);

After searching Plugins written by other people I saw a couple of ways commands were sent so I tried building the data manually and sending it. Either I would get the same error or sometimes no error but the command never worked with the plug. Here are some of the ways I tried.

var sendcommand=[0,0,0,42,-48,-14,-127,-8,-117,-1,-102,-9,-43,-17,-108,-74,-59,-96,-44,-117,-7,-100,-16,-111,-24,-73,-60,-80,-47,-91,-64,-30,-40,-93,-127,-14,-122,-25,-109,-10,-44,-18,-34,-93,-34,-93];
tcp.send(sendcommand);

tcp.send(“0,0,0,42,-48,-14,-127,-8,-117,-1,-102,-9,-43,-17,-108,-74,-59,-96,-44,-117,-7,-100,-16,-111,-24,-73,-60,-80,-47,-91,-64,-30,-40,-93,-127,-14,-122,-25,-109,-10,-44,-18,-34,-93,-34,-93”);

tcp.send(“0x00,0x00,0x00,0x2A,0xD0,0xF2,0x81,0xF8,0x8B,0xFF,0x9A,0xF7,0xD5,0xEF,0x94,0xB6,0xC5,0xA0,0xD4,0x8B,0xF9,0x9C,0xF0,0x91,0xE8,0xB7,0xC4,0xB0,0xD1,0xC0,0xE2,0xD8,0xA3,0x81,0xF2,0x86,0xE7,0x93,0xF6,0xD4,0xEE,0xDE,0xA3,0xDE,0xA3”);

tcp.send("\x00\x00\x00\x2A\xD0\xF2\x81\xF8\x8B\xFF\x9A\xF7\xD5\xEF\x94\xB6\xC5\xA0\xD4\x8B\xF9\x9C\xF0\x91\xE8\xB7\xC4\xB0\xD1\xC0\xE2\xD8\xA3\x81\xF2\x86\xE7\x93\xF6\xD4\xEE\xDE\xA3\xDE\xA3");

tcp.send("\0x00\0x00\0x00\0x2A\0xD0\0xF2\0x81\0xF8\0x8B\0xFF\0x9A\0xF7\0xD5\0xEF\0x94\0xB6\0xC5\0xA0\0xD4\0x8B\0xF9\0x9C\0xF0\0x91\0xE8\0xB7\0xC4\0xB0\0xD1\0xC0\0xE2\0xD8\0xA3\0x81\0xF2\0x86\0xE7\0x93\0xF6\0xD4\0xEE\0xDE\0xA3\0xDE\0xA3");

I started my Plugin by copying the code from the TCP Client example on The Home Remote website into the Plugin I created in designer. Is this the right way to start? or did I have to import a TCP Client Plugin and add my code to it?

Wireshark confirms that the binary data is never sent but an ASCII string does go but will do nothing with the Kasa device.

What can I do?

Jerry

It’s best to send the data over as raw bytes instead of a string. Here is an example showing how to do that.

If you want to send it as a string then you’ll probably have to manually set the encoding option to NULL because it defaults to UTF8 for string data.

I have made some progress. When I originally sent the byte[]Array that I encoded I found that each element was being sent as a signed value. Not being familiar with JavaScript I searched for quite a while looking for a function and gave up. If JavaScript has a direct way to convert please let me know.

I added the lines shown below and my array values are now all unsigned.

for(var x=4;x<WorkArray.length;x++){       // Loop thru byte array to encode with Key
		WorkArray[x]=WorkArray[x] ^ Key;   // XOR byte with Key and assign new value back into byte array
		Key=WorkArray[x];                  // Update rolling Key value
		if(WorkArray[x]<0){                // Added- If value <0 then bit 7 is set
				temp=WorkArray[x]&0x7f;    // Added- Perform bitwise AND to clear bit 7, store remaining bits in temp variable
				WorkArray[x]=temp+128;     // Added- Update byte array value to new Unsigned value
		}                                  // Added- Close 'if - then' statement 
}                                          // Close 'for - next' loop

Now my commands work to turn the plug off/on but only once. After sending one command I have to stop the designer simulator and can restart it for one more command. A quick look with Wireshark shows the plug sending back a packet with the RST bit turned on. This is new territory for me so if you have any ideas let me know.

Thanks,
Jerry

edit: Found correct way to display code in post.

I don’t know that I would even bother writing a convert function. Just send raw hex data like that example I shared.

In the example the commands are pre-determined as they don’t change. I need to be able to dynamically configure commands and encode them as some of the commands will vary. That part works now and it was just a couple of lines of code to fix it so I’m happy and moving on to see why I only can send one command at a time.