in reply to Re: Send a UDP Packet with bunch of numbers in PERL?
in thread Send a UDP Packet with bunch of numbers in PERL?

This is just a piece of code to explain at a high level. So,

1) I can't just send the data in string and cannot assign it the $DATA_to_be_SENT = $_ ; as $_ returns a string which in my case would be "-2.3,-4.5,901,223,34.098"

2) My Receiving end is a hardware which can only work on Floating numbers and all these five numbers should be sent within ONE Packet in the same order.

3) I noticed that I can do something like this for hexadecimal numbers

$socket->send("\x-2.3,\x-4.5,\x901,\x223,\x34.098");
If I try to use "\d-2.3,\d-4.5,\d901,\d223,\d34.098" and observe the network in wireshark , I am seeing the letter d along with the values.

So I believe you got a better insight of what i am trying to ask.

Replies are listed 'Best First'.
Re^3: Send a UDP Packet with bunch of numbers in PERL?
by pryrt (Abbot) on Aug 18, 2016 at 19:41 UTC

    Does your hardware accept a number as a string, or does it desire the floating-point encoding of the number? (Yes, this is re-asking what NetWallah already asked, but maybe in a slightly more prominent way, with some extra clarifications following below.)

    • if it wants a string, does it want it the numbers to each be a certain number of characters, or is there a specific delimiter between them (your examples imply a comma)
    • if it wants floating-point encoding of the number, does it want single (32bit), double (64bit), long double (128bit) or something else? what endian-ness? Once you know the exact format of the number it desires, you can split your string on the commas (or use a prebuilt CSV module) to get a list of the floating-point values, then use pack to encode the floating points into the underlying representation that your hardware understands: your format will be one of f (single), d (double), or D (long double); with perl ≥ v5.10, you can add a modifier, > for big-endian or < for little-endian.

    Once you have the answers to those, try coding it up. If you still need help afterwards, show us the coding progress you've made, and what's still not behaving as you expect.

      * My Hardware only accepts floating point encoding of the number.

      * It has to be either single (or) double . It has Big-Endian format .

      * I did split the string to an array of my floating point values.

      Do you mean to say that i need to use pack module so that it is transmitted viably as per floating point values . ???

      I will try to code now and post the answers.. Thanks
      Hello , I have a quick question in reagard to the numbers while UDP packet is created. I have used pack function to embed a value according to a float (f) expression. Do I need to do that to all numbers individually and Even if by doing so , How can i Send the packet with five different numbers at once. ?>
        Yes, you need to pack each value individually. Don't worry, as long as you don't run $socket->send() in the inner loop, you're allowed to have another loop. Though, if you want, the documentation could give you some ideas on how to do more than one float in the same call to pack().

        Some more hints:

        pack outputs a series of octets (bytes) that you can treat like a string: for fun, at the command line, perl -le "print pack 'f', 3.14", and you'll see the funny octets interpreted as strings. You might try other simple one-liners like that, to see what happens when you do different pack templates. The worst that will happen is that your terminal or cmd window will start using funny characters when you type -- at which point, just close it and open a new one. Printing it out is a good way to see what pack is doing.

        Since the group of octets is equivalent to a string, you can use any operator or function that manipulates strings, including ones that might concatenate strings (search the docs I linked for 'concatenate', 'concatenation', or synonyms, and I'm sure you'll find an appropriate operator or function), and the results of such activity can be placed in a scalar variable ($DATA_to_be_SENT)

        As Mr. Muskrat pointed out, you still haven't said how the encoded floating-points are supposed to be juxtaposed for your hardware; if they are just one right after the other, with no intervening octets, then you can just concatenate the pack() results one after the other; otherwise, you'll have to insert other bytes between, as defined by your hardware requirements. (An inner loop or pack's template should both allow you to easily place zero or more separators between each encoded floating point.)

        Once you've got everything concatenated in the way your hardware wants, then you send the string-like scalar to your hardware via the $socket->send($DATA_to_be_SENT);.

        I hope this will move you in the right direction. Good luck.