Decoding data from the MicroSoft Serial Mouse

Written by Paul Bourke
April 2003

The old MicroSoft serial mouse, while no longer in general use, can be employed to provide a low cost input device, for example, coupling the internal mechanism to other moving objects. The serial protocol for the mouse is 1200 baud, 7 bit, 1 stop bit, no parity. The pinout of the connector is standard serial as shown below.

1DCDData carried detect
2RDReceive data
3TDTransmit data
4DTRData terminal ready
5SGSignal ground
6DSRData set ready
7RTSRequest to send
8CTSClear to send

Every time the mouse changes state (moved or button pressed) a three byte "packet" is sent to the serial interface. For reasons known only to the engineers, the data is arranged as follows, most notably the two high order bits for the x and y coordinates share the first byte with the button status.

1st byte1LBRBY7Y6X7X6
2nd byte0X5X4X3X2X1X0
3rd byte0Y5Y4Y3Y2Y1Y0

Sample C code to decode three bytes from the mouse passed in "s", the button and position (x,y) are returned.

   s should consist of 3 bytes from the mouse
void DecodeMouse(unsigned char *s,int *button,int *x,int *y)
   *button = 'n'; /* No button - should only happen on an error */
   if ((s[0] & 0x20) != 0)
      *button = 'l';
   else if ((s[0] & 0x10) != 0)
      *button = 'r';
   *x = (s[0] & 0x03) * 64 + (s[1] & 0x3F);
   if (*x > 127)
      *x = *x - 256;
   *y = (s[0] & 0x0C) * 16 + (s[2] & 0x3F);
   if (*y > 127)
      *y = *y - 256;