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

Hello,

I'm receiving a date (mm dd yy yy) via RS232 port sent by another device which looks like this:

02 22 20 22

I'd like to increment the year from 2022 to 2023 and send it back to the device.

The code in funny squares within Komodo should look like this:

+-----++-----++-----++-----+ | 0 0 || 0 0 || 0 0 || 0 0 | | 0 2 || 2 2 || 2 0 || 2 3 | +-----++-----++-----++-----+

The device accepts manually created (incremented) expiration data:

my $send="\x02\x22\x20\x23"; my $ob->($send);

These are the lines in my script:

my $received="02 22 20 22"; my($month,$day,$year1,$year2)=unpack("A2xA2xA2xA4",$received); $year2++; my $send=$month.$day.$year1.$year2; $ob->write($send);

The received code on the other side is:

30 32 32 32 32 30 32 33

Which are the ASCII codes of the sent code.

I've tried to convert the code with pack and the closest I get is this:

$month=pack("c",$month); $day=pack("c",$day); $year1=pack("c",$year1); $year2=pack("c",$year2);

The other side receives the hex intrepretation of the sent codes:

02 16 14 17

It's the hexadecimal interpretation of the sent code. At this point I'm out of ideas.

I'll appreciate your help.

Replies are listed 'Best First'.
Re: Code conversion
by Corion (Patriarch) on Feb 22, 2022 at 08:41 UTC

    Perl is sending the string 02222023, but you want to send the bytes \x02\x22\x20\x23. The easiest way is to use the chr hex function:

    my $send = join "", chr($month), chr($day), chr($year1), chr($year2); $ob->write($send);

    Update: Indeed, it must be the hex function, not chr, since the numbers are BCD, not plain hexadecimal. Of course, this approach will fail in 2030, when you need to roll over from 29 to 30 instead of 2A, but that is still 8 years to go.

Re: Code conversion
by Anonymous Monk on Feb 22, 2022 at 08:39 UTC

    Ok, found it by myself:

    $year2=chr(hex($year2));

    The funny squares contain what I need. :)

Re: Code conversion
by davido (Cardinal) on Feb 22, 2022 at 17:43 UTC

    How do you plan to increment the year for the date "02 29 20 24"? Or is this just a one-time thing that won't be running on a leap year?


    Dave