MATLAB Answers

Raspberry does not write in a created file

1 view (last 30 days)
Hi,
I want creat a dataloger with raspberry pi. So I'm trying to create a file and write something on it. But my script only works when run on matlab. When run in a standalone mode the scrip only creat a empty file.
function creatFILE() %#codegen
% Create a Raspberry Pi object
r = raspi('169.254.0.2','pi','matlab');
data_atual = strip(system(r, 'date +"%d_%b_%y_%H_%M_%S"'));
doc_name = "Documents/dataLOG_" + data_atual;
system(r, convertStringsToChars("touch " + doc_name));
system(r, convertStringsToChars("sudo chown pi:pi " + doc_name));
system(r, convertStringsToChars("sudo chmod 777 " + doc_name));
for count = 1:100
system(r, 'sleep 0.5');
nanosegundos = strip(system(r, 'date +"%H_%M_%S_%N"'));
system(r, convertStringsToChars("echo " + nanosegundos + " >> " + doc_name));
system(r, 'sleep 0.5');
nanosegundos = strip(system(r, 'date +"H_%M_%S_%N"'));
system(r, convertStringsToChars("echo " + nanosegundos + " >> " + doc_name));
end
end
The result when it runs in standalone mode is a empty file:
result.png

  0 Comments

Sign in to comment.

Accepted Answer

Ryan Livingston
Ryan Livingston on 25 Jul 2019
Edited: Ryan Livingston on 25 Jul 2019
This is because of a bug in the Raspberry Pi system command in codegen. It doesn't trim the output to the proper size and instead returns a very large char array filled with zeros after the actual content. If I trim out the zeros then I get the expected behavior in the generated code:
data_atual = system(r, 'echo hello');
% Work around system issue
data_atual = deblank(data_atual(data_atual ~= 0));
nanosegundos = system(r, 'date +"%H_%M_%S_%N"');
% Work around system issue
nanosegundos = deblank(nanosegundos(nanosegundos ~= 0));
This problem has been reported to the development team so it can be fixed. Note that I've also changed strip to deblank because of another issue with strip. That's also been reported.
Thanks for taking the time to tell us about these issues.

  2 Comments

Michael Barbosa
Michael Barbosa on 25 Jul 2019
The code with the corrections generated the log file and wrote it correctly. Many thanks for the explanation and help.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 24 Jul 2019
Code generation for system() applied to a raspi object would generate host code for ssh into the pi. I would expect that when you generate code for deployment that it would ignore the operation -- though I cannot rule out the possibility that the Pi would attempt to shell into itself.
You should be using coder.ceval() to make C library calls and system calls -- or possibly to run a shell script that you pass the document name into.

  2 Comments

Ryan Livingston
Ryan Livingston on 25 Jul 2019
This is incorrect. The generated executable runs directly on the Raspberry Pi and runs the system comand there.
Michael Barbosa
Michael Barbosa on 25 Jul 2019
Walter, thank you for your willingness to help me.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!