http://www.perlmonks.org?node_id=1032441

belsamber has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks, I'm trying to send raw hex data in a UDP packet, for the purposes of controlling some multicolor LED bulbs.

For example, you can turn the bulbs on by sending a UDP packet with a payload of 0x22 0x00 0x55. You can adjust hue of the bulbs with a payload of 0x21 0x00 0x55 where the middle hex chunk varies from 0x00 to 0xFF. Example code:

#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $bulb_sock = IO::Socket::INET->new( Proto => 'udp', PeerPort => 50000, PeerAddr => '192.168.1.8', ) or die "Could not create socket: $!\n"; $bulb_sock->send("\x22\x02\x55") or die "Send Error: $!\n"; my $variable; for (my $i=1 ; $i<3 ; $i++) { $variable = "\x21"."$i"."\x55"; $bulb_sock->send("$variable") or die "Send Error: $!\n"; }
The first "send" works correctly and sends raw hex digits. A packet decode looks like this (note the 210255 at the end of the packet):
15:51:05.035323 IP localhost.53816 > 192.168.1.8.50000: UDP, length 3 0x0000: 0200 0000 4500 001f 3e3e 0000 4011 bd5c ....E...>>..@..\ 0x0010: ac1b 1168 c0a8 0108 d238 c350 000b 7518 ...h.....8.P..u. 0x0020: 2102 55 !.U
but any time I try to manipulate the values as in the loop, it ends up sending the ASCII value of the data instead of treating it as hex. the other packets are as follows:
15:51:05.035358 IP localhost.53816 > 192.168.1.8.50000: UDP, length 3 0x0000: 0200 0000 4500 001f f877 0000 4011 0323 ....E....w..@..# 0x0010: ac1b 1168 c0a8 0108 d238 c350 000b 74e9 ...h.....8.P..t. 0x0020: 2131 55 !1U 15:51:05.035369 IP localhost.53816 > 192.168.1.8.50000: UDP, length 3 0x0000: 0200 0000 4500 001f 3f34 0000 4011 bc66 ....E...?4..@..f 0x0010: ac1b 1168 c0a8 0108 d238 c350 000b 74e8 ...h.....8.P..t. 0x0020: 2132 55
Note that the 21 works, and the 55 works, but I can't cast the middle number into hex no matter what I try. Any thoughts?