# Serial write from Arduino to MATLAB

**URL:** <https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156>\
**Category:** Interfacing w/ Software on the Computer\
**Created:** [June 18, 2024, 6:56pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156 "2024-06-18T18:56:30Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![needhelpnow123](https://avatars.discourse-cdn.com/v4/letter/n/958977/32.png) [@needhelpnow123](https://forum.arduino.cc/u/needhelpnow123)\
**Post date:** [June 18, 2024, 6:56pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/1 "2024-06-18T18:56:30Z")

</div>

Hello,  
We are collecting info from a Mettler Toledo scale via an RS232 port. We have an adapter that converts the RS232 to a signal that the Arduino Mega can interpret. We are plugged into Serial Port 1 (18 TX and 19 RX). When we run the following script we get the following output which is what we want.

```arduino
void setup() {
  Serial.begin(115200); // initialise serial monitor port
  Serial1.begin(19200); // initialise Serial1
}

void loop() {
  static String buffer = ""; // Buffer to store characters

  if (Serial1.available()) {
    char receivedChar = Serial1.read();

    if (isDigit(receivedChar) || receivedChar == '.' || receivedChar == ' ') {
      // Append valid characters (digits, decimal point, space) to buffer
      buffer += receivedChar;
    }
    else if (receivedChar == '\n') {
      // If newline is received, print the buffer and clear it
      Serial.println(buffer);
      buffer = "";
    }
  }
}

```

Example output:  
0.00  
0.00  
0.00  
228.37  
656.97  
623.85  
620.52  
620.41  
620.37  
The purpose of this code is to do some preliminary data processing so that when we send it to Matlab it is easier and faster to work with. However, whenever we read the serial port in Matlab, it does not give us the same output as the Arduino with just the numbers instead it gives us the full ascii characters which is not what we are looking for.

---

<div class="post-metadata">

**Author:** ![manhoosbilli1](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@manhoosbilli1](https://forum.arduino.cc/u/manhoosbilli1)\
**Post date:** [June 18, 2024, 7:00pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/2 "2024-06-18T19:00:33Z")

</div>

Seems like you haven't attached matlabs side, probably a problem with receiving the data given that Arduino can successfully read the data and output it.

---

<div class="post-metadata">

**Author:** ![needhelpnow123](https://avatars.discourse-cdn.com/v4/letter/n/958977/32.png) [@needhelpnow123](https://forum.arduino.cc/u/needhelpnow123)\
**Post date:** [June 18, 2024, 7:03pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/3 "2024-06-18T19:03:26Z")

</div>

our matlab code looks like:

```arduino
clear;

a = arduino('COM7', 'Mega2560','Libraries','Serial');
scale_a = device(a,'SerialPort',1,'BaudRate',19200);

while true
disp(char(read(scale_a,1)))
pause(0.5)
end

```

---

<div class="post-metadata">

**Author:** ![manhoosbilli1](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@manhoosbilli1](https://forum.arduino.cc/u/manhoosbilli1)\
**Post date:** [June 18, 2024, 7:16pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/4 "2024-06-18T19:16:27Z")

</div>

probably something like this might work

```arduino
clear;

% Connect to Arduino board
a = arduino('COM7', 'Mega2560', 'Libraries', 'Serial');

% Connect to Serial port 1 with baud rate 19200
scale_a = device(a, 'SerialPort', 1, 'BaudRate', 19200);

% Initialize buffer
buffer = '';

% Infinite loop to read data
while true
    % Read one character at a time
    if scale_a.NumBytesAvailable > 0
        charData = read(scale_a, 1, 'char');
        
        if isstrprop(charData, 'digit') || charData == '.' || charData == ' '
            % Append valid characters (digits, decimal point, space) to buffer
            buffer = [buffer, charData];
        elseif charData == newline
            % If newline is received, print the buffer and clear it
            disp(buffer);
            buffer = '';
        end
    end
    
    % Pause for a short time to avoid excessive CPU usage
    pause(0.01);
end

```

---

<div class="post-metadata">

**Author:** ![needhelpnow123](https://avatars.discourse-cdn.com/v4/letter/n/958977/32.png) [@needhelpnow123](https://forum.arduino.cc/u/needhelpnow123)\
**Post date:** [June 18, 2024, 7:25pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/5 "2024-06-18T19:25:15Z")

</div>

> [@manhoosbilli1](#):
>
> ` charData = read(scale_a, 1, 'char');`

Got an error here that this was not the correct precision. We changed it to uint8 from char. This worked for a few seconds, then the code stopped. Any ideas?

Thanks

---

<div class="post-metadata">

**Author:** ![manhoosbilli1](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@manhoosbilli1](https://forum.arduino.cc/u/manhoosbilli1)\
**Post date:** [June 18, 2024, 7:47pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/6 "2024-06-18T19:47:02Z")

</div>

Probably something like this? just using chatgpt here. hope this helps

```arduino
clear;

% Connect to Arduino board
a = arduino('COM7', 'Mega2560', 'Libraries', 'Serial');

% Connect to Serial port 1 with baud rate 19200
scale_a = device(a, 'SerialPort', 1, 'BaudRate', 19200);

% Initialize buffer
buffer = '';

% Infinite loop to read data
while true
    % Read one character at a time
    if scale_a.NumBytesAvailable > 0
        % Read one byte as uint8
        charDataUint8 = read(scale_a, 1, 'uint8');
        
        % Convert uint8 to char
        charData = char(charDataUint8);
        
        % Check for valid characters
        if isstrprop(charData, 'digit') || charData == '.' || charData == ' '
            % Append valid characters (digits, decimal point, space) to buffer
            buffer = [buffer, charData];
        elseif charData == newline
            % If newline is received, print the buffer and clear it
            disp(buffer);
            buffer = '';
        end
    end
    
    % Pause for a short time to avoid excessive CPU usage
    pause(0.01);
end

```

---

<div class="post-metadata">

**Author:** ![needhelpnow123](https://avatars.discourse-cdn.com/v4/letter/n/958977/32.png) [@needhelpnow123](https://forum.arduino.cc/u/needhelpnow123)\
**Post date:** [June 18, 2024, 7:47pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/7 "2024-06-18T19:47:58Z")

</div>

yeah we also tried chat, hopefully this works

---

<div class="post-metadata">

**Author:** ![needhelpnow123](https://avatars.discourse-cdn.com/v4/letter/n/958977/32.png) [@needhelpnow123](https://forum.arduino.cc/u/needhelpnow123)\
**Post date:** [June 18, 2024, 7:51pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/8 "2024-06-18T19:51:48Z")

</div>

update it did not. returned the right output for 3 lines, then did nothing

---

<div class="post-metadata">

**Author:** ![manhoosbilli1](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@manhoosbilli1](https://forum.arduino.cc/u/manhoosbilli1)\
**Post date:** [June 18, 2024, 8:13pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/9 "2024-06-18T20:13:16Z")

</div>

probably because the buffer on arduino side is empty? might be that data is being sent when you aren't expecting and all matlab is doing is emptying that buffer but then no new data is coming... please confirm that new data is indeed coming in continously.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [June 18, 2024, 10:09pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/10 "2024-06-18T22:09:55Z")

</div>

You might find this Matlab tutorial useful:

> **[Read Streaming Data from Arduino Using Serial Port Communication
- MATLAB...](https://www.mathworks.com/help/matlab/import_export/read-streaming-data-from-arduino.html)**
>
> This example shows how to enable callbacks to read streaming ASCII terminated data from an Arduino® Due using the serialport interface.

---

<div class="post-metadata">

**Author:** ![d\_may123](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@d\_may123](https://forum.arduino.cc/u/d_may123)\
**Post date:** [June 18, 2024, 6:36pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/11 "2024-06-18T18:36:32Z")

</div>

Hello,  
We are collecting info from a Mettler Toledo scale via an RS232 port. We have an adapter that converts the RS232 to a signal that the Arduino Mega can interpret. We are plugged into Serial Port 1 (18 TX and 19 RX). When we run the following script we get the following output which is what we want.

```arduino
void setup() {
  Serial.begin(115200); // initialise serial monitor port
  Serial1.begin(19200); // initialise Serial1
}

void loop() {
  static String buffer = ""; // Buffer to store characters

  if (Serial1.available()) {
    char receivedChar = Serial1.read();

    if (isDigit(receivedChar) || receivedChar == '.' || receivedChar == ' ') {
      // Append valid characters (digits, decimal point, space) to buffer
      buffer += receivedChar;
    }
    else if (receivedChar == '\n') {
      // If newline is received, print the buffer and clear it
      Serial.println(buffer);
      buffer = "";
    }
  }
}

```

Example output:  
0.00  
0.00  
0.00  
228.37  
656.97  
623.85  
620.52  
620.41  
620.37  
The purpose of this code is to do some preliminary data processing so that when we send it to Matlab it is easier and faster to work with. However, whenever we read the serial port in Matlab, it does not give us the same output as the Arduino with just the numbers instead it gives us the full ascii characters which is not what we are looking for.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [June 18, 2024, 10:12pm UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/13 "2024-06-18T22:12:57Z")

</div>

Duplicate post to [Serial write from Arduino to MATLAB](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156)

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [June 19, 2024, 8:29am UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/14 "2024-06-19T08:29:32Z")

</div>

@d_may123 , do not cross post; you're wasting peoples time. Your two topics have been merged.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 16, 2024, 8:29am UTC](https://forum.arduino.cc/t/serial-write-from-arduino-to-matlab/1273156/15 "2024-12-16T08:29:44Z")

</div>

Closed on December 16, 2024, 8:29am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
