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

For opening context, I'm writing a script that will eventually test the robustness of a CLI interface on my TI development board, but before I scream out for help on the general functionality I figured I'd try troubleshooting it myself first. (probably learn more besides ;)

The problem is I'm having a hard time understanding what the board is sending back to me because I believe it is being obfuscated by the interpolation of the board's output.

Here's the setup:

  1. I write the input with Win32-SerialPort-0.19
  2. Input: a string written to the serial port
  3. Output: the CLI program parses when it sees carriage return and returns either OK, Not Supported, or Command Nor Found if the input node exists and the command is valid, if the node exists but is not compatible with the given command, or if the node/command don't exist respectively.
  4. I read the contents of the serial port with Win32-SerialPort-0.19

Here's the relevant piece of the code:

use strict; use warnings; use Win32::SerialPort; # Open COM port my $COM = Win32::SerialPort->new('COM1') || die("Can't open port."); # Input my $sCommand = "SET INTERFACE NETWORK CLI:0 ENABLE"; $COM->write("$sCommand\r"); $COM->read_interval(100); my $iReadBuffer = length($sCommand); my ($count, $sEcho) = $COM->read($iReadBuffer); # Output print "----------------\n"; print "$sEcho\n"; print "----------------"; undef $COM;

Here's what is printed:

---------------- OK SET INTERFACE NETWORK CLI:0 ----------------

While I'm expecting either:

---------------- ---------------- OK or OK SET INTERFACE NETWORK CLI:0 ---------------- ----------------

The point is I'm unsure exactly what the CLI is sending me. I know that you can print a literal string by defining it with single quotes, but since I'm not the one defining this string (merely grabbing it and sliding it into the $sEcho variable) how do I print the literal, uninterpolated string?

All my searches to this point have returned hits on single qoute printing of literal strings; so thanks in advance for your help!

Replies are listed 'Best First'.
Re: Printing Literal Contents of a Variable
by roboticus (Chancellor) on Apr 27, 2011 at 18:09 UTC

    lyos:

    I'd suggest looking at the bytes coming in from the serial port. You can do it in many ways, one of which is:

    $sEcho =~ s/([\x00-\x1f\x7f-\xff])/sprintf("\\%02x",ord($1))/ge;

    A complete example:

    #!/usr/bin/perl use strict; use warnings; my $t = join("", map { chr($_) } 0 .. 255); $t =~ s/([\x00-\x1F\x7F-\xFF])/sprintf("\\%02x",ord($1))/ge; print $t;

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thank you roboticus; it worked like a charm :)
      I can now clearly see the carriage return and newline characters the CLI's spitting out.

      Just to follow up, with the line you suggested here's what's produced:

      ---------------- \0d\0aOK\0d\0aSET INTERFACE NETWORK CLI:0 ENABLE\0d> ----------------