Sending hex values to socket.

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
JeffreyP
Posts: 2
Joined: Sat Feb 08, 2014 6:26 pm

Sending hex values to socket.

Post by JeffreyP » Sat Feb 08, 2014 6:32 pm

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.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Sending hex values to socket.

Post by Mark » Mon Feb 10, 2014 1:24 pm

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
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

splash21
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 369
Joined: Sun Dec 19, 2010 1:10 am
Contact:

Re: Sending hex values to socket.

Post by splash21 » Mon Feb 10, 2014 9:47 pm

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
LiveCode Development & Training : http://splash21.com

JeffreyP
Posts: 2
Joined: Sat Feb 08, 2014 6:26 pm

Re: Sending hex values to socket.

Post by JeffreyP » Tue Feb 11, 2014 3:34 am

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.

Post Reply