Using a Serial Mouse
to Create a Position Encoder

As we discussed in class, the rotating ball of a mouse turns a set of two position encoders, one for the x-movement and one for the y-movement. Using the electronics of the mouse, these position encoders (along with the two button inputs) can be used for feedback on the position of motors or user-controlled knobs. All that needs to be done is to decipher the serial data that is sent to the PC. (Note: The data interface for the PS/2 mouse is very similar and the procedures discussed here can be used for that port too.)


To begin with, take a serial mouse and connect it to one of the free serial ports on the back of the computer. Make a note of which one you use because this information will be needed when we set up the interface to Linux.

Not all serial mice are created equal. For example, a mouse might have a third button and therefore have a slightly different protocol. We will be using a serial terminal program called minicom to access the data that comes from the mouse you are using at your PC to verify what its protocol is.

Open minicom by typing minicom from the command line. Once the program opens (usually after making some announcement about initializing the modem) follow the following procedures to set it up so that it can capture the mouse data.

At this point, if you move the mouse you should see a jumble of characters appear on the screen. The next step is to capture those characters to a file so we can determine the mouse's protocol.

Now we are ready to begin capturing data to analyze the mouse protocol from.

The data you viewed and captured from minicom was shown to you in ASCII or text format. The mouse, however, sent the data to you in binary format. Therefore, we need to use a binary editor to view the data in the captured file. In the lab, you should have access to a package I downloaded called shed. Shed, which stands for "simple hex editor", will allow us to open the captured file and view its contents in ASCII, hexadecimal, decimal, octal, and binary. (You can download your own copy from the URL The following instructions show how to install shed from the downloaded package.

At this point, you should know what the protocol for your mouse is. Now we can begin writing the code for the mouse. The remainder of the lab involves accessing the serial port, capturing the data from the mouse, and indicating the number of turns the encoders take. To access the serial port, we will use the same file-type I/O port interface that we covered when we discussed device drivers. The specifics for this method can be found in Michael Sweet's article, "Serial Programming Guide for POSIX Operating Systems".

When prompted either with a keypress from the user or a timed output, your program should be able to give the number of pulses in both the x-direction and the y-direction since the last output. Good luck!