0

So I want to send some data from Matlab to Arduino. To verify that I am getting the correct data I am sending the data back to Matlab (since I get errors if I open the serial monitor, if there's an easier way to print debug messages let me know).

The problem is that I always get 48 back.

My matlab code:

arduino = serial('COM3','BaudRate',115200);
fopen(arduino);
fprintf(arduino,'%f',1);
fclose(arduino);

fopen(arduino);
disp(fscanf(arduino, '%f'))
fclose(arduino);

and my arduino code:

In the setup:

Serial.begin(115200);

In the loop:

delay(100);
myFunction();

In myFunction:

while(!Serial.available()>0)){}
int data = Serial.read();
Serial.println(data,DEC);

It seems like the 48 is an ascii code for something but it doesn't ever change when I change the 1 is the 3rd line of the matlab code to anything else.

1

Why am I always getting 48 with serial communication?

Your Arduino sketch (snippet) is reading a byte and writing the value, not the character. The value 48 in ASCII is the character '0'.

The below loop() function will echo all the characters received.

void loop()
{
  while (Serial.available()) {
     char c = Serial.read();
     Serial.println(c);
  }
}

This could be a good starting point when debugging what is actually written from MATLAB.

| improve this answer | |
0

I don't know much about maylab, but you are sending a float to the Arduino where you read bytes and send characters back to matlab and read it as a float.

Try sending a single character from matlab and reading a single character back, %c.

| improve this answer | |
  • If instead of sending data back from arduino I send back a set number it works (although I get 0 first and then the number 3 times). Trying the character as you suggsted, I get a 0 and then nothing. – Aequitas Aug 9 '16 at 2:55
  • You need to understand "data types". The Matlab code is sending 1 as a floating point number. The Arduino Serial.read() function reads a character and you then save this as an integer. A float is going to be at least 4 bytes and the number is encoded in a special way so it can store the whole number and the decimal. You cannot convert a float into a char or int without special handling. A char is 1 byte and an Arduino int is 2 bytes, the chances are the upper byte of the int will be blank when you write the char into it, but its not a great idea. – Code Gorilla Aug 9 '16 at 7:57
  • So if you want to send a float, you need to receive a float, which means calling Serial.Read() 4 times and then assembling that as a float using bit shifting. BUT you are sending a string a series of characters terminated by a NULL character. You are doing this because you are using fprintf(), if you want to send a float as binary you have to use fwrite(). – Code Gorilla Aug 9 '16 at 7:59
  • Thanks for your explanation @Matt I tried calling Serial.Read a second time but then it would just freeze at that line (presumably). I tried replacing the fprintf with fwrite but then I couldn't get any response back at all. – Aequitas Aug 16 '16 at 1:52
  • Also as some additional information: I put a loop in the matlab code to display and fscanf from the arduino object, and I noticed that it will end up printing the data sent from the arduino three times. So if I send a 5 from arduino to matlab it will print out 5 three times, if in the arduino I send back the data I receive from matlab, then 48 will be printed in matlab three times. – Aequitas Aug 16 '16 at 2:02
0

May I recommend that you take a look at the data stream that MatLab produces? That will show you what it is producing, and therefore what code you should write in the Arduino to process it.

If you had a second COM port on the PC, and a cable to connect your PC's MatLab port (COM3) to that port, you could use Arduino's Serial Monitor to show the data - make the Arduino Serial Monitor "pretend" to be the Arduino listening to MatLab.

But on the assumption that you don't have a second port or a NULL Modem serial cable, another alternative is to use a PC program such as "Serial Port Monitor" (I've heard good things about this program, although I've never used it). Apparently you can use it for 14 days to trial it.

Basically it presents a window (you'll want the Window called "Terminal", not any of the others) that shows what is being sent and received on the COM port - even data generated by another program. Shut down MatLab, start this program, tell it to listen to COM3, set the baudrate etc, press Go - then start MatLab again.

When MatLab sends data, you'll see whether it's sending "0.125" or "0y@/" - and assuming that you don't want to decode the latter, how to change your MatLab code so that it looks like the former! Then you can change the Arduino code to understand values like "0.125".

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.