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

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

Hello,
I'm working with a Serial device which has some built in special funktions.
Those functions are initiated via special characters like "ESC J".

Some of those functions take options with them which can be HEX or DEC characters,
some must be dec characters,bitmask sums for example.

Below are some examples,in BASIC and perl.
Beware that the CHR function does not work as defined with LPRINT and serial connection.
It DOES NOT convert the DEC to ASCII table character, CHR(120) is submitted as DEC 120.
99 percent work just fine just 2 things give me a headache.

use warnings; use strict; use Fcntl; use IO::Handle; my $PORT = 'COM8'; sysopen(USB, $PORT, O_WRONLY | O_APPEND) or die "ERROR: $!\n"; USB->autoflush(1); print USB "hello world\n"; print USB "\x1BJ"; print USB "\x96"; print USB "\x1Bi\n"; close(USB);
LPRINT "hello world"; CHR$(&HA); LPRINT CHR$(&H1B);"J"; LPRINT CHR$(150); LPRINT CHR$(&H1B);"i";

Both program run fine,
the problem happens if i would do
print USB "150";
the device only takes the 1 and would put 50 in the buffer for printing.
Is there a way to encapsulate DEC values in perl like i can do with HEX values ?.

\x1B is treated as 1 character 150 is treated as 3 DEC characters BASIC CHR$(150) is treated as 1 DEC character

In the above example i can counter the problem via submitting 150 as HEX, the real problem lies in functions where they demand DEC or STRING as options, like "L1".
Is there a way to encapsulate 150 DEC or L1 string,as it works with BASIC ?

kind regards
alex

ps: i need this raw low level access,and the device is troubled by more complex communications ;)
psps the device tells me exactly what it received, and how it treated it ;)

Replies are listed 'Best First'.
Re: serial raw write problem with multidigit decimal and strings
by choroba (Cardinal) on Mar 10, 2013 at 18:42 UTC
    Have you tried
    print USB chr 150;
    See chr.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      ofc i have ;)
      sadly perls chr works as defined and not like the BASIC one.
      it firsts evaluates the chr 150 and then submits the result which is a û

      kind regards
      alex

        Strange. "\x96" and chr 150 return the same for me:
        diff <(perl -e 'print "\x96"' | xxd) <(perl -e 'print chr 150' | xxd)

        No output.

        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: serial raw write problem with multidigit decimal and strings (bits bytes pack)
by Anonymous Monk on Mar 10, 2013 at 19:50 UTC

    I know nothing about basic, but if it sounds like you're talking about bytes and bits, so you should probably see pack, perlpacktut

    I imagine you might need  print USB pack 'c', 150

      pack looks like a good start, i'll experiment with it a bit :)
      pack 'i' 150 works as expected submits 150,
      pack 'c' 150 produces warning "Character in 'c' format wrapped in pack" and submits garbage, 150 is not a char i would guess :)
      i'll test some more complex things and will come back with the results.

      kind regards
      alex



      UPDATE
      pack seems to work in some cases, but also produces funny results in other cases.
      As it seems,it depends on the function it is used on, will further experiment with it.

Re: serial raw write problem with multidigit decimal and strings
by ultibuzz (Monk) on Mar 11, 2013 at 21:56 UTC

    i wanted to thank all people for their ideas
    I got all functions i need to work, and after all data is recovered i can store the machines form '88 in a dark corner.

    pack solved some issues, and suprisingly some functions expected their data in a defined time pules manner like:
    data 5msbreak option 5msbreak ...
    the timepulse could vary from 5ms to 10ms.
    The last function i got to work in analyzing what the printer recived from Basic and in what it got converted,
    so i transmited the exact converted data from perl to the device like \x1D\x38\x4C\x06\x00\x00\x00\x30\x45\x4C\x31\x01\x01 one of the smaller ones ;)

    kind regards
    alex