From: tirza@duteca.et.tudelft.nl (Tirza van Rijn)
Subject: Serial communication over leased line in C (UNIX)?
Organization: Delft University of Technology, Dept. of Electrical Engineering
Date: Wed, 20 Jan 1993 23:42:20 GMT


Hallo *,


I'm trying to write a little program to send database requests down a leased
(direct) line and save the answers to a file, but I can't get it to work.

My side of the line is the second RS232 port (ttyd2) of a Silicon Graphics
Iris Indigo running IRIX 4.0.1, the other side is an always stand-by database
server, the communication should be at 4800 baud, 8 bits/char, no parity,
1 stopbit, and there is no login procedure involved: you can just send a
request anytime and get back a reply. I can successfully contact the database
server with Kermit (set line ttyd2, set baud 4800) and cu (-s4800 -lttyd2).

A normal session with the database server looks something like:
question:                                     answer:
TI[LF]                                    =>  ? 0 Cmd error [LF]
                                              (1th request usually fails)
TI[LF]                                    =>  ! 18-JAN-93 12:03:46 [LF]
DB GSS6,HE1,+24:00,00:00,28-DEC-92[LF]    =>  ! #79 #80 #82 #82....... [LF]

My program looks as follows:

-8<--------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>

#define CR      '\r'
#define LF      '\n'

main()
{
    FILE *in, *out, *output;
    char c;
    char linebuf[500];
    char answer[500];
    char request1[] = "TI";
    char request2[] = "DB GSS6,HE1,+24:00,00:00,28-DEC-92";
    int i;

    system("sleep 10000000 < /dev/ttyd2 &");
    system("stty 4800 < /dev/ttyd2");
    in = fopen("/dev/ttyd2","r");
    out = fopen("/dev/ttyd2","w+");
    output = fopen("outfile", "w");

/* Ask MNZ for time */
    fprintf(stderr, "request1 = *%s*\n", request1);
    fprintf(out, "%s\n", request1);
    i = 0;
    while ((c = fgetc(in)) != LF)
    {
        fprintf(stderr, "c = %c\n", c);
        answer[i++] = c;
    }
    answer[i] = '\0';
    fprintf(stderr, "answer = *%s*\n", answer);
    fprintf(output, "%s\n", answer);

/* Ask MNZ for time again */
    fprintf(stderr, "request1 = *%s*\n", request1);
    fprintf(out, "%s\n", request1);
    i = 0;
    while ((c = fgetc(in)) != LF)
    {
        fprintf(stderr, "c = %c\n", c);
        answer[i++] = c;
    }
    answer[i] = '\0';
    fprintf(stderr, "answer = *%s*\n", answer);
    fprintf(output, "%s\n", answer);

/* Ask MNZ for spectal wave height band 200-500mHz from Marex on MPN */
    fprintf(stderr, "request2 = *%s*\n", request2);
    fprintf(out, "%s\n", request2);
    i = 0;
    while ((c = fgetc(in)) != LF)
    {
        fprintf(stderr, "c = %c\n", c);
        answer[i++] = c;
    }
    answer[i] = '\0';
    fprintf(stderr, "answer = *%s*\n", answer);
    fprintf(output, "%s\n", answer);

    fclose(output);
}

-end------------------------------------------------------------------------

It looks like I'm receiving the answers from the database server correctly,
but that is always a '? 0 Cmd error '. But if I run my program while at the same
time I am giving requests through 'cu -s4800 -lttyd2', it does receive about
half of the "sensible" answers (such as  '! 18-JAN-93 12:03:46 '), the other
half goes to cu.

I've got the feeling that my requests are not gettting through at all so that
I never get a "sensible" answer back.

Does any communications guru out there know what I'm doing wrong? Or can
sombody tell me where to find an example program for this kind of problem?

 ______
   |_          Thanks in advance for your consideration,
   |_)
   |v\                         Tirza

--------------------------------------------------------------------------------
Tirza van Rijn                       | e-mail: tirza@duteca.et.tudelft.nl
Department of Electrical Engineering |
Delft University of Technology       | Thou art beautiful, Oh my love, as Tirzah
Delft, The Netherlands               |                       Song of Salomon 6:4
--------------------------------------------------------------------------------
Newsgroups: comp.unix.programmer