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?

In reply to Sending raw hex in UDP packets by belsamber

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.