Pages: [1]   Go Down
Author Topic: Problem with MATLAB to Arduino MEGA  (Read 678 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I want to interface MATLAB with Arduino MEGA.
For the serial reading operation from Arduino MEGA to MATLAB i havn't any problem.

But I don't succeed for the writing operation from MATLAB to Arduino MEGA, this is my code:


Arduino Code:

byte incomingByte = 0;      

void setup()
{
      Serial.begin(9600);
}

void loop() {
      
      if (Serial.available() > 0)
       {
                incomingByte = Serial.read();
            Serial.print("I received: ");
            Serial.println(incomingByte, DEC);
            
       }
}


MATLAB code:

function serialMTOA()

s = serial('COM5', 'BaudRate', 9600);
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'Parity','none');

fopen(s);

fwrite(s, 5,'uint8','async');

stopasync(s);
fclose(s);

end



Are there some errors?
When I send the Matlab function I see that Arduino receives (TX led blinks) but when I try to see the Arduino Serial Monitor there's nothing.

Others informations: I use the usb alimentation, and only the usb as serial (no others comunication ports).
Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

hm

is it possible to write to the arduino with one program and to read from it with another program?

what happens when u make the arduino turning on pin 13 if it receives an 'A' and off if it receives an 'E'...?

-arne
« Last Edit: October 18, 2009, 08:30:55 pm by RIDDICK » Logged

-Arne

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I get round this by getting matlab to check the connection status first:

Comtest = arduino.TransferStatus; % check port status before trying to write
    if Comtest(1) == 'i';
            fwrite(arduino, ServoValue, 'uint8','async')
    else
        Comtest;
        pause(0.08)
    end

Hope this helps
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

'i' here as in idle
Logged

Pages: [1]   Go Up
Jump to: