Trouble sending Arduino serial output 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.

1 Like

3 posts were merged into an existing topic: Serial write from Arduino to MATLAB