Read data from RS232 output using Arduino Support Package on MATLAB

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.

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

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

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.

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

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 to avoid damaging the Arduino input pins.

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

// 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 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

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\⸮⸮+⸮

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

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:

(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

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 , e.g. 9600b baud is 104.167uSec

Oscilloscope display - transmitting letter a (hex 0x61 binary 01100001) ASCII code (bit 0 transmitted first) at 9600baud width of 1 bit is measured at 110uSec
image

change timebase to get more accurate reading
image

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?

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.

Here is the sketch:


// 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);
  }
}

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.

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

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