How to capture only the end result from a Mettler Toledo H53 Moisture Analyzer with 232key

Real-life example: Moisture Analyzer

One of customers wanted to capture the end result from his Mettler Toledo H53 moisture analyzer using our 232key software. However, the moisture analyzer sent a lot of data at the end of the moisture determination process:

14:08:42 Port opened: COM3 Bitrate: 9600 Data bits: 8 Stop bits: 1 Parity: NONE
...
14:08:45 -MOISTURE DETERMINATION-<13><10>
14:08:45 METTLER TOLEDO<13><10>
14:08:45 Type             HE53/01<13><10>
14:08:45 SNR           B123456789<13><10>
14:08:45 SW                  1.13<13><10>
14:08:45 <13><10>
14:08:45 Drying Prog.       Rapid<13><10>
14:08:45 Drying Temp.      130 <29>C<13><10>
14:08:45 Switch-Off          Auto<13><10>
14:08:45 Display mode         %AM<13><10>
14:08:45 <13><10>
14:08:45 Start Weight   0.637 g  <13><10>
14:08:45 <13><10>
14:08:45 Total Time      4:20 min<13><10>
14:08:45 Dry Weight     0.604 g  <13><10>
14:08:45 End Result      5.46 %AM<13><10>
14:08:45 <13><10>
14:08:45 Date: ..............<13><10>
14:08:45 Time: ..............<13><10>
14:08:45 <13><10>
14:08:45 Comment: ...............<13><10>
14:08:45 Signature: .............<13><10>
14:08:45 --------- END ----------<13><10>
14:08:45 <13><10>
14:08:45 <13><10>
14:08:45 <13><10>

Our customer was only interested in the end result (5.46 in the example above), but 232key captured and typed all the numbers it could find, resulting in the following output:

531234567891.131300.63740.6045.46

Regular Expressions to the rescue

Our software includes a powerful tool for such cases: regular expressions. Don’t worry, you don’t have to learn about them in detail. You can simply send us the data received from your device as displayed in the Event Log of our software (as shown above) and we’ll assist you. However, in some cases, all it takes is a small modification.

If you go to the Input tab in 232key and click on the Customize button, the currently used regular expression will be displayed:

Regular Expression in 232key MU

For the “Generic measuring device” and many other device profile, it looks as follows:

([-+]?\s*[0-9]*[\.,]?[0-9]+)

This regular expression captures the first number it encounters in each line of data. If you want to capture only the data of a specific line and ignore all others, look at what makes it unique. In this case, we could use the words “End Result”. Simply inserting them at the beginning, followed by “\s+” (which stands for “one ore more whitespace characters”) ensures that 232key only captures the number following these words:

End Result\s+([-+]?\s*[0-9]*[\.,]?[0-9]+)

That’s all it takes in this case. Alternatively, we could use the “%” character appearing at the end of the “End Result” line. As there’s no other line in the report where a number is followed by a percentage sign, the following regular expression also works (and will continue to work even if the user changes the language of the report):

([-+]?\s*[0-9]*[\.,]?[0-9]+)\s+%

Regular expressions are available in 232key Plus, 232key MU, 232key Pro and our Simple Data Logger software.

How to write the date, time and weight from your scale to Excel

For this article we assume that your scale is equipped with an interface and that it sends the date, time and weight in the following format:

Date 10/30/2018
Time 11:58:45 AM
Gross 376.5 g

We present two methods for transferring these values to Excel.

1. Use a keyboard simulation (virtual keyboard wedge)

You can use our software 232key to enter the values into Excel (or any other application) as if you had typed them on your keyboard. However, by default, 232key captures the first number in each line, which means it would only type the values shown in blue:
Date and time partially captured, weight captured completely

While the weight has been captured correctly, the date and time are incomplete. This can be fixed in 232key Plus as follows.

1. In the Input tab, set the device to “Barcode alphanumeric extended*:
Barcode alphanumeric extended

This enables 232key to type all printable ASCII characters including the slash and colon we need for the date and time.

2. Modify the regular expression 232key uses to capture data by clicking on the Customize button. Replace the existing expression with the following one:

\s*(\d+/\d+/\d+|\d+:\d+:\d+\s[AP]M|[-+]?\s*\d*.?\d+)

This expression will capture the date, time (in 12h AM/PM format) or weight. This only works if the values are transferred in individual lines, as in our example.

3. In the Output tab, instruct 232key to send “TAB” after the two first values and “Enter” after the third one:
Write values in three columns

232key will then write the date, time and weight in separate columns and jump to the next row:
Date, time and weight in Excel

2. Write the weight to a file (with date and time) and later open it in Excel

As an alternative to the “keyboard simulation” method described above, you can use our software Simple Data Logger (SDL) to write the weight to a file. This file can later be opened in Excel (or other applications).

This approach has two advantages:

  • Other keyboard operations can’t interfere with data acquisition and
  • Excel does not have to be open and running in the foreground (it does not even have to be installed on the same computer).

It is therefore particularly suitable for long-running unattended operations.

Concerning the date and time, you have two options when using SDL: Use the computer’s date and time or use the date and time sent by your scale.

a) Use the computer’s date and time

You’d usually choose this option if your scale only sends the weight or if the date and time format used by your scale does not match your requirements.

1. Choose a file in the Output tab in SDL. Click on the Set values button to set the default date and time formats (you can also directly modify the format strings, e.g. to use a 24h time format):
Time and date format

2. To make sure that only the weight is captured (and not the date and time sent from the scale), go to the Input tab, click on the Customize… button and add “\s*g” to the regular expression:
Capture weight in grams

Now, only values expressed in grams [g]  will be captured:
Weight in grams captured (shown in blue)

3. Even though they’re not captured, the other two lines sent from the scale (date and time) will still be written to the file as empty lines. In SDL Plus, we can combine all 3 lines into one row in the Process tab:
Combine 3 lines into one row

The CSV file generated by SDL can later be opened in Excel by simply double-clicking on it:
Excel with computer date/time and weight

As mentioned above, the two empty columns preceding the weight appear due to the date and time values which were sent from the scale, but were not captured. You can simply delete them.

b) Using the date and time sent by your scale

The procedure is very similar to what we did with 232key above.

1. In the Input tab, set the device to “Generic text device” to prevent SDL from treating the captured values as numbers:
Generic text device

2. Replace the regular expression by clicking on the Customize… button:

\s*(\d+/\d+/\d+|\d+:\d+:\d+\s[AP]M|[-+]?\s*\d*.?\d+)

This will ensure that the date, time and weight values sent by the scale are all captured.

3.  In the Process tab, combine the 3 lines sent from the scale into one row (functionality only available in SDL Plus):
Combine 3 lines into one row

4. In the Output tab, make sure that the date and time are set to “None” (unless you also want to add the computer’s date and time to each row):
No date and time added

The event log in SDL shows that all three values were captured:
Simple Data Logger event log: with date time weightThe resulting file contains the date, time and weight sent from the scale neatly written into 3 columns:
Scale date, time and weight in Excel

Links and further information

Ohaus Scout STX scale: capturing the weight, date and time with 232key

Our software 232key was originally designed to make it as easy as possible to capture the weight sent by a scale or balance (and then transmit it to another application as simulated keystrokes). As most weighing instruments also include the unit and possibly other characters, 232key assumes that the first (decimal) number it encounters is the weight. If you also want to capture the date and time sent from a scale, this can lead to unwanted results.

Capturing the net weight

Let’s look at a simple example first. Below you’ll see the data received from an Ohaus Scout STX scale configured to transmit only the net weight (as it appears in the event log in 232key):

Net: 302.4 g ? N<13><10>

The blue characters were captured by 232key using the “Ohaus” device profile. In this example, 232key managed to successfully capture the weight.

Capturing the net and tare weight

If we change the “Print Content” of the Ohaus STX scale so that it sends both the net and tare weight, the event log shows that 232key still captures both values:

Net: 302.4 g ? N<13><10>
Tare: 7.9 g T<13><10>

To have them appear in separate columns in spreadsheet applications like Excel, OpenOffice Calc or Google Sheets, we can go the output tab and modify the end with settings as shown below:

Alternate TAB with Enter every 2 values

Alternating the “TAB” with the “Enter” key every 2 value will produce the desired result (shown in Google Sheets):

Net and Tare weight in separate columns

Capturing date and time

Things get significantly more complicated if we configure the scale to also send the date and time. The event log now looks as follows:

11/17/2017 15:04:21<13><10>
Net: 302.4 g N<13><10>
Tare: 7.9 g T<13><10>

Using its default strategy of capturing the first number in each line, 232key only captures “11” and ignores the rest of the combined date/time line (as the backslash cannot be a part of a decimal number).

We can try to fix this by going to the device tab and setting the device to “Barcode alphanumeric extended” (available in the Plus version). This device appears in italics, which means that it is a “text” device instead of a “numeric” device. Note: Do not click the set defaults button after making this change.

As the scale is now sending three lines of data, we should also enter the number “3” in the end with settings in the output tab:

Alternate TAB with Enter every 3 values

If we press the print button on the scale again, the event log will show that all data has been captured by 232key:

11/17/2017 15:09:51<13><10>
Net: 302.2 g N<13><10>
Tare: 7.9 g T<13><10>

The output in Google Sheets looks as follows:

Google Sheets showing all data in 3 columns

If we simply wanted to record all data sent from the scale, this would be fine. However, it is not ideal for further processing for two reasons:

  1. Date and time appear together in one column.
  2. The measurement values are not recognized as numbers because the cells contain additional text.

232key does not support extracting multiple values from one line of data, so there’s nothing we can do about the first issue (although you could use 232key Pro with a custom script instead). Unfortunately, our Ohaus Scout STX scale cannot send the date and time in separate lines.

The second issue can be fixed using custom regular expressions. To do this, go to the device tab, click on the customize button and replace the existing regular expression with the following one:

(\d{2}/\d{2}/\d{4}\s+\d{2}:\d{2}:\d{2}|-?\d*\.\d+)

Note: This regular expressions assumes the scale is using the 24 hour time format.

The event log and the output in the spreadsheet now look as follows:

15:38:40 11/17/2017 15:39:01<13><10>
15:38:40 Net: 302.2 g N<13><10>
15:38:40 Tare: 7.9 g T<13><10>

Date, time, net weight and tare weight captured from Ohaus Scout STX using a regular expression

The cells containing the net and tare weights are recognized as numbers and can be used for further calculations.

If we just wanted to capture the time (and maybe add a “date” column manually), we could use the following regular expression:

(\d{2}:\d{2}:\d{2}|-?\d*\.\d+)

Please note:

  • The regular expressions above have been customized to work with the Ohaus Scout STX scale (configured to send the date, time and weight). They might not work with other scales and balances.
  • As we’re now using a “text” device, the computation and rounding settings in the process tab have been disabled (you can’t round non-numeric characters).
  • The option to change the decimal separator from a dot to a comma is also no longer available (and our custom regular expression does not capture decimal numbers using a comma as the separator).
  • For data logging purposes, consider using our new Simple Data Logger software instead of 232key. It has the ability to add the date and time to each weight. The CSV files it produces can be easily opened in Excel and other spreadsheet apps.

What to do when 232key captures too many values from your scale

232key uses pre-defined regular expressions to match the data coming from the connected device and to capture the value you’re interested in. In this article, we’ll demonstrate the use of custom regular expressions  (available in the paid Plus version) to capture the (net) weight from a balance and avoid capturing values we do not want to be typed. The instructions posted below can be applied to all kinds of serial devices (not only balances and scales).

When 232key captures too many values

For this example, we’ll be using an Adam Equipment HCB 3001 portable precision balance. It’s equipped with an RS-232 and USB interface. Either one works with 232key:

Adam HCB (Highland) balance connected to a laptop via USB

232key comes with a device profile for this balance. When you select it in the input tab and then press “Set defaults” to set the interface parameters, the following message appears:

Adam HCB output format message

This message asks you to modify the balance’s output format so that it only sends the (net) weight to the connected PC. If you change this setting, you won’t have to modify the regular expression in 232key as shown below. However, many other balances and scales have a fixed output format which cannot be modified (you can find multiple real-life example in our support forum). To simulate this problem, we’re keeping the balance set to output format 1 (default setting). As we’re about to see, this output format is not ideal for use with 232key.

In the output tab, we instruct 232key to press the enter key after typing the weight (clearly separating the captured values as they’re being typed):

232key output terminator set to Enter

If we now place an object on the balance, switch to an application like Notepad and press the print key, 232key types not just the weight, but 3 values, e.g.:

108.6
1
108.6

Weighing a second object leads to the following output:

83.0
2
191.6

A look at the event log in 232key reveals the problem with output format 1: The scale does not only send the weight over the interface, but also the number of accumulations and the total accumulated weight, each in a separate line. 232key captures and types all of these values (as it has no way of knowing which value you’re interested in). This is indicated by the characters in blue in the event log (note how they correspond to the output above):

232key event log showing unwanted values being captured from balance (No. and Total accumulated weight)

Capturing the right value

To make sure that 232key only captures the weight from the first line and nothing else, we have to find out what makes this line unique. One candidate would be the unit (“g”), but unfortunately it also appears on the third line:

GS        108.6 g
No.          01
Total.    108.6 g

However, only the first line starts with “GS” (which – according to the user manual – stands for “gross weight”). Therefore, we have to make sure that 232key only matches lines starting with “GS”. To do so, we go to the input tab and click on “Customize…”. This brings up a dialog which lets us modify the regular expression associated with this device:

Default regular Expression Adam HCB

The regular expression shown above captures the first (decimal) number it encounters. You don’t have to fully understand it, it’s sufficient to add “GS” in front of it to make sure 232key only matches lines where these characters appear before the desired value:Modified regular expression 1

When weighing the same two objects again, 232key now only captures the weight from the data sent by the balance (as highlighted in blue in the event log):

232key event log showing only the weight being captured

In many cases, it’s really that easy to prevent 232key from capturing too many values. All you have to do is find out what makes the line containing the desired value(s) unique and modify the regular expression accordingly. However, as discussed below, sometimes things can get a bit more complicated.

Potential pitfalls

White space and negative values

The space characters sent by the balance after “GS” are currently being matched by the “\s*” expression inside the parentheses. (“\s*” means “match a whitespace character between zero and unlimited times”):

GS([-+]?\s*[0-9]*[\.,]?[0-9]+)

If the balance were to send a negative value, the fact that “\s*” appears after “[-+]” in the regular expression could become a problem. This depends on the position of the minus sign in the data sent by the balance:

  • “GS       108.6 g” would be matched,
  • “GS       108.6 g” would not.

If you encounter this kind of problem, add another “\s*” right after the characters you’ve added before:

The regular expression above instructs 232key to look for “GS” and white space characters (between zero and unlimited times) ahead of the weight (and ahead of a potential minus or plus sign). Do we need it for the Adam Highland balance used in this example? We can’t say, because unfortunately it refused to send (“print”) any negative weights at all. For the following examples, we’ll be reverting to our previous regular expression.

Alternations and capture groups

There’s something else we did not consider: Remember that “GS” stands for gross weight? What happens if we tare a container, place an object inside and then press the print key? 232key does not type anything! A look at the event log reveals that the scale sent “NT” for net weight instead of “GS”. As this does not match our regular expression, 232key ignores the entire line:

Event log net weight NT

This can be fixed by using an alternation that matches either “GS” or “NT”:

Regular expression with alternation

However, testing this new expression shows that we’ve made things worse. Not only did 232key not type anything, the event log also contains several error messages:

event-log-4

We forgot to consider that 232key will always capture and process the data from the first capture group only. A capture group is marked by round parentheses and we’ve just inadvertently added a new one which captures either “GS” or “NT”. 232key then tried to convert these characters into a number, which obviously had to fail.

The solution is to mark it as a non-capturing group by adding “?:” as the first characters inside the parentheses. This means that the group is used for matching, but not for capturing data:

Regular expression with non-capturing group

Testing this expression shows that 232key now works as expected and also captures the net weight:

event-log-5

This is just one example of how you can use custom regular expressions in 232key Plus. If you want to learn more about regular expressions, this Quick Start guide is a great place to get started. If you need help creating a custom regular expression for your device, please post your question in our support forum.