Hi all,
I need to write some code that will read input
from a serial port that has a mouse attached to it.
First try is on linux but its got to be ported to windows
next. The linux attempt is a proof of concept.
Before you write the email saying : Dude,
you can just use the windows or os calls to get
to the mouse...no, no i cant.
The situation is this: I need two mice on a single system.
but only one mouse will control the mouse cursor, i just want
to read the input off the other. The mouse cursor will be ps/2,
the other will be plugged into a serial port..hence I need to read
from a serial port.
ACK! but it doesnt work. Im following an example online but im not
having any luck. I can open the port, i can configure the port..i think,
but when i goto read the port..it fails with "Input/output error"
Does anyone know howto do this?
Many Thanks in advance!
-Marwan
here is the code...its brief and should compile with no issues:
///// code starts here /////
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <term.h> /* POSIX terminal control definitions */
/*
* 'OpenPort()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int OpenPort(char *) ;
void ConfigurePort(int port) ;
void ReadPort(int port) ;
void ClosePort(int port) ;
//////////////////////////////////////////////////////////
int
main(int argc, char **argv) {
int port = 0;
if( argc == 1)
port = OpenPort(NULL);
else
port = OpenPort(argv[1]);
if(port) {
ConfigurePort(port) ;
ReadPort(port);
ClosePort(port);
}
}
//////////////////////////////////////////////////////////
void ConfigurePort(int port) {
struct termios options ;
tcgetattr(port, &options);
printf(" c_cflag: %d\n",options);
// set the baud rate
cfsetispeed(&options, B1200);
cfsetospeed(&options, B1200);
//Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
// set character size
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
// set parity: Even parity (7E1):
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
// set hw flow control
options.c_cflag |= CRTSCTS ; //CNEW_RTSCTS; /* Also called
CRTSCTS */
// set canonical input
options.c_lflag |= (ICANON | ECHO | ECHOE);
// set input parity options
options.c_iflag |= (INPCK | ISTRIP);
// set software flow control
options.c_iflag |= (IXON | IXOFF | IXANY);
//Set the new options for the port...
tcsetattr(port, TCSANOW, &options);
}
//////////////////////////////////////////////////////////
void ReadPort(int port) {
int done=0;
char buffer[80];
int readAmount =0;
while( !done ) {
memset(buffer, 0, 80);
readAmount = read( port, buffer, 10);
if( readAmount != -1 ) {
printf("Got data %d bytes:|%s|\n",readAmount,buffer);
readAmount = 0;
}
else {
if ( readAmount == -1 ) {
perror("Got error:");
return;
}//endif
else {
printf("EOF\n");
return;
}
}//endelse
}//endwhile
}
//////////////////////////////////////////////////////////
void ClosePort(int port) {
close(port);
}
//////////////////////////////////////////////////////////
int OpenPort(char *device) {
int fd; /* File descriptor for the port */
char portname[80];
char error[80];
if( device == NULL)
strcpy(portname,"/dev/ttyS0");
else
strcpy(portname,device);
fd = open( portname , O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
/*
* Could not open the port.
*/
sprintf(error,"open_port: Unable to open %s -", portname);
perror(error);
fd =0;
}
else {
int bytes =0;
printf("Opend the port:|%s|\n",portname);
fcntl(fd, F_SETFL, 0);
}
return (fd);
}
-
To unsubscribe from this list: send the line "unsubscribe linux-serial" in
the body of a message to [EMAIL PROTECTED]