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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 thoughts on “What to do when 232key captures too many values from your scale”