Reading and writing data from serial port in Matlab

Hi everyone,

I have a trouble in programming to read and write data of the VCNL4010 sensor through a serial port in Matlab. I use a VCNL4010 sensor and Arduino Uno to measure two values including ambient light and height. These values will be transported via a serial port to monitor in real time, as well as save to analyzing later. I have tried many codes but it seemed to not work well. Could you help me to solve them? Thank you so much.

global arduino; global i;

try fopen(arduino); % Open Serial Port Object disp('Serial port is opened'); catch err fclose(instrfind); error('Make sure you select the correct COM Port where the Arduino is connected.'); end

%% Read buffer data delaySec = 0.01; for i = 1: 10 pause(delaySec); arduino.RecordDetail = 'verbose'; arduino.RecordName = 'MySerialFile.txt'; record(arduino,'on') out = fscanf(arduino) end record(arduino,'off')

fclose(arduino); disp('Serial port is closed');

(deleted)

spycatcher2k: So you have an issue with Matlab? but you ask in an Arduino forum?

Because it is hybrid problems so I want to ask both.

Does your Arduino code work when using serial monitor (or other terminal emulators)?

If so, it's not a hybrid problem. If not, you will need to fix that first.

I have no idea about MatLab. It strikes me as a little odd that you have record(arduino, 'on') in your loop but only have one record(arduino,'off'); might be absolutely normal though.

Be aware that a number of Arduinos reset when the serial port is opened; as a result you will have a 3-plus second delay. You can work around it by waiting for an initial reply from the Arduino (e.g. let Arduino send "hello from Arduino" in setup) and check for that reply before continuing reading the actual data.