ITEM: D6052L

I am trying to send a char to a tty port.




Question:

I have a Black Box that is connected via. serial port to the RISC. And
it switches RISC when it receives an "A" or "B" signal. I have turned
the getty off and I can send the signal to the tty port "/dev/tty1".
We pluged a 3151 in and we do get a prompt, we then echo "B" >
/dev/tty1 and the B is sent to the screen. Then unpluging the 3151 and
plugging in the Black Box, we get can't creat specified device.  Then
pulged back in to the 3151 and all worked agian.

Response:

The Black Box device does not provide a DTR signal, as a result, AIX will
not allow you to open the port. There are two work-arounds for this. 
Black Box has provided you with a cable which maps RTS on the device side
to CD, CTS, and DSR on the AIX side. The device will raise RTS as long as it
is turned on, thus driving CD high on the RISC   System/6000 side.

Alternatively, you could set "clocal" on the port, telling the AIX to
ignore the CD signal. I FAXed you the following program to do this
(you will not be able to do this with SMIT unless you have a 128-port
adapter, where you can turn on "forcedcd". Setting "clocal" in the STTY
attributes in SMIT will only work if you are running a login prompt on
the port).

/*

        SETCLOCAL.C

        Create this file with the vi editor. Call the file "setclocal.c".
        Use the following command to compile the program

                \# make setclocal
        or
                \# cc -o setclocal setclocal.c

        Copy the "setclocal" file into a directory in your PATH like /usr/bin.
        You make want to invoke it from /etc/rc for the terminals that need
        to have clocal on them. Use the command as follows

                \# setclocal /dev/tty0

        To check that it worked, use the command

                \# stty -a \< /dev/tty0

        This will either hang (indicating the program did not work) or
        return the current tty attributes. If clocal is turned on, you
        should see the word "clocal" in the output. If it is turned off,
        you should see "-clocal" in the output.

*/

\#include \
\#include \
\#include \
\#include \

struct termios tty_attr_old;
int tty_fd;

error_exit(char *str)
{       fprintf(stderr, "%s", str);
        if (errno > 0)
                fprintf(stderr, " (%d)", errno);
        fprintf(stderr, "\\n");
        exit(1);
}
void main(int argc, char **argv)
{       struct termios tty_attr_new;
        int tty_fd;

        if (argc \< 1)
                error_exit("Usage: setclocal /dev/tty\#\\n");

        if (-1 == (tty_fd = open(argv[1], O_RDWR | O_NDELAY)))
                error_exit("Error opening device for attribute modification");
        if (-1 == tcgetattr(tty_fd, &tty_attr_old))
                error_exit("Error retrieving terminal attributes");

        tty_attr_new = tty_attr_old;
        tty_attr_new.c_cflag |= CLOCAL;

        if (-1 == tcsetattr(tty_fd, TCSANOW, &tty_attr_new))
                error_exit("Error setting terminal attributes");

        close(tty_fd);
}
Response:

I have a program that should do this with no problem.
The key is to open the port nonblocking, and then it
is simple to send out a single character.

/*                                                                     *
 * EXAMPLE PROGRAM: Open a port which has no CD and then write data    *
 * out of it.                                                          *
 * I have set it up for tty0, and it will send a single character 'A'  *
 * The idea is to switch a black/box terminal switch box               *
 *                                                                     */

\#include \
\#include \
\#include \
\#include \
\#define PORT "/dev/tty0"
extern errno;

main()
{        int port;               /* descriptor       */
        struct termios parms;   /* tty parms        */
        char c;                 /* Charater read    */
        int status;             /* read status      */
        char buf[8];
        int i;

        buf[0] = 'A';

        /* Open port non-blocking in case there is no carrier */
        /* This should allow me to write to THIN air - or the switch */
        if ( (port=open(PORT, O_RDWR|O_NONBLOCK)) \< 0 )
        {
                printf("ttyset: cannot open port %s\\n", PORT);
                perror("ttyset");
                exit(errno);
        }

        /*
         * Set the termios attributes (ie: stty -a).  Among these, the
         * tty is set to CLOCAL so we can open the port without carrier.
         */
        parms.c_iflag = BRKINT|IGNPAR|ICRNL|IXON|IXOFF;
        parms.c_lflag = ICANON;
        parms.c_cflag = CS8|CLOCAL|CREAD|B9600;         /* CLOCAL */
        parms.c_oflag = OPOST|ONLCR;
        parms.c_cc[VERASE] = CERASE;
        parms.c_cc[VKILL] = CKILL;
        parms.c_cc[VQUIT] = CQUIT;
        parms.c_cc[VINTR] = CINTR;
        parms.c_cc[VEOF] = 4;
        parms.c_cc[VEOL] = 0;
        parms.c_cc[VEOL2] = CNUL;
        parms.c_cc[VSTART] = '\\21';
        parms.c_cc[VSTOP] = '\\23';
        parms.c_cc[VSUSP] = '\\32';
        parms.c_cc[VDSUSP] = '\\31';
        parms.c_cc[VREPRINT] = '\\22';
        parms.c_cc[VDISCRD] = '\\17';
        parms.c_cc[VWERSE] = '\\27';
        parms.c_cc[VLNEXT] = '\\26';

        /* Configure the port with the new attributes */
        tcsetattr(port, TCSANOW, &parms);

        /* Write out the A */
        printf("Wrote %d bytes\\n",write(port,buf,1));

        /* Close and reopen without O_NDELAY */
        close(port);
}
This should send a single character out the port, enabled or disabled.


Response:

The code worked fine.  He asked about flushing the buffer of the tty.
I suggested he at the following code following the open statment:

        if (tcflush(port, TCIOFLUSH) \< 0)
                printf("Problem flushing\\n");

This should solve his initial problem.


Support Line: I am trying to send a char to a tty port. ITEM: D6052L
Dated: October 1993 Category: N/A
This HTML file was generated 99/06/24~13:30:56
Comments or suggestions? Contact us