Robotics

3.6 Serial Communication: RS-232

RS-232 is the most known serial port used in transmitting the data in communication and interface. Even though a serial port is harder to program than the parallel port, this is the most effective method in which the data transmission requires fewer wires and less cost. The RS-232 is the communication line which enables data transmission by only using three wire links. The three links provide ‘transmit,’ ‘receive,’ and common ground.

The ‘transmit’ and ‘receive’ line on this connecter send and receive data between the computers. As the name indicates, the data is transmitted serially. The two pins are TXD & RXD. There are other lines on this port such as RTS, CTS, DSR, DTR, and RTS, RI. The ‘1’ and ‘0’ are the data which defines a voltage level of 3 V to 25 V and -3 V to -25 V respectively.

The electrical characteristics of the serial port as per the EIA (Electronics Industry Association) RS-232C Standard specifies a maximum baud rate of 20,000bps, which is slow compared to today’s standard speed. For this reason, we have chosen the new RS-232D Standard, which was recently released.

The RS-232D has existed in two types, i.e., D-Type 25-pin connector and D-Type 9-pin connector, which are male connectors on the back of the PC. You need a female connector on your communication from host to guest computer. The pin outs of both D-9 & D-25 are shown in Table 3.10.

About DTE and DCE

Devices, which use serial cables for their communication, are split into two categories. These are DCE (Data Communications Equipment) and DTE (Data Terminal Equipment). Data Communication Equipments are devices such as your modem, TA adapter, plotter, etc., while Data Terminal Equipment is your computer or terminal. A typical Data Terminal Device is a computer and a typical Data Communications Device is a modem. Often people will talk about DTE to DCE or DCE to DCE speeds. DTE to DCE is the speed between your modem and computer, sometimes referred to as your terminal speed. This should run at faster speeds than the DCE to DCE speed. DCE to DCE is the link between modems, sometimes called the line speed.

  TABLE 3.10
D-type 9-
pin no.
D-type 25-
pin no.
Pin outs Function
3
2
7

8
6
5
1

4
9

2
3
4

5
6
7
8

20
22

RD
TD
RTS

CTS
DSR
SG
DCD

DTR
RI

Receive data (serial data input)
Transmit data (serial data output)
Request to send (acknowledge to modem that UART
is ready to exchange data)
Clear to send (i.e., modem is ready to exchange data)
Data ready state (UART establishes a link)
Signal ground
Data carrier detect (this line is active when modem
detects a carrier)
Data terminal ready
Ring Indicator (becomes active when modem detects
ringing signal from PSTN)

Most people today will have 28.8K or 33.6K modems. Therefore, we should expect the DCE to DCE speed to be either 28.8K or 33.6K. Considering the high speed of the modem we should expect the DTE to DCE speed to be about 115,200 BPS (maximum speed of the 16550a UART). The communications program, which we use, has settings for DCE to DTE speeds. However, the speed is 9.6 KBPS, 144 KBPS, etc., and the modem speed.

If we were transferring that text file at 28.8K (DCE to DCE), then when the modem compresses it you are actually transferring 115.2 KBPS between computers and thus have a DCE to DTE speed of 115.2 KBPS. Thus, this is why the DCE to DTE should be much higher than the modem s connection speed. Therefore, if our DTE to DCE speed is several times faster than our DCE to DCE speed the PC can send data to your modem at 115,200 BPS.

Null Modem

A null modem is used to connect two DTEs together. This is used to transfer files between the computers using protocols like zmodem protocol, xmodem protocol, etc.

Figure 3.45 shows the wiring of the null modem. The main feature indicated here is to make the computer chat with the modem rather than another computer. The guest and host computer are connected through the TD, RD, and SG pins. Any data that is transmitted through the TD line from the host to guest is received on the RD line. The guest computer must have the same setup as the host. The Signal Ground (SG) line of the both must be shorted so that grounds are common to each computer.


the connections of the null modem
using an RS 232D connector

FIGURE 3.45    Above shows the connections of the null modem
using an RS 232D connector.

The Data Terminal Ready (DTR) is looped back to Data Set Ready and Carrier Detect on both computers. When the Data Terminal Ready is asserted active, then the Data Set Ready and Carrier Detect immediately become active. At this point, the computer thinks the virtual modem to which it is connected, is ready, and has detected the carrier of the other modem.

All that s left to worry about now is the Request to Send and Clear to Send. As both computers communicate together at the same speed, flow control is not needed; thus these two lines are also linked together on each computer. When the computer wishes to send data, it asserts the Request to Send high and as it is hooked together with the Clear to Send, it immediately gets a reply that it is OK to send and does so.

The ring indicator line is only used to tell the computer that there is a ringing signal on the phone line. As we do not have a modem connected to the phone line this is left disconnected.

To know about the RS-232 ports available in your computer, right-click on My Computer, go to Properties, select the tab Device Manager, go to Ports (COM & LPT). In that you will find Communication Port(Com1), etc. If you right-click on that and go to properties, you will get device status. Make sure that you have enabled the port (use this port selected).

Programming the Serial Port using C/C++

There are two popular methods of sending data to or from the serial port in Turbo C. One is using outportb(PORT_ID, DATA) or outport(PORT_ID,DATA) defined in dos.h. Another method is using the bioscom() function defined in bios.h.

Using outportb()

The function outportb() sends a data byte to the port PORT_ID. The function outport() sends a data word. These functions can be used for any port including serial ports, and parallel ports. Similarly, these are used to receive data.

  • inport reads a word from a hardware port
  • inportb reads a byte from a hardware port
  • outport outputs a word to a hardware port
  • outportb outputs a byte to a hardware port

Declaration

  • int inport(int portid);
  • unsigned char inportb(int portid);
  • void outport(int portid, int value);
  • void outportb(int portid, unsigned char value);

Remarks

  • inport works just like the 80x86 instruction IN. It reads the low byte of a word from portid, the high byte from portid + 2.
  • inportb is a macro that reads a byte.
  • outport works just like the 80x86 instructions OUT. It writes the low byte of a value to portid, the high byte to portid + 1.
  • outportb is a macro that writes the value argument.

portid

  • Inport port that inport and inportb read from;
  • Outport port that outport and outportb write to

value

  • Word that outport writes to portid;
  • Byte that outportb writes to portid.

If you call inportb or outportb when dos.h has been included, they are treated as macros that expand to inline code.

If you don t include dos.h, or if you do include dos.h and #undef the macro(s), you get the function(s) of the same name.

Return Value

# inport and inportb return the value read
# outport and outportb do not return

Using Bioscom

The macro bioscom() and function _bios_serialcom() are used in this method in the serial communication using an RS-232 connecter. First we have to set the port with the settings depending on our need and availability. In this method, the same function is used to make the settings using a control word to send data to the port and check the status of the port. These actions are distinguished using the first parameter of the function. Along with that we are sending data and the port to be used to communicate.

Here are the details of the Turbo C functions for communication ports.

Declaration

  • bioscom(int cmd, char abyte, int port)
  • _bios_serialcom(int cmd ,int port, char abyte)
  • bioscom() and _bios_serialcom() use the bios interrupt 0x14 to perform various serial communications over the I/O ports given in port.
  • cmd: The I/O operation to be performed.

portid

Port to which data is sent or from which data is read.
   0:COM1
   1:COM2
   2:COM3

a byte

When cmd = 2 or 3 (_COM_SEND or _COM_RECEIVE) parameter a byte is ignored.

When cmd = 0 (_COM_INIT), a byte is an OR combination of the following bits (one from each group). For example, if a byte = 0x8B = (0x80 0x08 0x00 0x03) = (_COM_1200 _COM_ODDPARITY _COM_STOP1 _COM_ CHR8). The communications port is set to:

   1200 baud (0x80 = _COM_1200)
   Odd parity (0x08 = _COM_ODDPARITY)

  TABLE 3.11
cmd (boiscom) cmd(_bios_serialcom) Action
0
1
2
3
 _COM_INIT
 _COM_SEND
 _COM_RECEIVE
 _COM_STATUS
 Initialize the parameters to the port
 Send the character to the port
 Receive character from the port
 Returns the current status of the
 communication port


  TABLE 3.12
value of abyteMeaning
Bioscom _bios_serialcom
0x02   _COM_CHR7 7 data bits
0x03   _COM_CHR8 8 data bits
0x00   _COM_STOP1 1 stop bit
0x04   _COM_STOP2 2 stop bits
0x00   _COM_NOPARITY No parity
0x08   _COM_ODDPARITY Odd parity
0X10   _COM_EVENPARITY Even parity
0x00   _COM_110 110 baud
0x20   _COM_150 150 baud
0x40   _COM_300 300 baud
0x60   _COM_600 600 baud
0x80   _COM_1200 1200 baud
0xA0   _COM_2400 2400 baud
0xC0   _COM_4800 4800 baud
0xE0   _COM_9600 9600 baud

1 stop bit (0x00 = _COM_STOP1)
8 data bits (0x03 = _COM_CHR8)

To initialize the port with the above settings we have to write, bioscom(0, 0x8B, 0). To send a data to COM1, the format of the function will be bioscom(1, data, 0). Similarly, bioscom(1, 0, 0) will read a data byte from the port.

The following example illustrates how to serial port programs. When data is available in the port, it inputs the data and displays it onto the screen and if a key is pressed the ASCII value will be sent to the port.

#include <bios.h>
#include <conio.h>
#define COM1   0
#define DATA_READY 0x100
#define SETTINGS ( 0x80 0x02 0x00 0x00)
int main(void)
{
    int in, out, status;
    bioscom(0, SETTINGS, COM1); /*initialize the port*/
    cprintf(“Data sent to you: “);

    

while (1)
     {
       status = bioscom(3, 0, COM1); /*wait until get a data*/
       if (status & DATA_READY)
         if ((out = bioscom(2, 0, COM1) & 0x7F) != 0) /*input
             a data*/
             putch(out);
         if (kbhit())
        {
             if ((in = getch()) == 27) /* ASCII of Esc*
                   break;
             bioscom(1, in, COM1); /*output a data*/
        }
    }
    return 0;
}

When you compile and run the above program in both computers, the characters typed in one computer should appear on the other computer screen and vice versa. Initially, we set the port to the desired settings as defined in macro settings. Then we wait in an idle loop until a key is pressed or a data is available on the port. If any key is pressed, then the kbhit() function returns a nonzero value. Then we send it to the com port. Similarly, if any data is available on the port, we receive it from the port and display it on the screen.

To check the port, if you have a single computer, you can use a loop-back connection as follows. This is the most commonly used method for developing communication programs. Here, data is transmitted to that port itself.

If you run the above program with the connection as in Figure 3.46, the character entered in the keyboard should be displayed on the screen. This


Loop-back plug connection

FIGURE 3.46    Loop-back plug connection.

method is helpful in writing serial port programs with a single computer. Also, you can make changes in the port id if your computer has 2 RS-232 ports. You can connect the com1 port to com2 of the same computer and change the port id in the program. The data sent to the com1 port should come to the com2 port. Then whatever you also type on the keyboard should appear on the screen.

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: DC Motors
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.