Serial Communication Code Error

h=instrfindall;
for i= 1:length(h)
fopen('COM'+string(i))
end
h=instrfindall;
str=string(h.name(find(cell2mat(h.BytesAvailable))));
fclose('all');
fopen(str);
I developed this code on friday for finding out on which Port my Arduino is plugged in and it actually worked. Today I tried to implement the code into my GUI and an error is appearing.
Error: Struct contents reference from a non-struct array object.
What do I have to do, so that the error won't appear anymore?
Thank you for your help

답변 (2개)

Nate
Nate 2017년 10월 26일

0 개 추천

Hi Tobias,
Could you please provide more information?
1) Does the code still work correctly when it is not in a GUI? 2) Could you please provide the full error? I am interested in knowing which line causes the error.
Thanks, Nate

댓글 수: 1

Tobias Wzl
Tobias Wzl 2017년 10월 27일
편집: Tobias Wzl 2017년 10월 27일
No unfortunately not, but in first place it worked in both ways I don't know what's wrong now.
If run the code separate outside of a GUI the error appears in line 6:
*Struct contents reference from a non-struct array object.
Error in Untitled (line 6) str=string(h.name(find(cell2mat(h.BytesAvailable))));*

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2017년 10월 27일

0 개 추천

You would get that error if instrfindall returns empty.

댓글 수: 8

Tobias Wzl
Tobias Wzl 2017년 10월 27일
but how could it be that instrfindall returns empty if some device like my Arduino is plugged in? It's confusing me because this worked in first place when I created this code...
Walter Roberson
Walter Roberson 2017년 10월 27일
None the less, put in a test for h being empty. If it is an "impossible" situation then your error about non-struct will not disappear. If somehow it can happen then you will avoid the error.
Tobias Wzl
Tobias Wzl 2017년 10월 27일
i checked if h is empty or not and it somehow it is.
but doesn't the command 'instrfindall' provide to search for all of my ports whether they are closed or open? Or did I get something wrong here?
Walter Roberson
Walter Roberson 2017년 10월 27일
It has been known to happen in the past that instrfind has lost track of a port, and that it was necessary to restart MATLAB to find it again. In the time that was happening most often there was no way to force a bus rescan. Part of the problem back then was that when this was happening most often, USB serial ports could only be detected if the device was plugged in at the time MATLAB was started. That should no longer be the case in current versions, but it used to happen. (You do not happen to mention your MATLAB version.)
Tobias Wzl
Tobias Wzl 2017년 10월 27일
Sorry I was so desperate about the code that I forgot to mention the version I'm using... I'm currently using 2017a
I already tried to restart Matlab again, because this would've been the easiest way to make it work again but unfortunately it isn't^^
And my Arduino is plugged in all the time.
What is returned if you execute
com.mathworks.toolbox.instrument.Instrument.jinstrfindall
Also, try
instrreset
Tobias Wzl
Tobias Wzl 2017년 11월 3일
if I execute the first command an empty answer appears --> ans=[]
if I execute the second command Matlab tells me that the function is undefined
And sorry for the late answer I was ill for the last whole week
Walter Roberson
Walter Roberson 2017년 11월 3일
Hmmm, instrreset has been around in Instrument Control Toolbox since before R2006a; https://www.mathworks.com/help/instrument/instrreset.html
Anyhow, I do not know why your devices cannot be seen, unless perhaps the system considers them to already be in use for something else. Perhaps this link will help: http://www.intech.co.nz/downloads/help/mscanv5/faq_84.htm

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

질문:

2017년 10월 24일

댓글:

2017년 11월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by