简体   繁体   中英

Matlab code runs in the command window but not in the script file

I have a very simple code in MATLAB for a laser device which I need to use. Here's the code:

% creating a serial port object
s = serial('COM3');

% opening the port
fopen(s);

% enabling the port
fprintf(s, 'e');

% sending the power to the laser
fprintf(s, 'a738.8889');

% disabling the port
fprintf(s, 'd');
fprintf(s, 'z');

fclose(s);
delete(s);

The code works perfectly fine when I write them one by one from the command window, but it's not working when I put the whole code in either a script file or a function file.

I don't get any errors or any other messages from MATLAB when I run the script or the function. Any ideas why?

Thank you.

So I tried delaying the commands and the script works now. Apparently when the commands are being executed in the script, it is too fast for the device to read them all and that is why it wasn't working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM