Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
Practical Paper
Industrial Training
Serial Port Communication
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 <bios.h>
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 <bios.h>
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.