Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Hardware
User Name
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?


  Search this Thread
Old 01-18-2007, 09:45 AM   #1
LQ Newbie
Registered: Dec 2006
Posts: 17

Rep: Reputation: 0
Thumbs up How can I say mouse is attached or not attached at the serial port ?

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.
Old 02-07-2007, 10:44 AM   #2
Registered: Aug 2002
Posts: 19,415

Rep: Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103
Look at the serial programming howto guide.
You can poll a signal handler to see if there are any incomming characters without waiting.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
pl2303 attached to ttyUSB0 but driver attached to device is "serial" sheeluh Linux - Software 4 03-12-2007 05:27 AM
Serial attached SCSI support? krish240574 Linux - Hardware 1 03-20-2005 11:29 PM
Promise PDC20376 wont see the disk attached to the P-ATA port! NickLess Debian 4 09-13-2004 03:42 PM
linux locks up when usb mouse attached to usb port during startup. BGuy891653 Linux - Hardware 0 02-08-2004 06:26 PM
Sending commands and viewing replies from a serial-attached appliance bgianardo Programming 3 01-19-2004 12:00 PM > Forums > Linux Forums > Linux - Hardware

All times are GMT -5. The time now is 04:20 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration