Communicating Mettler Toledo XS4002S with PC Via RS232


mylesp

Recommended Posts

OK, the Mettler Toledo balance uses the Mettler standard MT-SICS protocol which is a simple ASCII protocol. For example, to retrieve the stable weight value, you send an S followed by a CRLF pair, and the device will respond with S, followed by space, followed by another S followed by the weight (padded with spaces) followed by another space, and then the units. Unfortunately as is typical I'm afraid, the manual is not completely clear. Without being able to try this on a real device, I don't know if the S's in the response are an echo of the command, or have some other meaning, especially since there are two of them. You'll probably need to try other commands, like SI and see what they do.

First thing you should always do though is try it manually. First create a new serial / ethernet device. Go to quick-device configuration and select new Serial / Ethernet device. Add a new RS232 comm port. Call it whatever you want (stick with lower case). I can't say what the comm parameters will be. You'll likely have to configure or retrieve them from the balance itself since they are configurable. The docs don't indicate what the defaults are. I'd use 9600, 8, N, 1. Don't use flow control (handshake). Leave the end of line as CRLF. Once you've created the comm port, select Monitor to monitor is. From here you can type in commands manually and see what the device responds. To start, do the S command. Enter:


S\013\010

[/CODE]

then click on the Output button. You should get a response from the device with the weight. If not, your comm parameters or wiring is likely wrong. If so, try SI and see if the response is SI SI or SI S or some other combination.

Once you are done manually communicating, close the monitor window, select the NULL device, give your device a name (like Mettler) and hit OK. Next we need a place to put the weight measurement. Create a new channel, call it weight, device type "Test", "A to D", Timing = 0. Next we need script to poll the device. I'm going to do this off the cuff since I don't have the hardware. The script will look something like this:

[CODE]
private string datain
private data
while(1)
device.mettler.purge()
device.mettler.write("S" + chr(13) + chr(10))
datain = device.mettler.readUntil(10)
data = strToDouble(mid(datain,3, 1000))
weight.addValue(data)
delay(1)
endwhile


[/CODE]

That's the simple of it. You'll want to add some error handling, but try and get this working first so you can see any errors. A few important points:

purge() clears out whatever may be in the serial port. A good idea before any comms.

chr(13) + chr(10) generate a CRLF pair.

readUntil(10) reads all the data on the serial port up to the LF character

mid() takes a part of a string. The string is the S S xxx g response. Starting at character 3 skips the two S's. 1000 is just a big number so it gives us the rest of the line. Note that if you do other commands like SI and it returns SI SI instead of S S, you'll need to increase the 3 to 5.

strToDouble() converts a string to a number. The string must start with a number (can't start with "S"), but can end with an invalid character like "g" or whatever units you have.

addValue() shoves the result into your channel

delay(1) waits 1 second then loop around to do it all again, thus continually polling your scale

Link to comment
Share on other sites

Hi. So I got as far as the manual trial. I checked the device comm parameters and they are all as you suggested above. When I entered the S\013\010 command and clicked send, it gave back the response attached below. I tried checking and unchecking the various boxes supplied and changing from S to SI and each line here is one try.

post-63-0-75927400-1334248206_thumb.jpg

Link to comment
Share on other sites

P.S. I also changed out the Cable itself but the results were the same. Do you think there is possibly an issue with the cable? I heard somewhere that the 2 and 3 pins may need to be 'short wired'. Do you have any insight into this?

Link to comment
Share on other sites

Your device didn't respond. All you have is Tx. If it was working you'd get Rx. The boxes you check have no affect on what is being transmitted or received. They just control how the data is displayed.

I don't know what they mean by 'short wired' You certainly don't want to short across them. This isn't half duplex, and I haven't seen half-duplex in RS232 in a long, long time. Your cable does have to be short. I believe the limit of RS232 is 15', but could be wrong. You only need to wire pins 2, 3 and 5. You must wire pin 5 through to pin 5 on the PC. They have pin 2/3 flipped from the PC port (typical), so you can use a straight through cable, pin 2 to pin 2, pin 3 to pin 3. If that doesn't work, cut the cable and connect 2 to 3, and 3 to 2, typically this would be the red and brown wires on a commercial cable, pin 5 is typically yellow (follows resistor color codes). Make sure you have handshaking off in the balance. Triple check your comm parameters.

Link to comment
Share on other sites

I found the problem with my cable. Loose connection on the gender bender I used. Replaced it with a new one and have it communicating now. Thanks so much for this.

I tried the little sequence as well and got a couple of errors. I initially had a problem due to a typo but then the sequence ran for ~10 seconds before getting a timeout error. See attached.

Any insight as to what the issue is? I feel I'm very close to getting what I need out of this. All that will be left is the exporting of data to Excel after the data collection.

Then I'll be looking to invest in a Labjack U-12 which seems to be an easier beast to handle than the RS-232 connection!

Again thanks for your help with this. Very good service.

Link to comment
Share on other sites

Sorry, you seem to be having problems with attachments. You can't cut and paste. You must select the attachment's button and then upload the file (so two buttons, one to select the file, one to upload it).

You are thinking of using a U12 to do serial comms? That would be a big mistake. The U12 is a great product, but you'll want to use the regular PC serial port for serial comms.

Link to comment
Share on other sites

I will be leaving the balance with the serial port. I will be using a pressure transducer on the same skid the balance is used. Actually, any suggestions on what make/type of transducer to use? I was thinking http://www.omega.co.uk/ppt/pptsc.asp?ref=PXM309&Nav=preb03 with a standard power supply.

I can't seem to find the attachment button. Email sent with attachment.

Link to comment
Share on other sites

HI,

So the data aquisition is working in sequence now. I needed to put the "SI" command in instead of "S" when the balance reading is changing quickly. This command will send the balance value regardless of stability whereas the S command will only send if the balance deems itself stable. Can you advise a suitable sequence for the error handling as you previously suggested.

Thanks again for all your time and effort on this.

Link to comment
Share on other sites

Sure:

private string datain
private data
while(1)
try
device.mettler.purge()
device.mettler.write("S" + chr(13) + chr(10))
datain = device.mettler.readUntil(10)
data = strToDouble(mid(datain,3, 1000))
weight.addValue(data)
catch()
? strLastError
endcatch
delay(1)
endwhile[/CODE]

Link to comment
Share on other sites

  • 1 year later...

I know some Mettler scales will communicate with Modbus serial and/or Modbus TCP, though you may have to buy a comm expansion card.  Any time you can use a common standardized protocol,  especially one as ubiquitous as Modbus, that's just so much easier than having to roll your own, I'd advise that if it's an option on your scale.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.