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". ;-)