Page 1 of 1

Sending hex values to socket.

Posted: Sat Feb 08, 2014 6:32 pm
by JeffreyP
Hello everyone,

I'm trying to send hex values to a socket. I am working on an app that will control wireless RGB lightbulbs. They come with a wifi bridge that takes commands on a UDP socket.

There are more details of the API on the LimitlessLED Dev website, but I'm not allowed to post links.

All commands are three bytes. For example I need to send a TCP/IP UDP packet of: 0x42 0x00 0x55 to turn all the lights on.

I know i need to open a socket connection or do a write to socket, but how do I get Livecode to send a hex value like 0x42? Does it involve numtobyte? If someone can point me in the right direction for getting live code to send those commands, I would appreciate it.

Re: Sending hex values to socket.

Posted: Mon Feb 10, 2014 1:24 pm
by Mark
Hi Jeffrey,

Are you sure that you should send hex values? Probably, you have to send binary data and the documentation gives the hex values for the binary data. Probably, you can use numToByte (or numToChar).

Code: Select all

put numToChar(0x42) & NULL & numToChar(0x55) into myData
write myData to socket mySocket
Kind regards,

Mark

Re: Sending hex values to socket.

Posted: Mon Feb 10, 2014 9:47 pm
by splash21
Hi, Jeffrey. If the docs are showing the data in hex, you can...

Code: Select all

write numToByte(0x42) to socket tSocket
Good for reference when you look back over your code as it matches the docs.

To send the 'lights on' string, you could use a little re-usable function ...

Code: Select all

function binData
   local tIndex, tData
   repeat with tIndex = 1 to paramCount()
      put numToByte(param(tIndex)) after tData
   end repeat
   return tData
end binData

# turn the lights on
write binData(0x42, 0x00, 0x55) to socket tSocket

Re: Sending hex values to socket.

Posted: Tue Feb 11, 2014 3:34 am
by JeffreyP
Thank you so much for the help Mark and Splash21! I regret I can't test it yet it because the hardware is still on the way, but it won't stop me from tinkering around with creating a program in the mean time.

I couldn't quite figure out whether it was num2byte, byte2num or binary convert, and exactly how it was implemented no matter how I studied the doc entries. I believe this will put me on the right path.