Serial communication between matlab and arduino problem

1 visualizzazione (ultimi 30 giorni)
i was trying to connect the arduino uno to matlab using this setupSerial.m file
if true
%%~~~~~~~~~ SERIALPUSH SETUP ~~~~~~~~~ %
function [s,flag] = setupSerial(comPort)
% Initialize the serialpush port communication between Arduino and MATLAB
% The input value is the COMPORT should be changed as per requirement
% We ensure that the arduino is also communicatiing with MATLAB at this
% time. A predefined code on the arduino acknowledges this.
% if setup is complete then the value of setup is returned as 1 else 0
%instrfind
flag = 1;
s = serial(comPort);
set(s,'DataBits', 8 );
set(s,'StopBits', 1 );
set(s,'BaudRate', 9600);
set(s,'Parity', 'none');
fopen(s);
{
a='b';
while (a ~='a')
a=fread(s,1,'uchar');
end
if (a=='a')
disp('serial read');
end
fprintf(s,'%c','a');
mbox = msgbox('Serial Communication setup.'); uiwait(mbox);
}
disp(fscanf(s,'%s'));
end
so when i do this
>> comPort='com4'; [s,flag] = setupSerial(comPort);
I get this error
Error: File: setupSerial.m Line: 17 Column: 2 The expression to the left of the equals sign is not a valid target for an assignment.
the setup arduino code is
if true
void setup()
{
// Initialize the Serial Bus for printing data.
Serial.begin(9600);
Serial.println('a');
char a = 'b' ;
while(a != 'a')
{
a=Serial.read();
}
// Initialize the 'Wire' class for the I2C-bus.
Wire.begin();
// Clear the 'sleep' bit to start the sensor.
MPU9150_writeSensor(MPU9150_PWR_MGMT_1, 0);
MPU9150_setupCompass();
}
end
Can anyone tell me what is the problem plz?

Risposta accettata

Geoff Hayes
Geoff Hayes il 25 Gen 2015
Noorhan - try removing the braces, { }, around your code. There are unnecessary (and incorrect) for MATLAB programming which can lead to an error like the one that you are observing. For example, if I have the following simple function
function g
k = 1;
while k<42;
{
t=3;
k=k+1;
}
end
I see the same error, The expression to the left of the equals sign is not a valid target for an assignment., as you. Just remove the braces to get
function g
k = 1;
while k<42;
t=3;
k=k+1;
end
and you should be able to get further with your testing.

Più risposte (0)

Categorie

Scopri di più su Instrument Control Toolbox in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by