system("echo -e "\xFF\x08\x01" >> /dev/ttyACM0");
Why do you shell out just to write to a file or device? Perl has open, print, close, no need to mess with the shell. Using File::Slurp reduces that to a single line for each write, including error handling:
use File::Slurp qw( append_file );
if ($foo) {
append_file('/dev/ttyACM0',{ err_mode => 'croak', binmode => ':raw
+' },"\xFF\x08\x00");
} else {
append_file('/dev/ttyACM0',{ err_mode => 'croak', binmode => ':raw
+' },"\xFF\x08\x01");
}
Some other notes:
- I don't think you need to append to /dev/ttyACM0, it's a tty device that usually is not seekable, so a single ">" in the shell or write_file() in case of File::Slurp should be sufficient.
- I guess that the code you posted here is not the code you actually wrote. You managed to write something like system("echo -e '\xFF\x08\x00' >> /dev/ttyACM0"), and here things start to go wrong. The string you pass to system contains a NUL character. Perl has no problem with that, but the underlying C API has. C uses the NUL character as end-of-string marker, because C uses strings without length information. For C, you have passed the string "echo -e '".chr(255).chr(8) to system(). No problem for system(), but system passes exactly that to the shell. echo -e followed by a string "in" single quotes, but with the final single quote missing. Hence the syntax error from the shell. Had you used single quotes in perl, the backslashes would have been passed to the shell unmodified, without an embedded NUL character. echo -e, not perl, would have changed \x00 to a NUL character.
- append_file is ok for one or two "single-shot" commands, but repeatedly calling append_file is inefficient. In that case, use classic open/print/close, perhaps enabling autoflush on the file handle so that control commands don't sit in the file buffers for ages.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.