Is there some electronic circuit on you 4-button-device that makes it
talk the RS232 protocol?
If not, then I suppose you are trying to read the status of 4 pins on
the serialport directly. As far as I know there are only 2 pins on
the serial port you can write to directly (RTS & DTR), and only 2
other pins you can read directly (DSR & CTS). For reading, there may
be 3; If the DCD-pin also works. Not sure about this. So reading 4 may
not be possible without RS232 circuitry on your 4-button-device.
I recently got into this because I wanted to do the opposite (writing
directly to some pins on the serial post): I wanted to switch 3 LED's
independently through software without an external power supply. USB
is obviously the best way to do this, but it involves too much
electronics for my purpose; I was looking for a solution as cheap as
possible. (If somebody has some info about how to do this with USB, I
would be very interested though, especially a electronic diagram...)
Anyways, I managed to switch on only 2 LED's through the serial port
using the experimental program below. It needs only little changes to
read the DSR and CTS lines.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
char *program_name;
int fd;
int openserial(char *device)
{
if ((fd = open(device, O_WRONLY)) == -1) {
perror(program_name);
return 0;
}
return 1;
}
int closeserial(void)
{
if (close(fd) == -1) {
perror(program_name);
return 0;
}
return 1;
}
int RTS(unsigned short level)
{
int status;
if (ioctl(fd, TIOCMGET, &status) == -1) {
perror(program_name);
return 0;
}
if (level) {
status |= TIOCM_RTS;
} else {
status &= ~TIOCM_RTS
}
if (ioctl(fd, TIOCMSET, &status) == -1) {
perror(program_name);
return 0;
}
return 1;
}
int DTR(unsigned short level)
{
int status;
if (ioctl(fd, TIOCMGET, &status) == -1) {
perror(program_name);
return 0;
}
if (level) {
status |= TIOCM_DTR;
} else {
status &= ~TIOCM_DTR;
}
if (ioctl(fd, TIOCMSET, &status) == -1) {
perror(program_name);
return 0;
}
return 1;
}
int main(int argc, char **argv)
{
char *dev = "/dev/ttyS0";
program_name = argv[0];
if (!openserial(dev)) {
fprintf(stderr, "%s: Could not open device file %s\n", program_name, dev);
return 1;
}
RTS(0);
DTR(0);
sleep(1);
RTS(1);
sleep(1);
DTR(1);
if (!closeserial()) {
fprintf(stderr, "%s: Error close device file %s", program_name, dev);
return 1;
}
return 0;
}
Some documents you may want to read:
man termios
IO-port programming mini-HOWTO
Serial programming HOWTO
man ioctl
man ioctl_list
http://www.easysw.com/~mike/serial/serial.html (recommended)
If you manage to read (or better for me: write) to/from more than 2
pins directly, I would appreciate it very much if you tell me by
posting here or by e-mail: heiky_at_freeshell.org
TIA and good luck.