Loading...

Serial Port Communication in 'C'

Hardware interaction will always take place through ports. Port I/O is not limited to functions such as inportb( ), outportb( ) etc.
 In this article we will see how two computers can communicate via the Serial/COM port.

The Serial/COM port is a 9/25-pin port connector at the back of a computer. 9-pin mail connectors are more common.

 All COM ports are mail connectors. If the connector is 25-pin and a female, it is an LPT port and not a COM port. 
The type of connector will not affect the performance of the port.

A serial port is so named because it transmits data serially i.e. one bit at a time. So even if there are 9/25-pins on the connector,

 only 3 are of importance to us. The other are either left open. Some are loped back. 
Looping back is something like fooling the computer to believe that it is actually connected to some serial device like a modem.

The serial port is commonly used by software developers to connect two computers for testing new software.

 One computer runs the software while the other records the information sent by the software. This method, 
often known as debugging helps the developers find errors in their program. 
The cable used for connecting the two computers is known as Null Modem cable, debug-cable,
COM port to COM Port cable or simply serial cable. We will us this cable for our program.
 Our program will display whatever is typed on one computer on the other computer.

The software, is actually two different programs running on two different machines.

 The two machines are connected through the serial cable. Select one machine as the server and other as the client. 
The server will be used to type whereas the client will show whatever is typed. Any machine can be made the server or client.

The client side program:

/* client.c */
#include 
main( )
{
int out, status ;
bioscom ( 0, 0x80 | 0x02 | 0x00, 0 ) ;
while ( ! kbhit( ) )
{
status = bioscom ( 3, 0, 0 ) ;
if (status & 0x100 )
{
out = bioscom ( 2, 0, 0 ) & 0x007F ;  /* to check the 7 bit data for error condition */
if ( out != 0 )
putch ( out ) ;
}
bioscom ( 1, 0, 0 ) ;
}
}

The server side program:
/* server.c*/
#include 
main( )
{
int in, status ;
bioscom ( 0, 0x80 | 0x02 | 0x00, 0 ) ;
while ( ( in = getch( ) != 27 )
{
status = bioscom ( 3, 0, 0 ) ;
if ( status & 0x100 )
{
bioscom ( 2, 0, 0 ) ;
bioscom ( 1, in, 0 ) ;
}
}
}

In these programs we have not used the inportb( ), or outportb( ) functions instead we have used the bioscom( ) function.

 This is a standard library function declared in the bios.h file.

Before compiling and running these programs, make sure the two computers are connected.

 Now start the programs on separate machines. (Hopefully) Whatever is typed will be displayed on both the server as well as the client.
 If nothing appears on the client side, change the last parameter in the function bioscom( ) from 0 to1.
This will change the selected COM port from COM 1 to COM2 it is not necessary to connect COM1 to COM1 only or COM2 to COM2 only.
 Set the appropriate COM port for both the computers.

There is one more problem. If even one of the two computer runs a WinNT family (WinNT, Win2000, WinXp) operating system,

 this communication will not succeed. That is because this WinNT family of Operating systems do not support 
direct port communication through DOS programs. For this to work under windows, a windows program has to be written.

The bioscom( ) function accepts 3 parameters.

Parameter 1. It can have 4 ( 0 - 3 ) values depending upon what action to take.
0 - Sets the communication parameters like the speed of communication etc. for the
     Second parameter
1 - Sends the second parameter over the communication cable.
2 - Receives a character from the communication line.
3 - Returns the current status of the port.

Parameter 2. It sets the communication parameters for the byte being sent through the
                    link. 

Parameter 3. This can have 2 ( 0 - 1 ) values for the port through which to communicate.

0 - Communication through the COM1 port
1 - Communication through the COM2 port

In the initialization phase we have specified three speeds at which this communication can take place. 

'0x80 | 0x02 | 0x00' signifies a speed of 1200 bits per sec.

Lets now see how the programs actually work. The client or listening program first initializes the port through 

the call bioscom ( 0, 0x80 | 0x02 | 0x00, 0 ). It is now ready to listen to the server. After this we start the while loop which
 will continue until the user presses any key. During this time the program will listen on the COM port and display whatever it reads.
 We have implemented the bioscom( ) function thrice in the while loop. The first instance ( bioscom ( 3, 0, 0 ) )will read the status of the port
, the second instance ( bioscom ( 2, 0, 0 ) ) will read a character from the port and store it in the 'out' variable if the status is OK
 The third instance (bioscom ( 1, 0, 0 ) ) will send back a notification to the server that it has successfully received the character. 
The last instance will prompt the server to send another byte.

In this case the program instead of reading from the port, will write on it and wait for notification to send another byte.

 Here also the server will first initialize the port and proceed into the while loop. This program will continue until the user presses the 'Esc' key
. The server program implements bioscom( ) three times inside the while loop. The first instance ( bioscom ( 3, 0, 0 ) ) will check 
for status of the port. The second instance ( bioscom ( 2, 0, 0 ) )will see if the previously sent byte was received correctly or not. And the third
 instance ( bioscom ( 1, in, 0 ) ) will send the new byte. The byte being sent is the lower byte of the integer (16-bit) value. 
The upper 8 bits represent the status of the data transfer.

The LPT is commonly known as the Parallel port or Printer port. It is a pink colored 25 pin female connector at the back of a computer.
 Parallel port is so called because it transmits data in bytes and not bits. We can transmit one complete byte at a time, along with control
 signals for the distant device. This mechanism of transmitting bytes instead of bits greatly increases the rate of data transfer through this port.
 Because of these features the parallel port has become one of the most widely used for implementing projects dealing with control of 
real world peripherals.
The LPT is actually a group of three ports, data port, control port and status port. These ports enjoy three separate addresses.

  For a standard PC the port addresses are typically 378h, 379h, 37ah for data, status and control ports respectively, these addresses 
may differ depending upon whether it is an LPT1 Port or a LPT2 port, but the order remains same.
The 25 pins are divided among the ports in the following manner. 

Pin Number  
Port No / Port Name  
Pin Name(s)  
18 – 25  
Non  
Ground Pins  
2 – 9  
0x378 / Data Port  
Data Pins  
10 – 13, 15  
0x379 / Status Port  
Status Pins  
1, 14, 16, 17  
0x37a / Control Port  
Control Pins  
The remaining pins are reserved for other use. The data pins are true logic, i.e. the output is the same as we put.
 In other words if we send true logic ( logic 1 or 5V signal ) the same will be reflected on the pins. There are some pins in the control port
 and the status pins that show inverted logic. This means that if we output a true signal ( logic 1 or 5V signal ) on the pin it will actually be 
reflected as a false (logic 0 or 0V signal). Pin number 1, 10, 14, 15, 17 are inverted logic pins. This makes the LPT a little bit more 
complicated than a serial port.
The LPT was primarily designed for printer interfacing, this required bi directional communication of information. 

The computer needs to read the printer status of the printer and the printer needs to get information to decide the mode of operation 
from the computer. Of the 25 pins 17 are active pins whereas the rest are ground (0 voltage always). Of these 17, 2 to 9 are the data pins. 
The remaining ones are the ones that are shown in the table
Pin Number 
Description (Port ) 
Type 
1 
Strobe ( Control ) 
Inverted Logic 
10 
ACK  ( Status ) 
Inverted Logic 
11 
Busy  (Status ) 
True logic 
12 
Paper Empty (Status ) 
True Logic 
13 
Select  ( Status ) 
True Logic 
14 
Auto Feed  (Control ) 
Inverted Logic 
15 
Error  (Status ) 
Inverted Logic 
16 
Initialize Printer  ( Control ) 
True Logic 
17 
Select Input  (Control ) 
Inverted Logic 
 In this article we will interface a 
simple 7-segment LED array with the
 LPT. The 7-segment will display 
numbers 0- 9 in cyclic manner.
 The circuit for the same is as given
 below. Rightmost connected pin in 
the top row of the Figure. 1 is the
 second pin whereas the Leftmost 
connected pin is pin number 9 of the 
LPT.
This was our circuit, for someone else this might change a little bit. The resistors we used were 560 ohm’s and the grounds were connected
 to pin 18 – 25.
The 7-Segments are separately controlled by 8 data pins of the LPT. To control each of these pins we need to send specific numbers. 

The format of these numbers is given below.

         32
      -------
 64|           |           The numbers are the Bit that needs to be sent
     |  128   | 16      through the LPT in order to glow that specific
      -------            filament of the 7-Segment.
     |           |
  1 |           |  4       These numbers activate only one pin of the LPT port at a
      -------    
. 8    time. The result is that when we OR these numbers in a
           2                specific format, we get control over each filament of the
                             7-Segment.
The program to run this circuit is given below:
# include 
main( )
{
int  i = 0 ;
unsigned char  seg[ ] = {
64 | 1 | 2 | 4 | 16 | 32,          /* 0 */
16 | 4,                               /* 1 */
32 | 16 | 128 | 1 | 2,            /* 2 */
32 | 16 | 128 | 4 | 2,            /* 3 */
64 | 128 | 16 | 4,                /* 4 */
32 | 64 | 128 | 4 | 2,            /* 5 */
32 | 64 | 1 | 2 | 4 | 128,        /* 6 */
32 | 16 | 4,                         /* 7 */
32 | 64 | 128 | 4 | 2 | 1 | 16,  /* 8 */
32 | 64 | 128 | 16 | 4 | 2        /* 9 */
} ;
while ( ! kbhit( ) )
{
outportb ( 0x378, seg[ i ] ) ;
delay ( 1000 ) ;
i++ ;
if ( i == 10 )
i = 0 ;
}
outportb ( 0x378, 0 ) ;
}
Again a word of caution these programs might not work properly under WinNT family of operating systems.


Reactions: 
Mini Projects 637215825972153787
Home item

Follow by Email

Popular Posts

Random Posts

Free PAKISTAN Cursors at www.totallyfreecursors.com