Reconnect to serialport within same app

1 view (last 30 days)
TheoBoveri
TheoBoveri on 26 Jan 2021
Edited: TheoBoveri on 28 Jan 2021
Hi there,
I'm using appdesigner to communicate with an arduino which is connected to a small robot arm. The robot is doing a simple pick&place for me.
For communication with the robot I send simple strings like this:
'<90,165,0,0,0,55,1,50>'
The first six numbers refer to the degrees of the robot joints. The last two are addition info, which is unimportant for now.
As there can be several hours between the robot actions, I would like Matlab to move the robot and then disconnect from the serialport, and reconnect when it's time.
This is currently some of the code that I'm using:
app.s = serialport('COM5',9600);
Position2 = '<80,165,0,0,0,55,1,50>';
write(app.s,Position2,'string'); % Do something similar to this
app.s.delete; % Delete serialport
%% === Wait until needed (might be 2min; might be 2h) ===
app.s = serialport('COM5',9600);
Position3 = '<120,165,0,0,0,55,1,50>';
write(app.s,Position2,'string'); % Do something similar to this
When this code is executed, no error pops up. The serialport is succesfully opend, but the Robot doesn't move.
Normally it takes the robot a couple seconds (3-5s) to 'wake-up'. If I create the serialport object manually via the console and I wait a couple seconds and then execute the write command via console, it works fine. It just won't work when using the script. Also adding an additional pause after reconnection won't work.
Is there anyone out there who has some usefull insights into the matter?
Thank's a lot!
  1 Comment
TheoBoveri
TheoBoveri on 28 Jan 2021
Found a workaround myself.
The Arduino gives feedback when it has sucessfully moved the robot. When Matlab reconnects to the Arduino it goes into a while loop. When Matlab has successfully read the Arduino feedback, it will contiue.
Still, Matlab runs into two serial errors because it cannot read the Arduino feedback, before it successfully connects.
So inspite of my workaround, I'd still be very glad if someone could share some insights with me, on how to solve this issue more efficiently/elegantly and especially faster!
Thank you guys!

Sign in to comment.

Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!