Serial Port Adapter

My serial port adapter is based on the Picaxe® 20X2 from Revolution Education. The Picaxe is a Microchip PIC-based microcontroller that is programmed in BASIC over a serial connection using a free IDE environment. The 20X2 is overkill for this application in some respects – I’m only using a quarter of the available program memory, but it does offer a bi-directional 8-bit I/O port as well as sufficient other input and output lines to control the Membership Card and communicate with the PC.

I considered using other interfaces, including other Picaxe chips, but the 20X2 gives me a flexible one-chip solution to connect to the Membership Card. All that is needed is a 20X2, a handful of support components, cables to connect to the PC and the Membership Card, and it’s ready to go. I’ve tested the adapter with both a standard 9-pin serial port and a USB-to-serial converter with no problems.

The 20X2 is available from several vendors. You can use your favorite search engine to find a source. I suggest Bing, but I work for Microsoft and so I would suggest it.

Picaxe BASIC Program

The software for the 20X2 is a simple state machine that listens for commands and data from the PC on the serial port and then sets the control lines on the Membership Card according to the commands and the data bus on the Membership Card via the 25-pin interface.

Due to the way the Picaxe firmware works, all commands sent to the adapter must be a two-byte string. After the 20X2 has processed the command, it responds back to the PC over the serial connection. Each response is followed by a CR/LF pair so that the PC can read each response as a line from the serial port.

The commands that the 20X2 listens to are listed in the following table. All commands and data sent to the adapter must be in upper-case only and sent without a following CRLF pair, and any other command sent to the 20X2 is ignored.

Command Response Meaning
RE RESET Put the Membership Card in RESET mode.
LD LOADING Put the Membership Card in LOAD mode.
GO RUNNING Put the Membership Card in RUN mode.
MP MP Set Memory Protect to read-only.
MW MW Set Memory Protect to write-only.
ID ID Input down.
IU IU Input up.

All strings sent to the 20X2 must be two bytes long – when a Picaxe chip executes a receive serial data command it will block on the command until it receives the exact number of bytes specified in the command. To ensure that the 20X2 never blocks waiting for serial data, and that it does not get out of sync with the controlling program, all commands and data sent to the adapter are two-byte strings. The Picaxe is not a particularly fast processor, I’ve found that I need to insert a 125ms delay after each character to enable the 20X2 to keep up with a steady flow of data from the PC.

When the Membership Card is in LOAD or RUN mode the adapter will accept and send bytes formatted as hex strings to the Membership Card. The adapter converts the hex string to a byte value and then sets the data lines to the specified byte. Giving the responsibility to the adapter to convert hex strings to byte values enables text-mode serial programs to control the Membership Card. For example, the following string could be sent to the Membership Card from a terminal program to load a simple program into the Membership Card and then run it:

RELDMW7A3F007B3F01REGO

Note that the program data (7A3F007B3F01) is sent as two-byte hex strings without separating them with a press and release of the input button. In LOAD mode the adapter will strobe the INP line each time a byte is sent to the adapter, making it easier to send data to the Membership Card from terminal programs. Once the application is running, the Q LED should turn on every time you send the string “ID” to the adapter, and should turn off when you send “IU”.

A heavily commented version of the BASIC source code for the 20X2 is included in the installation archive for the Membership Card Control Panel. A PDF file containing the schematic is also included in the installation archive.

Log in