Raspberry does not write in a created file

1 次查看(过去 30 天)
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

采纳的回答

Ryan Livingston
Ryan Livingston 2019-7-25
编辑:Ryan Livingston 2019-7-25
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 个评论
Michael Barbosa
Michael Barbosa 2019-7-25
The code with the corrections generated the log file and wrote it correctly. Many thanks for the explanation and help.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2019-7-24
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 个评论
Ryan Livingston
Ryan Livingston 2019-7-25
This is incorrect. The generated executable runs directly on the Raspberry Pi and runs the system comand there.
Michael Barbosa
Michael Barbosa 2019-7-25
Walter, thank you for your willingness to help me.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Raspberry Pi Hardware 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by