# Read data from RS232 output using Arduino Support Package on MATLAB

**URL:** <https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161>\
**Category:** General Guidance\
**Created:** [June 7, 2024, 1:25pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161 "2024-06-07T13:25:33Z")\
**Posts on this page:** 14\
**Page:** 1

<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 7, 2024, 1:25pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/1 "2024-06-07T13:25:33Z")

</div>

I want to actively monitor the weight of a scale (eventually a total of 5) specifically the VGW 10001 check weigh scale. I know that it has data transmission through the TXD wire of a RS232 port. I have an Arduino MEGA that is connected to MATLAB using the Arduino IDE which has been working great, and I think that I should be able to read the data at any baud rate under 115201.

I am just struggling to know where to start or if it is even possible as reading this type of data is super new to me. Especially since not many people have done this through the MATLAB relationship specifically.

---

<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 7, 2024, 1:57pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/2 "2024-06-07T13:57:33Z")

</div>

You will need an RS232-to-TTL converter; the Mega can't handle RS232. Use one of the spare serial ports (Serial1, Serial2, Serial3).

Next you can start analysing the data

```cpp
void setup()
{
  Serial.begin(115200);
  Serial1.begin(whateverBaudrateIsNeeded);
}

void loop()
{
  if(Serial1.available())
  {
    byte b = Serial1.read();
    if (b < 0x10)
    {
      Serial.print("0");
    }
    Serial.print(b, HEX);
    Serial.print(" ");
  }
}

```

What the numbers mean should be in the datasheet / user manual of the scale. You should post a link to that datasheet / user manual if you need more help.

If the protocol is text based, you can simplify the above code to

```cpp
void setup()
{
  Serial.begin(115200);
  Serial1.begin(whateverBaudrateIsNeeded);
}

void loop()
{
  if(Serial1.available())
  {
    byte b = Serial1.read();
    Serial.write(b);
  }
}

```

Note:  
In first instance, use serial monitor to analyse the data; matlab stuff can come later.

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [June 7, 2024, 1:59pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/3 "2024-06-07T13:59:41Z")

</div>

> [@d\_may123](#):
>
> I think that I should be able to read the data at any baud rate under 115201.

No, the baud rates are very specific, based on the chip's clock rate.

---

<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 7, 2024, 2:50pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/4 "2024-06-07T14:50:01Z")

</div>

> [@d\_may123](#):
>
> VGW 10001 check weigh scale

The RS232 communications are configurable, so consult the user manual to determine how to set the Baud rate (or just use the default).

As mentioned, you will need an [RS232 to TTL level converter](https://www.pololu.com/product/126) to avoid damaging the Arduino input pins.

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [June 8, 2024, 2:16pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/5 "2024-06-08T14:16:44Z")

</div>

it is a good idea to check the connections to the TTL-RS232 modules by performing a loopback test, e.g.

```cpp
// Arduino Mega serial1 test

// mega Serial1 pin 18 is TX1
// Serial1 pin 19 is RX1
// for loopback test connect pin 18 to pin 19

// for RS232 shield connect
// Mega pin 18 TX1 to TTL/RS232 Tx
// Mega pin 19 RX1 to TTL/RS232 Rx
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)
// connect GND pins together and VCC to 5V

void setup() {
  Serial.begin(115200); // initialise serial monitor port
  Serial1.begin(9600);//115200); // initialise Serial1
  Serial.write("Arduino Mega Serial1 test - for loopback test connect pin 18 to pin 19\n");
  Serial.write("RS232: Mega pin 18 TX1 to TTL/RS232 Tx and pin 19 RX1 to TTL/RS232 Rx\n");
  Serial.write("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}

void loop() {
  if (Serial1.available()) { // read from Serial1 output to Serial
    Serial.write(Serial1.read());
  }
  if (Serial.available()) { // read from Serial outut to Serial1
    char inByte = Serial.read();
    //Serial.write(inByte); // local echo if required
    Serial1.write(inByte);
  }
}

```

serial monitor output

```arduino
Arduino Mega Serial1 test - for loopback test connect pin 18 to pin 19
RS232: Mega pin 18 TX1 to TTL/RS232 Tx and pin 19 RX1 to TTL/RS232 Rx
RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx

TTL to RS232 module loopback test pin 2 connected to pin 3
test 2 1234567890
test 3 abcdefghijk

```

---

<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 11, 2024, 1:20pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/6 "2024-06-11T13:20:14Z")

</div>

Sorry for the late reply, the adaptor just came in.

Here is what the scale spat out:  
Arduino MegRx  
RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx  
S232: Mega pin 18 TX1 to TTL/RS232 Tx and pin 19 RX1 to TTL/RS232 Rx  
RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx

5⸮⸮.⸮&C2\⸮⸮+⸮

---

<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 11, 2024, 1:20pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/7 "2024-06-11T13:20:54Z")

</div>

The gibberish makes me think that the baud rate isnt matched, but the scale says it is outputting at 9600.

---

<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 11, 2024, 1:39pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/8 "2024-06-11T13:39:19Z")

</div>

I found an old Mettler Toledo scale which has better documentation online, so I decided to use that. Would any issues arise with the 8 bit output with 1 stop bit, or would the arduino be able to understand it.  
Here is my current wiring:

 ![IMG_2208](https://europe1.discourse-cdn.com/arduino/original/4X/2/7/a/27a8057f6227a256edf56039cc93c2467fb30275.jpeg)

(operation Manual: (page 51 is what I used)  
[https://www.bing.com/ck/a?!&&p=b7b8f5e46d9df13bJmltdHM9MTcxODA2NDAwMCZpZ3VpZD0xZDAzMDE3MC1kMDMxLTY5MzEtMjZkNS0xNWY1ZDE1MzY4MGImaW5zaWQ9NTIwNA&ptn=3&ver=2&hsh=3&fclid=1d030170-d031-6931-26d5-15f5d153680b&psq=mettler+toledo+xp6002sdr+manual&u=a1aHR0cHM6Ly93d3cubXQuY29tL2RhbS9tdF9leHRfZmlsZXMvRWRpdG9yaWFsL0dlbmVyaWMvMy9YUF9QcmVjaXNpb25fQkFfRWRpdG9yaWFsLUdlbmVyaWNfMTExNjQxOTU3NTk5OV9maWxlcy94cC1wcmVjLWJhLWUtMTE3ODA1NzJlLnBkZg&ntb=1](https://www.bing.com/ck/a?!&&p=b7b8f5e46d9df13bJmltdHM9MTcxODA2NDAwMCZpZ3VpZD0xZDAzMDE3MC1kMDMxLTY5MzEtMjZkNS0xNWY1ZDE1MzY4MGImaW5zaWQ9NTIwNA&ptn=3&ver=2&hsh=3&fclid=1d030170-d031-6931-26d5-15f5d153680b&psq=mettler+toledo+xp6002sdr+manual&u=a1aHR0cHM6Ly93d3cubXQuY29tL2RhbS9tdF9leHRfZmlsZXMvRWRpdG9yaWFsL0dlbmVyaWMvMy9YUF9QcmVjaXNpb25fQkFfRWRpdG9yaWFsLUdlbmVyaWNfMTExNjQxOTU3NTk5OV9maWxlcy94cC1wcmVjLWJhLWUtMTE3ODA1NzJlLnBkZg&ntb=1)

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [June 11, 2024, 2:11pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/9 "2024-06-11T14:11:37Z")

</div>

> [@d\_may123](#):
>
> The gibberish makes me think that the baud rate isnt matched, but the scale says it is outputting at 9600.

try 4800.,19200, 38400, 57600, 115200

if you have an oscilloscope measure the width of one bit to get an idea of the [baud rate](https://lucidar.me/en/serialib/most-used-baud-rates-table/) , e.g. 9600b baud is 104.167uSec

Oscilloscope display - transmitting letter a (hex 0x61 binary 01100001) [ASCII code](https://www.ascii-code.com/) (bit 0 transmitted first) at 9600baud width of 1 bit is measured at 110uSec  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/8/4/0/84039dc47047346d48ce890dc46dc96285ff4990.png)

change timebase to get more accurate reading  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/5/8/0/5804e9028486f27346656ab2e16edd31dfd4f2db.png)

---

<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 11, 2024, 2:33pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/10 "2024-06-11T14:33:12Z")

</div>

I have just updated a setting on the scale to turn handshake to off. I am now getting a reading on the Arduino output that gives the correct weight of the scale for a second, but after this it outputs an infinite stream of question marks and gibberish.

Here is the latest output of the code.

RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx  
S D 1339.4 g  
TX1 to TTL/RS232 Tx and pin 19 RX1 to TTL/RS232 Rx  
RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx  
S D 1339.4 g  
⸮⸮d⸮⸮kC0⸮T,⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮.........

I have spared you by not including every single question mark. lol

Any ideas as to what could be causing this infinite stream of characters?

---

<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 11, 2024, 2:50pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/11 "2024-06-11T14:50:50Z")

</div>

> [@d\_may123](#):
>
> but after this it outputs an infinite stream of question marks and gibberish.

What is the sketch that produced that? I suspect that you continuously send to the serial monitor while nothing is received; the question marks are in that case the -1 returned by SerialX.read() which indicates that nothing was received.

---

<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 11, 2024, 3:26pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/12 "2024-06-11T15:26:49Z")

</div>

Here is the sketch:

```arduino

// for RS232 shield connect
// Mega pin 18 TX1 to TTL/RS232 Tx
// Mega pin 19 RX1 to TTL/RS232 Rx
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)
// connect GND pins together and VCC to 5V

void setup() {
  Serial.begin(115200); // initialise serial monitor port
  Serial1.begin(9600);//115200); // initialise Serial1
  Serial.write("Arduino Mega Serial1 test - for loopback test connect pin 18 to pin 19\n");
  Serial.write("RS232: Mega pin 18 TX1 to TTL/RS232 Tx and pin 19 RX1 to TTL/RS232 Rx\n");
  Serial.write("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}

void loop() {
  if (Serial1.available()) { // read from Serial1 output to Serial
    Serial.write(Serial1.read());
  }
  if (Serial.available()) { // read from Serial outut to Serial1
    char inByte = Serial.read();
    //Serial.write(inByte); // local echo if required
    Serial1.write(inByte);
  }
}

```

---

<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 11, 2024, 6:52pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/13 "2024-06-11T18:52:07Z")

</div>

IT IS DONE!!!

Fo anyone wanting to do this, here is the MATLAB code (I am aware this is an arduino forum, but an arduino is used). It most definitely is not optimized or perfect, but with some tinkering you probably could get it to fit.

```arduino
clear
a = arduino('COM7', 'Mega2560', 'Libraries', 'Serial');
serialdevObj = device(a,'SerialPort',1,'BaudRate', 19200, 'DataBits',8, 'StopBits', 1);
out = read(serialdevObj, 63);

% Find the index every occurrence of 32
index_of_32 = find(out == 32);

offset = 0;
stop = 0;

% Check if there are four consecutive occurrences of 32 starting from the index found
while true

out = read(serialdevObj, 63);
% Find the index every occurrence of 32
index_of_32 = find(out == 32);
offset = 0;
stop = 0;

for j = 1:length(index_of_32)

if stop == 1
    break
end
offset = 0;
if out((index_of_32(j)):(index_of_32(j)+3)) == 32
    offset = 4;
    c = char(out((index_of_32(j)+offset):(index_of_32(j)+offset+6)));
    stop = 1;
    if out((index_of_32(j)):(index_of_32(j)+4)) == 32
    offset = 5;
    c = char(out((index_of_32(j)+offset):(index_of_32(j)+offset+5)));
    else
    end
else
end
end
disp(c)
pause(0.50)
end

```

---

<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 8, 2024, 6:52pm UTC](https://forum.arduino.cc/t/read-data-from-rs232-output-using-arduino-support-package-on-matlab/1269161/14 "2024-12-08T18:52:47Z")

</div>

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