Freelance Writing Jobs | Today's Articles | Sign In


Reading Information From a PC Serial Port

How To Read a Character From The RS-232 COM1 of a Personal Computer

Jun 4, 2009 Martin Bell

A home PC can be set up to read characters from an external device. Voltage considerations are discussed, as well as C/C++ code that will perform the information capture.

There are times when it is useful to be able to use a home computer to "talk" with other devices through the serial ports. For example, an external device that reads the temperature or humidity in a greenhouse may be able to send the information through a built-in serial port. Another example is when a measurement device can supply a voltage (representing temperature or humidity) that is converted to a number through an Analogue to Digital Converter (ADC), which is then sent to a PC serial port for recording.

The article Reading a Serial Port Using Windows can be used for a Windows (95 or above) environment.

Signal Voltage on RS-232

Before two devices can communicate with each other via a serial port, there are electronic considerations. It is necessary for the two devices to have the same signal meaning, usually "1" is denoted by a negative voltage, and "0" denoted by a positive voltage. The operating voltage should be the same for each device, and the RS-232C voltage level is defined as +3 to +15 Volts for "0", -3 to -15 Volts for "1". If the signals from the computer and the device are not the same, they will not work, so the ADC or the device output must be +15 V to -15V.

RS-232 Communications Protocol

The devices that are communicating must be expecting the same "format" of data:

  1. Bit priority (Least Significant Bit first)
  2. Number of data bits
  3. Whether parity bit is used
  4. Number of Start & Stop bits
  5. Bit rate (i.e. baud rate, or bits/second)

The five items above are defined by software. In the program used here, the function called "port_init" sets them up. If there are any doubts about which settings to use, the manufacturers data sheet should be consulted.

Software to Read RS-232 Port

The standard Input / Output library is used:

  • #include<stdio.h>

The serial ports on a standard home PC (COM1 and COM2) may vary, but the most common address for COM1 is hexadecimal 3f8:

  • #define BASE_ADDR 0x3f8

Declare and Define a Function to Set Up the Port Communications

The code used to set up RS-232 port communications is as follows:

void port_init(int base_addr);
void port_init(int port_num)
{outportb(port_num+3,131); /* Set-up mode */
outportb(port_num,24); /* For 4800 divisor = 24 */
outportb(port_num+1,0); /* ... and high byte = 0 */
outportb(port_num+4,0); /* No handshake */
outportb(port_num+3,3); /* Back to Communication mode */
outportb(port_num+1,1); }

Declare and Define a Function to Read From the Serial Port

Reading from the Serial Port is achieved with code such as:

char get_char(int port_num);
char get_char(int port_num)
{char x;
while(((inportb(port_num+5))&1)!=1) /* While bit 0 of the Line Status Register is zero */
{} /* ... Do nothing */
x=inportb(port_num); /* When Line Status Register is 1, a character is ready to be read so read the serial port */
return(x); } /* return the character read from the serial port to the main program */

Summary of Reading a Computer Serial COM1 Port Using RS-232 / EIA-232

It is possible to use a home computer to communicate with external devices such as other computers or external sensors, using the serial port.

The signal voltage of the external device may need to be conditioned first, and communications protocol must be agreed between the devices, but when that is done, the software shown here may be used to read information into the home computer. The article How to Configure an RS-232 Port On A PC To Read describes how to change data transfer rate, parity, word length etc. The hardware needed to connect to the serial cable is readily available and cheap.

The copyright of the article Reading Information From a PC Serial Port in Computer Programming is owned by Martin Bell. Permission to republish Reading Information From a PC Serial Port in print or online must be granted by the author in writing.
PC Serial Port Can Be Used To Talk To Other Device, beanworks PC Serial Port Can Be Used To Talk To Other Device
   
What do you think about this article?

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
post your comment
What is 0+1?

Comments

Jun 4, 2009 1:16 PM
Martin Bell :
I am fixing up the format to be more easily read.

This code does work on the windows 98 and win2000 platforms - it was written with a C++ 4.52 compiler.
Jun 16, 2009 5:37 AM
Martin Bell :
Note that the windows version of this code is available in the article link at http://c-programming.suite101.com/article.cfm/reading_a_serial_port_using_w indows
2 Comments
;