loopback test on serial port. Default termios config - Unix

This is a discussion on loopback test on serial port. Default termios config - Unix ; I am writing a code to identify serial ports with a loopback device. I fork 2 processes, one to write "Hello\n" and the other one to read and compare with "Hello". If everything completes properly within 1 second (alarm) then ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: loopback test on serial port. Default termios config

  1. loopback test on serial port. Default termios config

    I am writing a code to identify serial ports with a loopback device. I
    fork 2 processes, one to write "Hello\n" and the other one to read and
    compare with "Hello". If everything completes properly within 1 second
    (alarm) then it's a loopback device. Otherwise it's not. I use it to
    associate port numbers with cables.

    The problem is how to configure the serial line properly. With default
    parameters it works fine. However, if another program modified it
    (e.g. the programs I use to read from serial sensors) without
    restoring the defaults, it doesn't work any more.
    So how to restore the default settings? Or what are the settings I
    need for this test?

    Here is how I currently do it. It seems to work ok in most cases but
    I'd like to be sure. I removed all error checking to make the code
    more readable.

    int fd = -1;
    int oldflags;
    struct termios tty;

    fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
    oldflags = fcntl(fd, F_GETFL, 0);
    fcntl(fd, F_SETFL, oldflags & ~O_NONBLOCK);
    tcflush(fd, TCIOFLUSH);

    memset(&tty,0,sizeof(struct termios));
    tty.c_iflag = ICRNL | IXON;
    tty.c_cflag = CS8 | CREAD | HUPCL | CLOCAL;
    tty.c_lflag = ICANON | OPOST | ISIG | IEXTEN;
    tty.c_lflag |= ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE;
    tty.c_oflag = OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 0;
    tty.c_line = N_TTY;

    cfsetospeed(&tty, B9600);
    cfsetispeed(&tty, B9600);

    tcsetattr(fd, TCSADRAIN, &tty);


    I got all these setting by looking at "stty -F /dev/ttyS0 -a" just
    after booting.




  2. Re: loopback test on serial port. Default termios config

    Hi, I'm wondering if you've had any success with fixing your problem.
    I'm trying to setup a simple loopback test on Redhat and it says
    writes to the ttyS0 port are fine, but it never reads back from it.
    I've tried your code, my code, and other simple code that I found
    online .... none of them have been able to read. I know it's writing
    for sure because I hooked it up to an oscilloscope. Should I try a
    reboot and changing settings? That's all I can think of because I
    don't know what's wrong.

    Any help would be greatly appreciated
    Thanks

  3. Re: loopback test on serial port. Default termios config

    >Hi, I'm wondering if you've had any success with fixing your problem.
    >I'm trying to setup a simple loopback test on Redhat and it says
    >writes to the ttyS0 port are fine, but it never reads back from it.


    Are you attaching anything to the serial connector during this test?
    What is the wiring of the loopback connector?

    >I've tried your code, my code, and other simple code that I found
    >online .... none of them have been able to read. I know it's writing
    >for sure because I hooked it up to an oscilloscope. Should I try a
    >reboot and changing settings? That's all I can think of because I
    >don't know what's wrong.


  4. Re: loopback test on serial port. Default termios config

    Yes, I have pins 2 and 3 shorted.

    Do I need to turn off all the echo commands? How would I even do that
    in a unix terminal?

  5. Re: loopback test on serial port. Default termios config

    Matt Camack writes:
    >Yes, I have pins 2 and 3 shorted.


    How about the modem control lines (dsr/dtr/cts/rts/cd?)

    scott

  6. Re: loopback test on serial port. Default termios config

    On Jun 20, 1:51*pm, sc...@slp53.sl.home (Scott Lurndal) wrote:
    > Matt Camack writes:
    > >Yes, I have pins 2 and 3 shorted.

    >
    > How about the modem control lines (dsr/dtr/cts/rts/cd?)
    >
    > scott


    Hey thanks for the responses, but I guess I found the problem. The
    serial port must've been faulty on one machine or somethin .... tried
    it on another linux box and it works like it should. The code wasn't
    too hard so, neither was the wiring, so I figured it had to be
    something out of my control at the time.

    Thanks anyways!

  7. Post Re: loopback test on serial port. Default termios config

    I am also working on doing loop-back test for reading and writing from serial port. I am able to write but not able to read. I have sorted 2nd and 3rd pin of serial cross-cable for loop-back. Please suggest what changes i should do in my code.

    My code is as here
    #include // standard input / output functions
    #include // string function definitions
    #include // UNIX standard function definitions
    #include // File control definitions
    #include // Error number definitions
    #include // POSIX terminal control definitionss
    #include // time calls
    #include


    int open_port(void)
    {
    int fd; // file description for the serial port

    fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);

    if(fd == -1) // if open is unsucessful
    {
    perror("open_port: Unable to open /dev/ttyS0 - ");
    }
    else
    {
    fcntl(fd, F_SETFL, 0);
    }
    printf("%d",fd);
    return(fd);
    }
    int configure_port(int fd) // configure the port
    {
    struct termios port_settings; // structure to store the port settings in

    cfsetispeed(&port_settings, B9600); // set baud rates
    cfsetospeed(&port_settings, B9600);
    port_settings.c_cflag |= ( CLOCAL | CREAD );

    port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
    port_settings.c_cflag &= ~CSTOPB;
    port_settings.c_cflag &= ~CSIZE;
    port_settings.c_cflag |= CS8;
    tcflush( fd, TCIOFLUSH );
    tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
    return(fd);

    }
    int main()
    {
    int fd= open_port();
    int d=configure_port(fd);
    printf("%d",d);
    int bytes;
    char mk[10];
    scanf("%s",&mk);
    int w=write(fd,mk,strlen(mk));

    int y=ioctl(fd,FIONREAD,&bytes);

    printf("%d\n",w);
    perror("write");
    printf("%d",y);
    char buffer[80];
    char *data;
    int nbytes;

    data=buffer;
    nbytes=read(fd,data,5);
    printf("the outputis \n%d\n\n",nbytes);
    perror("read");
    while(nbytes > 0)
    {printf("datmukun %d\n\n",nbytes);
    data+=nbytes;
    if (data[-1]=='\n'||data[-1]=='\r')
    break;
    }
    return 0;
    }

+ Reply to Thread