Go Down

Topic: Need help with Arduino-Xbee-Matlab interfacing (Read 593 times) previous topic - next topic

jtl033

When I run my code on the serial monitor, I can a clear line of data similar to the line below:

X:   -6   Y:   16   Z:   4092   t(ms):   74

however when I go to retrieve a line of data in Matlab, I get something similar to this:


Line =

î~ '�  $ X:   -2   Y:   14   Z:   4084   t(ms):   91781

Î~ &�  ' X:   0   Y:   28   Z:   4096   t(ms):   91803

÷~ '�  $ X:   -4   Y:   14   Z:   4076   t(ms):   91826

Ë~ '�  ' X:   -8   Y:   16   Z:   4062   t(ms):   91848

Ã~ (�  $ X:   -12   Y:   16   Z:   4086   t(ms):   91871

™~ '�  ' X:   -16   Y:   2   Z:   4078   t(ms):   91893

Â~ &�  $ X:   -8   Y:   8   Z:   4052   t(ms):   91916


where I get multiple lines of data and these characters at the beginning of each line. Can you please give me some advice on how to get rid of those characters at the beginning of each line and how to grab only one line of data at a time?



And here is the Matlab code that I am using:

%%

close all
clear all
clc

%% Setup

% Clears open COM ports that are already in use by MATLAB
instrreset

% Sets communication channel with the sensor to COM4
s = serial('COM4');

% Sets the baud rate for the connection
% This must be the same baud rate of the XBee transmission
set(s, 'BaudRate', 57600);

% Open the data file for recording the sensor data
% Was previously a .dat file. Can be changed easily.
dat = fopen('sens_data_buoy.txt', 'wt');

fprintf(dat,'x data \t y data \t \t z data \t time(ms) \t time(s) \t time(s) \n');

% Opens the communication channel with the sensor
fopen(s);
ii = 1

%%

Number_of_Samples = 50;
fprintf('Setup Complete. \nStarting data collection.\n');

while ii <= Number_of_Samples
    Line = fgets(s)
    ii = ii + 1;
end

fclose(s);
fclose(dat);
delete(s);
clear s;

Thank you.

PaulS

Quote
When I run my code on the serial monitor
Since you can't run your code "on the serial monitor", this statement makes no sense.

You CAN run your code on the Arduino that is connected to the other end of the serial port that the Serial Monitor is connected to.

Where do you write data to the file? The ONLY statement I see that writes to the file is:
Code: [Select]
fprintf(dat,'x data \t y data \t \t z data \t time(ms) \t time(s) \t time(s) \n');
That statement is executed ONCE each time the file is opened.

The data read from the serial port (that the Arduino wrote) is discarded.
The art of getting good answers lies in asking good questions.

Go Up