LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Hardware (https://www.linuxquestions.org/questions/linux-hardware-18/)
-   -   Serial Mouse is attached or not attached at seiral port (https://www.linuxquestions.org/questions/linux-hardware-18/serial-mouse-is-attached-or-not-attached-at-seiral-port-523211/)

lovelylinux 01-27-2007 05:38 AM

Serial Mouse is attached or not attached at seiral 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 FILES
===============
#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)
exit(0);

/* Loop For Input */

while (!loop)
{
FD_ZERO(&readfs);
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;
break;
}
else
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.

DragonSlayer48DX 01-28-2007 07:21 PM

No offense intended, but it sounds like you're trying to re-invent the wheel. And IMHO, it probably just won't work.

First, the serial mouse does not send anything. It is read by the device driver.

Secondly, it is not a PnP device; it is only detected at system startup. IOW you can unplug and replug the serial mouse at will and BIOS nor the OS will recognize that.

Lastly, if you could write a program that persistantly monitored the serial port for device connection/disconnection, it would be painfully slow, as it would need to constantly be jumping back into the detection loop.


All times are GMT -5. The time now is 10:07 AM.