Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Sending raw hex in UDP packets

by belsamber (Initiate)
on May 07, 2013 at 05:59 UTC ( #1032441=perlquestion: print w/ replies, xml ) Need Help??
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?

Comment on Sending raw hex in UDP packets
Select or Download Code
Re: Sending raw hex in UDP packets
by BrowserUk (Pope) on May 07, 2013 at 06:51 UTC
    it ends up sending the ASCII value of the data instead of treating it as hex

    Change this:$variable = "\x21"."$i"."\x55" to $variable = sprintf "\x21%02x\x55", $i;


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Unfortunately that gives the same result...
      18:10:16.913890 IP 192-168-1-149.59755 > 192-168-1-8.50000: UDP, lengt +h 3 0x0000: 2cab 25ac cfdd 7cd1 c3e8 97a1 0800 4500 ,.%...|.......E. 0x0010: 001f dcc2 0000 4011 1a1e c0a8 0195 c0a8 ......@......... 0x0020: 0108 e96b c350 000b 592b 2102 55 ...k.P..Y+!.U 18:10:16.913891 IP 192-168-1-149.59755 > 192-168-1-8.50000: UDP, lengt +h 4 0x0000: 2cab 25ac cfdd 7cd1 c3e8 97a1 0800 4500 ,.%...|.......E. 0x0010: 0020 e1e9 0000 4011 14f6 c0a8 0195 c0a8 ......@......... 0x0020: 0108 e96b c350 000c 7ca6 2130 3155 ...k.P..|.!01U 18:10:16.913892 IP 192-168-1-149.59755 > 192-168-1-8.50000: UDP, lengt +h 4 0x0000: 2cab 25ac cfdd 7cd1 c3e8 97a1 0800 4500 ,.%...|.......E. 0x0010: 0020 ddcf 0000 4011 1910 c0a8 0195 c0a8 ......@......... 0x0020: 0108 e96b c350 000c 7ba6 2130 3255 ...k.P..{.!02U

        Sorry, my mistake: Use::$variable = "\x21" . chr( $i ) . "\x55";


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Sending raw hex in UDP packets
by graff (Chancellor) on May 08, 2013 at 00:01 UTC
    Even though the pack function can be a bit hard to grok from the documentation, it seems like a more "intuitive" approach in this sort of case (or, at least it would, if anything about "pack" were intuitive...)
    perl -e 'for $i (1..3) { print pack( "C*", 0x21, $i, 0x55 ) }' | xxd - +g 1 0000000: 21 01 55 21 02 55 21 03 55 !.U!.U!.U
    (UPDATE: In case it helps, remember that there's also the ample "tutorial" man page for pack: perlpacktut)
Re: Sending raw hex in UDP packets
by Browne (Initiate) on Jan 08, 2014 at 22:56 UTC
    I hope Belsander doesn't mind me adding another question to his post given it's nearly the same question. Oppose to creating hex and sending it via UDP, I'm reading it out of a file. I have the same issue as Belsander in that the hex will arrive if I send it like this.
    $socks->send('\xa1\xb1\xc1\xd1')
    The file I'm using has the same format, each line looks like this (except it's about 50 bytes per line)
    \xa1\xb1\xc1\xd1\xe1 \xa2\xb2\xc2\xd2\xe2
    The actual code:
    use strict; use warnings; use IO::Socket; my $socks = IO:Socket:INET->new( Proto => 'udp', PeerPort => 5010, PeerAddr => '101.101.101.101', ) or die "It didn't work. $! \n" my $file = 'values.hex'; open my $info, $file or die "It didn't open $file. $! \n"; while(my $line = <$info> { $socks->send($line) or die "Nope, can't send that. $! \n"; }
    Any solutions would be gratefully received thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1032441]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2014-09-18 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (101 votes), past polls