I want to know which device is attached at the serial port.

Now, Suppose I attach a serial mouse at COM Port 1.

I want when I attach serial mouse I can detect that serial mouse is attached at serial port and

when I just remove serial mouse from the serial port I can detect that mouse is not attached at the serial port.

Now the procedure is that if I connect Mouse at the Serial Port it just send character 'M' after 14 ms for 2 - Button Serial Mouse and after another 63 ms it will send '3' for 3 - Button Serial Mouse.

To read the character send by the mouse I have to use the read function.

But this function I have to use only when mouse send some data to the serial port.

If no data is send from the mouse and then also if I try to read it then the program is just hang from that moment.

So with the help of select function and read funtion I can detect the serial mouse at the serial port.

My problem is that How data is send from the mouse that I do not know.

So If I know the exact way if data is coming from the mouse or not then my problem is over.

Let us view a simple program :

/************************************************** **************************/


#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>


int main()
int fd; /* Input Sources */
fd_set readfs; /* File Descriptor Set */
int loop=0; /* Loop While TRUE */
int res;
struct timeval Timeout;
char *device = "/dev/ttyS0"; /* COM1 */

/* Opens a Device, Sets The Port Correctly, and Returns a File
Descriptor */

fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK); /* OPEN DEVICE AT PORT COM1 */
if (fd == -1)

/* Loop For Input */

while (!loop)
FD_SET(fd, &readfs); /* Set Testing For Source */

/* Set Timeout Value Within Input Loop */

Timeout.tv_sec = 0; /* Seconds */
Timeout.tv_usec = 250000; /* Microseconds */
res = select(fd+1, &readfs, NULL, NULL, &Timeout);
if (res > 0)
printf("\nNow you are ready");
loop = 1;
printf("\nTill You are not ready");
return 0;

/************************************************** ****************************/

So if data is available from the mouse then the message is that Now you are ready and if there is no data then Till You are not ready message is continously on the screen at the given time interval.

So what should I have to do to come out the loop when data is just received by the select fucntion or not to come out the loop when there is no mouse and then data is not received by the select function.

Please do the needful.

Any good suggestion will be appreciated.

Thanks in advance.