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