I have an older serial based LCD display (2x16char) and a mini-ssc (serial servo controller by Scott Edward) connected together. To move the servos I need to send the following 5 bytes:

\xFE\x90 #This moves the LCD 'cursor' off screen \xFF\x00\x05 #This tells the SSC to move the first servo to #position 5 (range of 0-254 acceptable, but I #will limit that to 253 to avoid the LCD #display).

I am using the Device::SerialPort module from CPAN and am confused by what appears to be magic type casting.

Here's my test code. The first section works, the loop does not. Why?

#!/usr/bin/perl use Device::SerialPort 0.12; $ob = Device::SerialPort->new ("/dev/ttyS1") || die "S1 dead"; $ob->datatype("raw") || die "raw"; $ob->baudrate(2400) || die "baudrate"; $ob->parity("none") || die "parity"; $ob->databits(8) || die "databits"; $ob->handshake("none") || die "handshake"; $ob->write_settings || die "write settings"; #write to both lines of the LCD display #clear screen $ob->write("\xFE\x01"); $ob->write("Hello"); $ob->write_drain; #second line $ob->write("\xFE\xC0"); $ob->write("World"); $ob->write_drain; # These lines move the servo to 5 different positions sleep(1); $ob->write("\xFE\x90\xFF\x00\x01"); $ob->write_drain; sleep(1); $ob->write("\xFE\x90\xFF\x00\x11"); $ob->write_drain; sleep(1); $ob->write("\xFE\x90\xFF\x00\x21"); $ob->write_drain; sleep(1); $ob->write("\xFE\x90\xFF\x00\x31"); $ob->write_drain; sleep(1); $ob->write("\xFE\x90\xFF\x00\x41"); $ob->write_drain; #This loop should move the servo #nearly full range in 25 step #it doesn't. $x = 25; while ($x>0) { sleep(1); $ob->write("\xFE\x90\xFF\x00"); $ob->write(int($x*10)); $ob->write_drain; $x--; } #end

I have tried a couple of variations for sending the value.

$fs = sprintf "\\xFE\\x90\\xFF\\x00\\x%X", int($x*10); $ob->write($fs); # does not appear to work

In reply to Casting magic - how to write numbers to the serial port by ehud42

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.