The Yoctopuce team recently decided to record the weight of each package to be sent, in order to compare the pre-computed weight to the actual weight. And as we don't really like repetitive tasks, we decided to do it automatically. So we bought a few scales with an RS232 output. And this week we take the opportunity to show you in detail how to transform a Kern scale into a Yocto-Scale in a jiffy.
The basic idea is dead simple: we connect a Yocto-RS232 on the scale RS232 output and we configure the module so that it automatically analyzes the data that the scale outputs and presents them as a Yoctopuce sensor would do. Connecting the Yocto-RS232 takes only a second, the difficulty is located in the configuration...
We'd like to transform this scale into a USB scale
How the scale works
The scale serial protocol is very simple: if you send the w character, the scale answers on a single line:
- A space character or the M character
- The sign
- The weight in grams over 10 characters, right justified with spaces
- The unit, as it happens g for grams
- A carriage return
w 797 g w 797 g w 797 g
The Yocto-RS232 configuration
You can naturally configure the Yocto-RS232 by software, but we are going to use the VirtualHub. The first step to be performed is to configure the protocol and the speed to Line ASCII Protocol, 9600, and 8N1, which are the default values.
Configuring the protocol and the speed
Then we must create a job containing a single task that takes care of:
- Sending the w command every 500ms
- Reading the answer, interpreting it as an integer, and mapping it to genericSensor1.
To read the answer, we use a regular expression: a dot to ignore the first character, then "($1:INT)" to read an integer and to map it to genericSensor1, and finally we ignore the remaining characters until the end of the line with ".*" In the end, this requires only two commands:
writeLine w expect .($1:INT).*
Creating the task to read the values
Finally, we must run the job that we have just created, configure the Yocto-RS232 for the job to automatically start when the module starts up, and save it all.
Making sure that the reading job starts automatically
Optionally, we can assign a logical name to genericSensor1, for example "scale", and configure its unit to "g" for grams. Finally, we can easily check that everything works as expected by displaying the function list in the VirtualHub main interface.
From then on, the genericSensor1 function of the Yocto-RS232 behaves exactly like a Yoctopuce sensor. Here is a Python piece of code which displays in a loop the scale value:
from yocto_api import *
from yocto_genericsensor import *
if YAPI.RegisterHub("usb", errmsg)!= YAPI.SUCCESS:
print("%.0f%s" % (scale.get_currentValue(), scale.get_unit()))
In less than five minutes, we transformed a scale with an RS232 output into a Yoctopuce USB sensor working without driver. We could even afford to add a YoctoHub-Ethernet or a YoctoHub-Wireless to obtain a scale with a network connection. It makes you wonder if there is not some magic in Yoctopuce products :-)
Today, we built a USB scale working without drivers