Introduction
Protocol
C Program
An Application

Introduction

Picture of the keypad The Qtronix Scorpius 22 keypad has the keys found in the "numeric" part of a standard keyboard. It has an RS-232 interface with a 9-pin connector. This note describes the keypad, its key codes, and simple application using the keypad.

The keypad is available from Jameco for $39.95 in their catalog #993. The unit evaluated was purchased from a local store (T-Zone) for 22 dollars.

Auxiliary keypads are also available which plug in-line with a normal keyboard using a PS2 style connector. These units are a better choice if you really want a numeric keyboard for your console or X-window applications. Currently serial devices can not be conveniently used as auxiliary keyboards under Linux

A PIC 17C54 is at the heart of the keypad. The modem control lines on the serial port supply power for the 17C54 and the green NumLock LED. The photo below is of the circuitry in the keypad.

Picture of the keypad circuitry


Protocol

It looks like the keypad uses RTS and DTR to power the serial port. DTR needs to be set to +12 volts and RTS must be set to -12 volts. The port should be configured for 8 data bits, no parity, and one stop bit (8,N,1) at 9600 baud. The unit sends a single byte for each keystroke. The keypad has three modifier keys: Shift, Fn, and NumLock. NumLock is modal with a green LED to indicate state. Fn and Shift are chord keys like the shift key on most keyboards. The table below shows the key codes for all keys for each of the modifiers.
 
Keypad Key Codes (in hex)
Key FnNumShift
*838383ac
+93939393
-92929292
.9f9f9090
/95959595
09eaa8e8e
19aa68a8a
296a08484
39ba78b8b
497a18585
5b2a48888
698a28686
79ca88c8c
899a38787
99da98d8d
BkSp89a58989
Enter94949494
Esc91919191
Tab8fab8fad

 

C Program

The following C program opens the port, sets the modem control lines and reads a single keystroke from the keypad. The output is a two character hex number which represents the keycode.

/*  keypad : reads a single key code from a Qtronix serial keypad */
/*  Usage:  keypad <serial port>                                  */
/*     eg:  keypad /dev/ttyS0                                     */

#include <stdio.h>
#include <termio.h>
#include <sys/fcntl.h>

main (int argc, char *argv[])
{
    struct  termios    tbuf;
    int                fd_dev;
    int                sm;  /* state of the modem lines */
    unsigned char      buf;

    if (( fd_dev = open(argv[1], (O_RDWR | O_NDELAY), 0)) < 0 ) {
        printf("Unable to open tty port specified\n");
        exit(1);
    }

    tbuf.c_cflag = CS8|CREAD|B9600|CLOCAL;
    tbuf.c_iflag = IGNBRK;
    tbuf.c_oflag = 0;
    tbuf.c_lflag = 0;
    tbuf.c_cc[VMIN] = 1; /* character-by-character input */
    tbuf.c_cc[VTIME]= 0; /* no delay waiting for characters */
    if (tcsetattr(fd_dev, TCSANOW, &tbuf) < 0) {
        printf("%s: Unable to set device '%s' parameters\n",argv[0], argv[1]);
        exit(1);
    }
    if (( fd_dev = open(argv[1], (O_RDWR), 0)) < 0 ) {
        printf("Unable to open tty port specified\n");
        exit(1);
    }

    sm = TIOCM_DTR;
    if(ioctl(fd_dev, TIOCMBIS, &sm) == -1) {
        printf("Unable to set modem control lines\n");
        exit(1);
    }

    read(fd_dev, &buf, 1);
    printf("%02x\n", buf);
    exit(0);
}

 

An Application

This is a description of the author's use of the Qtronix serial keypad.

One of my PCs boots from a SanDisk flash disk into OpenDOS and then into loadlin and Linux. The root file system is on a RAM disk. There are no fans on the CPU or power supply so the computer has no moving parts. This machine is meant to be less a PC and more an appliance. It has a modem, printer, Palm Pilot cradle, and a text-to-speech board, as well as the CD-writer and other I/O devices. It does not have a monitor or keyboard.

The serial keypad is used to set-up ('7') and tear-down ('8') a PPP link to my ISP. If "NumLock" is on, the command about to be executed is spoken by the text-to-speech board. The '4' key is used to speak the time.

The keypad.sh script file
#!/bin/bash
# A script to read a Qtronix serial keypad and
# execute commands based on the key pressed.
# 'NumLock' usually implies a spoken response

while  true
do
    # Sleep waiting for a keypress
    KEY=`./keypad /dev/ttyS4`

    case $KEY in
      85 | 97 )    # Either '4' or 'NumLock 4' 
        speak `date +%k%t%M`
        ;;
      88)    # Both '5' and 'NumLock 5' 
        speak `/usr/local/bin/temperature` degrees
        ;;
      9c)    # '7' key
        /usr/sbin/dip .aim
        ;;
      8c)    # 'NumLock 7' key
        speak "Setting up P P P link"
        speak `/usr/sbin/dip .aim`
        ;;
      99)    # '8' key
        /usr/sbin/dip -k
        ;;
      87)    # 'NumLock 8' key
        speak "Tearing down P P P link"
        speak `/usr/sbin/dip -k`
        ;;
      *)
        echo $KEY is not implemented
        ;;
    esac
done