Matlab and arduino: serial port connection

Click For Summary

Discussion Overview

The discussion revolves around establishing a serial port connection between MATLAB and an Arduino. Participants are sharing code snippets and troubleshooting issues related to data transmission, specifically focusing on how to retrieve a single number from the Arduino in MATLAB.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares their Arduino code and MATLAB code, indicating that the output includes multiple characters instead of just the number "3".
  • Another participant suggests checking the parity and number of stop bits as potential issues affecting the serial communication.
  • A participant expresses confusion about the terms "parity" and "stop bits".
  • A reference to external documentation is provided, suggesting the use of LF as a terminator, which another participant notes leads to a terminator error, necessitating the use of CR instead.
  • A later reply proposes modifications to both the Arduino and MATLAB code to ensure consistent serial settings, including specifying data bits, parity, and stop bits in the MATLAB code.
  • There is a question raised about the use of both fread() and fscanf() in the MATLAB code, seeking clarification on their purpose and the content of the variable 'out'.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to resolve the output issue, and multiple competing views on the correct serial settings and methods remain present.

Contextual Notes

There are unresolved aspects regarding the correct configuration of serial communication parameters and the implications of using different terminators. The discussion also reflects varying levels of familiarity with serial communication concepts among participants.

lcr2139
Messages
58
Reaction score
0
Hello, I am trying to hook up MATLAB to my arduino. I am trying to write a number to the serial port in the arduino IDE and retrieving the same number in the MATLAB IDE. My arduino code is:

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {

if (Serial.available()){
Serial.println(3);

delay(100); // delay in between reads for stability
}
}


And my MATLAB code looks like:

%s = serial('COM9');
s = serial ( 'COM9' , 'BaudRate' , 9600, 'terminator' , 'CR' );
%set(s,'BaudRate',4800);
fopen(s);

fprintf(s,'*IDN?')
fread(s)
disp(s)
out = fscanf(s);
fclose(s)
delete(s)
clear s

and the MATLAB output looks like:

10
51
13
10
51
13
10
51
13

How can I change this so only "3" comes out?
Thanks!
 
Physics news on Phys.org
Double check that you have the right parity and numbers of stop bits.
 
Sorry, I'm pretty new at this. What are parity and numbers of stop bits?
 
In http://home.iitb.ac.in/~rahul./ITSP/serial_comm_matlab.pdf the guy uses LF for the terminator. Try it.

J.
 
LF gives a Terminator error so I have to use CR.
 
Okay, then try the following

Arduino

Code:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600,SERIAL_8N1);
}

void loop() {
  if (Serial.available()){
    Serial.println(3);
    delay(100); // delay in between reads for stability
    }
}

On the Matlab side:

Code:
s = serial ( 'COM9' , 'BaudRate' , 9600, 'DataBits', 8, 'Parity', 'none', 'StopBits', 1, 'terminator' , 'CR' );
fopen(s);
fprintf(s,'*IDN?')
fread(s)
disp(s)
out = fscanf(s);
fclose(s)
delete(s)
clear s

This will make sure that everything serial is set the same on both ends of the cable.

Also, something I have some difficulties understanding: you are using fread() and fscanf() in the same run. Why? What's the content of the variable out?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
15K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 9 ·
Replies
9
Views
2K