June
2006, Issue 191
Measurement
System for Weight and Dimensions
Renesas
M16C Platform Design Contest 2005
HOST
SOFTWARE
Let’s
take a look at how the Weasure works in a typical shipping
application. I created a program that takes a package’s
measurements and a couple of additional parameters (parcel
origin and destination). It then looks up the prices
of various shipping options on UPS’s web site (see Photo
4). I wrote the application in Python language because
it has excellent tools for interacting with other Internet
services and processing Internet-based data. Because
applications in Python are portable, it should run on
almost any host supporting serial port access and a
web browser (Windows, Mac OS, Linux, etc.). Refer to
the “Python Programming” sidebar
(p. 18) for more information.
|

(Click
here to enlarge)
|
Photo
4—The Weasure form page is shown in the foreground.
The UPS Cost Estimate page is in the background.
The form is a template with the measurement values
filled in by the local server software. This data
is sent to the UPS web site to generate the cost
page. |
My
first thought when implementing the host application
was to use one of the available user interface libraries
for Python to open up a dialog box to collect the user
information. But because the result from the UPS site
comes back as a nicely formatted web page, I came up
with a much quicker solution. I used a local web page
form to collect the information because I knew I’d be
using a web browser to display the results anyway. Thus,
rather than writing code to format and display dialog
boxes, I simply used my favorite web page editor to
create a web form page. The host application then became
a simple web server, collecting parameters and displaying
results via my web browser.
When
the application runs, it launches the web browser and
displays a form like the one shown in the foreground
of Photo 4. The fields in the Package Size table are
read via the Weasure’s serial line. When you click the
Update Size and Weight button, the web server rereads
the measurement data and updates the page. If you fill
out the Package Origin and Package Destination fields
and then click on the Get Rate button, the program will
send the data to the UPS web site and return a web page
like the one shown in the background.
Most
of the application’s data is kept in a Python dictionary
structure called UPSPostData. This represents the data
posted to the UPS web site, with fields named as required
by the UPS Calculate Cost and Time form. Some of these
fields are constant (e.g., currency and measurement
units) and some you must supply (e.g., package origin
and destination). The Weasure supplies the rest of the
fields.
The
program uses an HTML template file for the web form.
The ReplaceHTMLTemplateValues routine replaces fields
delimited with anchor tags with the values in the UPSPostData
dictionary read from the serial port by the UpdateMeasurements
routine. (A serial port library isn’t part of the normal
Python language distribution, but an open source solution
is available at http://sourceforge.net/projects/pyserial/.)
When
the application starts, it launches the web browser
to a URL on your PC’s local host TCP address (127.0.0.1:8000).
It then starts a web server listening in on that port
that’s implemented in the LocalHTTPServer routine. Within
that routine, the do_GET subroutine handles the browser’s
initial request to display the updated template page.
The do_POST method handles the requests coded into the
HTML template for either updating the measurements or
fetching the UPS data when you click the form’s buttons.
The FetchUPSRatePage routine sends the request to the
UPS web site and takes the response from there and routes
it through the local server to your browser.
The
complete source code is available on the Circuit Cellar
FTP site. Using the extensive Internet tool kit in Python’s
library and getting your web browser to handle the user
interface, the host application is extremely compact—only
160 lines of code!