Sunday, April 7, 2013

Null Modem Receiver

If there is a sender, the we need a receiver. On the other end of the null modem cable, we have a linux box that receive what is sent by the sender. The source code for the receiver is presented below :

#include "termios.h" /*this file contains declaration of required functions and types*/
#include "stdio.h" /*you know what this is*/
#include "fcntl.h" /*this file constains declaration for O_RDWR and O_NOCTTY)*/

int main()
    {
    int fd; /*variable of type integer used as descriptor to a serial port*/
    char c;
    struct termios termiosA; /*declaring a variable of type termios*/
    /* Turn on the null modem */
    fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY); /* I have done : sudo chmod 777 /dev/ttyUSB0 */

    /* We don't care about incoming bytes with parity on data input */
    termiosA.c_iflag = IGNPAR;

    /*
    Raw output.
    */
    termiosA.c_oflag = CR0; /* output modes */

    /*
    we are going to set a connection with serial port to the sender (this computer is a receiver)
    1. Baudrate is 4800 BPS (B4800)
    2. Enable RTS/CTS (hardware) flow control (CRTSCTS)
    3. Ignore modem control lines (CLOCAL)
    4. Character size mask is 8 (CS8)
    */

    termiosA.c_cflag = B4800 | CRTSCTS | CLOCAL | CS8; /* control flags */

    /* The change to the line's state takes effect straight away*/
    /* flush the modem line and initiate the modem */
    tcflush(fd, TCIFLUSH);
    tcsetattr(fd,TCSANOW,&termiosA);
    /* input is handled by the loop below */

    for(;;)
        {
        read(fd,&c,1); /* reads the null modem */
        write(1, &c,1); /* prints input to the screen */
        }
    return 0;

    }

No comments:

Post a Comment