Matlab to

I'm still trying to get this serial communication between MATLAB and the Diecimila board working properly. I have some communication happening, but it's not predictable. All I'm trying to do at the moment is receive a 5 from MATLAB and send it back. My Arduino code just does this:

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

byte input;
void loop()
{
if (Serial.available()>=1)
{
input = Serial.read();
Serial.print(input,BYTE);
}
}

The MATLAB code is simply:

function communicate(mydata)

s = serial('COM1');
fopen(s);

fwrite(s, mydata,'uint8');
confirmLSB = fread(s, 1, 'uint8')

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

Here is some output from the MATLAB screen.

communicate(4)

confirmLSB =

112

communicate(5)

confirmLSB =

65

communicate(6)

confirmLSB =

48

communicate(12)

confirmLSB =

56

As you can see, there is something being sent to the Arduino. I had it coded so that the onboard LED turned on if Serial.available() became not 0, and it worked. There is something being sent both ways, it's just that I can't figure out how to send a specific value. Sending a 4 always receives a 112 while a 5 returns a 65 every time. I can't figure out if I'm sending it wrong from the Arduino - i.e. Serial.print(input,DEC/HEX/BYTE...) - or from MATLAB - i.e. encoding it as uint8. Can someone help me? I have the RS232 connected to ground, tx to pin 0, and rx to pin 1 on the Arduino board and the other end into COM1 on the computer.

Thank you,
buggaby

have the RS232 connected to ground, tx to pin 0, and rx to pin 1 on the Arduino board

Do you have an RS232 to TTL voltage level converter?

I have looked at the codes and can't see anything that would point to this other than you trying to feed RS232 voltages directly into the Arduino.

Wow... Grumpy_Mike, thanks for that. I didn't have a converter in the circuit. The stupid thing is that I knew that I needed one, but apparently, that's not enough for me. Someone told me this last week, I saw the circuit for the MAX3323 chip on arduino.cc, and I even took a class last semester on serial communication protocols and it didn't stay with me when I was actually working on the circuit. I guess the stress is just getting to me. :stuck_out_tongue: I'll try that right away and let you know.

Thanks for pointing that out to me :slight_smile:

buggaby

Matlab and Arduino? Sounds interesting!

What are you going to to with this combination?
Did you try to visualize incoming data from the arduino in Matlab?

We are simply using MATLAB to control a linear actuator via stepper motor and Arduino. That part isn't too exciting, but its purpose is pretty cool. It's for use as an extension to a 3-d positioning system which is meant to characterize an electro-magnetic beam, which is itself used to irradiate cancer from cancer patients. This is part of a final year Engineering design project.

On a technical help note :slight_smile: I spoke with someone at my university who works with serial and micros. He said that while the voltage requirements are different between USB and RS232, he has never actually experienced a problem getting the two to communicate without any voltage regulators or whatever. He suggested it was a problem with baud, parity, or stop bits. Does anyone know what the Arduino uses for serial communication (i.e. # of data bits, # of stop bits, if it uses parity...)? I couldn't find anything on the website.

Thanks,
buggaby

Just to put this on the forum, I have fixed the problem. The problem was two-fold: I was not accounting for the voltage differences between USB and RS232. I got the MAX3323 chip and hooked it up according to the site (http://www.arduino.cc/en/Tutorial/ArduinoSoftwareRS232) but it still didn't work.

I had to change the values for the bit9600delay values as described by the above site. It worked with slightly higher values. I'm not sure exactly why but anyway...

buggaby

Glad to here you got it working. As to your friend:-

He said that while the voltage requirements are different between USB and RS232, he has never actually experienced a problem getting the two to communicate without any voltage regulators or whatever.

He is either a lire, an idiot or he never experienced a problem because he never tried it.

... liar... :slight_smile:

Actually, I just think it was a miscommunication. He is a tech guy at the university and in days past, when there was no WIFI, all their communication was done over long serial lines. So, he has quite a bit of experience with that. I haven't sat down and gone over exactly what we did to make it work because... well it works so I don't really care :slight_smile: I'm too busy for that.

In any case, I'm sure glad I found the resources on this website. Thanks to all the great folks who have helped.

buggaby

Hi guys,
I'm trying to program a couple of server motors using matlab and working with arduino. I don't know much about electronics etc. or programming, but need to do it for a uni project that I'm working on. I'm working through a usb, so perhaps that's the problem? Do any of you think that I should try the RS232-TTL converter?
Any tips at all would be greatly appreciated.
Thanks!

I'm working through a usb, so perhaps that's the problem? Do any of you think that I should try the RS232-TTL converter?

Not a lot to go on here. The only advantage of using RS232 rather than the USB is that the drivers are normally loaded onto a PC for the built in COM ports. Other than that there are no differences. So if you have drivers installed in the PC and are communicating with the arduino then you should have no difficulty using that over RS232.

Hey,
Thanks for that. Also, do you know if there's any issues with different versions of Matlab and how well they work with Arduino? We're currently having some problems with Matlab working with our Arduino board.
Thanks again.