Hello,
I have been working on a code to try and communicate the Mettler PM2000MC scale and the Arduino board together. I am using the pre-built circuit of the RS232 and then i just connect a cable between the scale and that part of the circuit. Essentially I used the code that was originally used to just test out an RS232 connection which was from the Arduino website. So far this is what I have with the code..
//Created August 23 2006
//Heather Dewey-Hagborg
//http://www.arduino.cc
#include <ctype.h>
#define bit9600Delay 100
#define halfBit9600Delay 50
#define bit4800Delay 188
#define halfBit4800Delay 94
byte rx = 6;
byte tx = 7;
byte SWval;
void setup() {
pinMode(rx,INPUT);
pinMode(tx,OUTPUT);
digitalWrite(tx,HIGH);
delay(2);
digitalWrite(13,HIGH); //turn on debugging LED
SWprint('T'); //debugging hello
SWprint(13);
SWprint(10); //carriage return
}
void SWprint(int data)
{
byte mask;
//startbit
digitalWrite(tx,LOW);
delayMicroseconds(bit9600Delay);
for (mask = 0x01; mask>0; mask <<= 1) {
if (data & mask){ // choose bit
digitalWrite(tx,HIGH); // send 1
}
else{
digitalWrite(tx,LOW); // send 0
}
delayMicroseconds(bit9600Delay);
}
//stop bit
digitalWrite(tx, HIGH);
delayMicroseconds(bit9600Delay);
}
int SWread()
{
byte val = 0;
while (digitalRead(rx));
//wait for start bit
if (digitalRead(rx) == LOW) {
delayMicroseconds(halfBit9600Delay);
for (int offset = 0; offset < 8; offset++) {
delayMicroseconds(bit9600Delay);
val |= digitalRead(rx) << offset;
}
//wait for stop bit + extra
delayMicroseconds(bit9600Delay);
delayMicroseconds(bit9600Delay);
return val;
}
}
void loop()
{
SWval = SWread();
SWprint(toupper(SWval));
}
Now I have set the SWprint to 'T' because that will Tare the scale. This part of the code works perfectly. If I have a certain mass on the scale and run the program, once I press reset on the board it will clear the weight on the scale. This process works perfectly.
My problem is when I try to change the command of the scale to 'S' or 'SI' for it to send me mass values. The commands I am getting are from the manual book of the scale and I have already matched up the Baud rate, data bits, parity, and stop bits. At the end of all this, I am just trying to get the mass reading from the scale to my computer. Honestly I'm not even sure what program to use to get the mass reading from the scale (Serial Monitor, Hyper terminal). Ultimately my end goal is, I will try to communicate the scale and try and run it with the Shield to record data on the SD card.
Any information or anything would be helpful.
-Thanks
Hi, welcome to the forum.
Can you tell what you use for RS-232 ? I want to be sure that it is okay.
Is the device set in a mode that no handshake is used ?
The Arduino serial monitor can be used for other devices as well. I also use Q Serial Monitor in Windows, or gtkterm in linux.
You have to test everything on a computer. Can you get the mass value on a computer ?
Using a software serial port makes it a lot harder. At least use the Arduino SoftwareSerial instead of the code in your sketch.
Which Arduino board do you use ?
The Arduino Leonardo has a spare hardware serial port.
I attached the image of the Rs 232 and I am using an Arduino Uno. The LED is the debugging LED that is hooked up across pin 13 and ground. The other two connections go to pin 7 and 6 which are my TX and RX. The last two wires go to ground and 5V.
My problem is with the mass values, getting the computer to tell me the mass on the scale. Like I said with the code I have, I can send a tare command and that works. Any command where my computer needs to display a weight it doesn't work.
Also recently I have been trying to use an Rs 232 to USB converter. Essentialy I connect that right up to the cable from the scale and into my computer. I have been trying to use Cool Term, BC Wedge, and Advanced Serial Data Logger but I am having problems with the connection then. I want to stick with Arduino but I wanted to try these new ways outs.
I tried to do this little communication test using this Rs 232 to USB converter but it didn't work either.
http://us.mt.com/us/en/home/supportive_content/product_documentation/installation_instructions/PS_Comm_Tst/jcr:content/download/file/file.res/Ser_Communication_Test_PS_scale_7-23-09.pdf
The RS232 converter is okay.
The led is not okay. You need a resistor for the led, about 270 to 1k. You have to fix this before using the Arduino board again.
Can you find a computer with a 'real' RS-232 port ? I still have a computer with a RS-232 port.
A usb-rs232 converter should also work. When you say that it didn't work, then I wonder what exactly didn't work.
I think it is important to have it working from a computer. Did you try Q Serial Terminal ?
The Arduino serial monitor uses 8 bit, no parity. You need a program that can set it as 7 bit, even parity.
Did you turn off the hardware handshake on the Mettler scale ?
It seems that only a 'W' should be typed without CR or LF.
The SoftwareSerial doesn't seem to do 7bit Even parity 
The hardware serial port does support it : https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/HardwareSerial.h
The Arduino Leonardo has a spare serial port. I could be initialized like this : Serial1.begin(9600, SERIAL_7E1);
I'm not sure that it is implemented in the library.
I don't know if the code in your sketch will work. Perhaps that code is more than a year old, and the Arduino uses a different compiler which could change the timing.
Some Arduino users on this forum advice to use the Arduino default 8 bit data, and calculate the outgoing parity in the sketch, and strip the incoming parity. That is a good option. The SoftwareSerial could be used with this.