Matlab to Arduino Diecimila Serial Communication

I am doing a project which involves serial communication via USB to the Diecimila from a Matlab program. To test this communication, I have written 2 simple programs. All I want is to have printed to the Matlab screen the value that I sent (a 5 here).

On the Arduino:

byte input = 0; void setup() { Serial.begin(9600); } void loop() { if (Serial.available()) { input = Serial.read(); Serial.print(input); } }

On Matlab:

function varargout = test(varargin) s = serial('COM3'); set(s,'BaudRate',9600); set(s,'DataBits',8); set(s,'StopBits',1); set(s,'Parity','none');

fopen(s);

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

fclose(s); delete(s); clear s; end

When I run this from Matlab, before it prints the value of the fread(s) command, Matlab gives me:

Warning: The specified amount of data was not returned within the Timeout period.

Followed by:

ans = Empty matrix: 1-by-0

I'm not sure about the serial protocol used by the Arduino. I'm assuming that this is asynchronous, 8 data bits, no parity check, 1 stop bit.

What am I missing?

Ok, so I have found out that Matlab doesn't seem to have any general USB functions that will work with the Diecimila, so I have attached a rs232 line to ports 0 and 1 and am powering the device externally. Now information is being sent and received between the programs.

The only problem is that I am trying to send a "5" and then print that "5" to the Matlab screen, but it is printing some other 8 bit number. I have tried using the "BYTE" option on the Serial.print command and using 'uint8' on both the fread() and fwrite() command in Matlab. It doesn't seem to work. Any thoughts?

Thanks! buggaby

Hello i'm trying to do the same thing you are by using Matlab to communicate with an Arduino board if you could post on my topic so that everyone that is doing something similar will be on the same disscussion board. I am e-mailing others so that they can do the same.

its under tricky_bluefox

Arduino used with MATLAB