Serial write from Arduino to MATLAB

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.

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.

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.

our matlab code looks like:

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

probably something like this might work

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

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

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

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

yeah we also tried chat, hopefully this works

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

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.

You might find this Matlab tutorial useful:

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.

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.

1 Like

Duplicate post to Serial write from Arduino to MATLAB

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