Raspberry does not write in a created file

조회 수: 1 (최근 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

채택된 답변

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