Sunday, April 7, 2013

Null Modem Sender

After installing null modem, then we are ready to send character data through serial port. I have an example of sending data through the null modem that we have prepared before. Lets have a look at the source code :

#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)*/
#include "time.h" /*this file contains time's operatives*/


int main(void)
    {
    int i, j, s;
    char t[9];
    time_t ttrs;
    struct tm *ti;
    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/ttyS0", O_RDWR | O_NOCTTY); /* I have done : sudo chmod 777 /dev/ttyS0 */


    /* 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 receiver (this computer is a sender)
    1. Baudrate is 4800 BPS (B9600)
    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
    */


    /* flush the modem line */
    tcflush(fd, TCIFLUSH);

    /* initiate the modem */
    tcsetattr(fd,TCSANOW,&termiosA);

    /* Now its time to send something. */
    i = 0;
    for (;;)
        {
        time(&ttrs);
        ti=localtime(&ttrs);
        if (s != ti->tm_sec)
            {
            printf("Sending time data : %s", t);
            sprintf(t, "%d:%d:%d\n", ti->tm_hour, ti->tm_min, ti->tm_sec);
            s = ti->tm_sec;
            for (j = 0; j < 9;j++)
                {
                write(fd, t[j], 1);
                }
            }
        }
    return 0;
    }

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;

    }

Thursday, March 12, 2009

Making a Null Modem : Loop-back type



Before doing serial port programming, we need to build a null modem. A null modem is a cable with two rs-232 connectors. Usually we use female connector. This connector is known as DB-9 female connector. The Layout of a DB-9 connector, if we see it from THE SOLDERING SIDE, is shown below :

1 2 3 4 5
6 7 8 9

For our first project we are going to build a loopback type null-modem. You will need two DB-9 CONNECTORS, 1 meter long UTP cables, and a set of soldering equipment. You will have to solder cables at the soldering site if DB-9 CONNECTORS. To make things simple, I named one connector as CONNECTOR-1 and the other as CONNECTOR-2. Now these are the soldering work of our null-modem :
  • for BOTH CONNECTORS you have to SHORT-CIRCUIT pin 1, 4, and 6.
  • for BOTH CONNECTORS you have to SHORT-CIRCUIT pin 7 and 8.
  • connect pin 2 of CONNECTOR-1 with pin 3 of CONNECTOR-2 (I use BROWN cable).
  • connect pin 3 of CONNECTOR-1 with pin 2 of CONNECTOR-2 (I use GREEN cable).
  • connect pin 5 of CONNECTOR-1 with pin 5 of CONNECTOR-2 (I use BLUE cable).

Greet

Hello World