MATLAB Answers

Raspberry does not write in a created file

2 ビュー (過去 30 日間)
Michael Barbosa
Michael Barbosa 2019 年 7 月 24 日
コメント済み: Ryan Livingston 2019 年 7 月 25 日
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 件のコメント

サインインしてコメントする。

採用された回答

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.
Ryan Livingston
Ryan Livingston 2019 年 7 月 25 日
You're very welcome

サインインしてコメントする。

その他の回答 (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.

サインインしてコメントする。

カテゴリ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by