LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Check for status of Serial ports in any language? (https://www.linuxquestions.org/questions/programming-9/check-for-status-of-serial-ports-in-any-language-86223/)

BongFish 08-27-2003 07:54 AM

Check for status of Serial ports in any language?
 
Is there any way to do something like the folowing in ANY language?

if 'pin 1 and pin 2' are connected:
do command
elif 'pin 1 and pin 3' are connected:
do other command

etc.


The reason is I have a box with 4 buttons connected to a serial port that I used to controll winamp when I was running XP before I made my remote. It would be usefull to have buttons that could flick through windows in ratpoison or something like that.

sk8guitar 08-27-2003 10:50 AM

http://www.tldp.org/HOWTO/Coffee-3.html

http://www.tldp.org/HOWTO/Coffee-2.html

those will probably give you some idea on this.

Hko 08-28-2003 08:30 AM

Reading/writing pins directly
 
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.

kev82 08-28-2003 08:56 AM

hko: how about using the parallel port, in basic output mode it has has 5 input pins(10-13, 15) and 8 output pins(2-9) which can be read/wriiten by two 8 bit registers(forgotten io address but you can get it from cat /proc/ioports) i doubt the parallel port driver lets you set/get these registers directly but if it doesnt you can write a kernel module and call inportb() and outportb() as necessary.

Hko 08-28-2003 09:11 AM

kev82: Thanks for thinking along with me. But I want to use the serial port because I want no external power supply and because people tend to have the serial port unused, while often having a printer attached to the parallel port.

It is impossible, or at least difficult and "dangerous", to drive LED's from the parallel port without an external power supply. The serial port can supply more current than the parallel port. Also, when you take too much current from the parallel port it easily breaks! This would leave me with a broken IO-card, or worse, a broken motherboard when the parallel port is integrated with the motherboard.

BongFish 08-28-2003 10:35 AM

No, there's no circutry, pressing the buttons simply shorts two of the pins. I'll post a circuit diagram in a moment, showing which buttons are connected to which pins.

I know it's possible because there's a winamp plugin called ComCtrl that works with the drvice I have. I may write to the author of it asking for the source.

_____________
S | | | |
E \ \ \ \
R __ | | | |
I _____ | | |
A ________ | |
L ___________ |

This is the circuit diagram, I don't have the names of the pins that they are connected to yet.


Anyway, thanks for a very comprehensive reply Hko!!

Hko 08-28-2003 12:07 PM

I found the circuit diagram by googling for "ComCtrl" at:
http://diba.hotbox.ru/comctrl/tutorial1/index.html
and at:
http://www.mattsclearpcs.com/com/com.html

I looked at it, and it's indeed possible to read 4 pins like this.
I figured out the names of the pin-names thanks to de doc I recommended.

It does not help me to switch more than 2 LED's however :-(
I'm still convinced that's not possible without additional circuitry and/or external power-supply.

By using parts of my code and change it, it's not too difficult to read the 4 pins independently in Linux.

If you need more help, just say so, and I'll make a basic program for this. I think I'm going to make the thing myself as well. (don't know when).

Thanks too you as well.

BongFish 08-28-2003 01:17 PM

I'm going to try and create the program myself as an exercise, I'm just starting to learn how to program and there's nothing better to aid learning than a good project.

However I would like it if you could PM me the code when you're done for comparison. I have a tendancy to do things very inneficiently at the moment as I am a very poor programer.

Thanks

Hko 08-28-2003 02:15 PM

OK.
("PM" means Private Message, I suppose?)

BongFish 08-28-2003 02:41 PM

Yep

Hko 09-10-2003 05:38 PM

Its' finished. I've made a program that handles the 4 buttons. It does not do anything useful (yet). It just prints what happens when the buttons are pressed and released.

You can get it here:

http://www.xs4all.nl/~heiky/

BongFish 09-11-2003 01:46 AM

Downloading source now, I'm off to college then I'll try it out later.

Cheers.


All times are GMT -5. The time now is 10:35 PM.