C Source Code Serial Port Communication Tutorial

Download example source code

Serial Communication Calculations and Program using 8051 Microcontroller. Serial communication is commonly used for transmitting and receiving the signal. The 8051 microcontroller have consist UART serial communication the signals transmitted and received by the Rx and Tx pins. The UART takes bytes of data and sends the individual bits in a. Perform a read on the serial port. Reading input from serial port is done by Listen invoked right after initialization of the serial port. We do this in the sample code by creating an async read task using the DataReader object that waits on the InputStream of the SerialDevice object. Due to differences in handling concurrent tasks, the. A short tutorial on building a Graphical User interface (GUI) Serial communication program using C# and Dotnet framework.The C# program is then used for communicating with an Arduino Uno /Microcontroller (ATMEL AVR,Mirochip PIC,MSP430) to send and receive data from Windows OS.You can use the opensource code to develop your own C# based serial control program. Serial Port Communication Code In C Codes and Scripts Downloads Free. I have enumerated my experience working with MATLAB serial port interfacing features in this article. Bluedoc is a Tool for generating documentation in HTML format from doc comments in source code in C and C. A Serial Communications Interface or Universal Asynchronous Receiver-Transmitter (UART) is a serial communications peripheral that implements the asynchronous serial communications protocol. The serial communications interface can be used to communicate with several devices such as displays, sensors, data acquisition systems, etc.


Introduction to RS-232:

RS232 is the most known serial port used in transmitting the data in communication and interface. Even though serial port is harder to program than the parallel port, this is the most effective method in which the data transmission requires less wires that yields to the less cost. The RS232 is the communication line which enables the data transmission by only using three wire links. The three links provides ‘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 as RTS, CTS, DSR, DTR, and RTS, RI. The ‘1’ and ‘0’ are the data which defines a voltage level of 3V to 25V and -3V to -25V respectively.

The electrical characteristics of the serial port as per the EIA (Electronics Industry Association) RS232C 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 show below.

D-Type-9 pin no.

D-Type-25pin no.

Pin outs

Function

3

2

RD

Receive Data(Serial data input)

2

3

TD

Transmit Data (Serial data output)

7

4

RTS

Request to send(acknowledge to modem that UART is ready to exchange data

8

5

CTS

Clear to send(i.e.; modem is ready to exchange data)

6

6

DSR

Data ready state(UART establishes a link)

5

7

SG

Signal ground

1

8

DCD

Data Carrier detect(This line is active when modem detects a carrier

4

20

DTR

Data Terminal Ready.

9

22

RI

Ring Indicator(Becomes active when modem detects ringing signal from PSTN

About DTE & 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 Communications 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.

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-DCE), then when the modem compresses it you are actually transferring 115.2 KBPS between computers and thus have a DCE-DTE speed of 115.2 KBPS. Thus, this is why the DCE-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.

What is NULL MODEM?

Null modem is used to connect two DTE's together. This is used to transfer files between the computers using protocols like Zmodem protocol, xmodem protocol, etc

Figure: Above shows the connections of the Null modem using RS-232D connecter

Above-mentioned figure shows the wiring of the null modem. The main feature indicated here is that the to make the computer to chat with the modem rather than another computer. The guest & host computer connected through the TD, RD, and SG pins. Any data that is transmitted through TD line from the Host to Guest is received on 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 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.

C Source Code Serial Port Communication Tutorial Free

All 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 RS232 ports available in your computer, Right click on 'My Computer', Goto 'Properties', Select 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 is selected).

How to program 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 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 port, parallel ports. Similarly to receive data these are used.

  • 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 lowbyte 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 instruction OUT. It writes thelow byte of value to portid, the high byte to portid + 1.

  • outportb is a macro that writes 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 themacro(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

Program

For more details of these functions read article from beondlogic.com

Using bioscom:

The macro bioscom () and function _bios_serialcom() are used in this method in the serial communication using RS-232 connecter. First we have to set the port with the settings depending on our need and availability. In this method, same function is used to make the settings using 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 deatails 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() uses the bios interrupt 0x14 to perform various communicate the serial communication over the I/O ports given in port.

cmd: The I/O operation to be performed.

Serial port communication program

cmd (boiscom)

cmd(_bios_serialcom)

Action

0

_COM_INIT

Initialise the parameters to the port

1

_COM_SEND

Send the character to the port

2

_COM_RECEIVE

Receive character from the port

3

_COM_STATUS

Returns rhe current status of the communication port

portid: port to which data is to be sent or from which data is to be read.

0: COM1
1: COM2
2: COM3

abyte:

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

C Source Code Serial Port Communication Tutorial

When cmd = 0 (_COM_INIT), abyte is an OR combination of the following bits(One from each group):

value of abyte

Meaning

Bioscom

_bios_serialcom

0x02

0x03

_COM_CHR7

_COM_CHR8

7 data bits

8 data bits

0x00

0x04

_COM_STOP1

_COM_STOP2

1 stop bit

2 stop bits

0x00

0x08

0X10

_COM_NOPARITY

_COM_ODDPARITY

_COM_EVENPARITY

No parity

Odd parity

Even parity

0x00

0x20

0x40

0x60

0x80

0xA0

0xC0

0xE0

_COM_110

_COM_150

_COM_300

_COM_600

_COM_1200

_COM_2400

_COM_4800

_COM_9600

110 baud

150 baud

300 baud

600 baud

1200 baud

2400 baud

4800 baud

9600 baud

C Source Code Serial Port Communication Tutorial Download

For example, if

abyte = 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)
1 stop bit (0x00 = _COM_STOP1)
8 data bits (0x03 = _COM_CHR8)

To initialise the port with 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 illustrate how to serial port programs. When a data is available in the port, it inputs the data and displays 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*/
}
}
return0;
}

When you compile and run the above program in both the computers, The characters typed in one computer should appear on the other computer screen and vice versa. Initially, we set the port to desired settings as defined in macro settings. Then we waited in an idle loop until a key is pressed or a data is available on the port. If any key is pressed, then kbhit() function returns non zero value. So will go to getch function where we are finding out which key is pressed. Then we are sending it to the com port. Similarly, if any data is available on the port, we are receiving it from the port and displaying it on the screen.

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


Fig 2. Loop-back plug connection

If you run the above program with the connection as in this diagram, the character entered in the keyboard should be displayed on the screen. This method is helpful in writing serial port program with single computer. Also you can make changes in the port id if your computer has 2 rs232ports. 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 port com1 should come to port com2. then also whatever you type in the keyboard should appear on the screen.

The program given below is an example source code for serial communication programmers. It is a PC to PC communication using RS232. Download the code, unzip and run to chat in dos mode between two computers. Use the program to get more idea about serial port programming.

Click here to download example source code: pc2pc.zip

Please contact us if any problem exits in the above programs.

C# serial port tutorial

>Interfacing Links:Links related to serial port, bioscom function, rs232, rs485, usb, parallel port etc.
>Click here to list other C Programming sites

C Serial Port Communication

Click here to send feedback or use email: harsha@electrosofts.com or veena@electrosofts.com

Serial Port Communication Program

Note: Examples given above are tested with Turbo C++ Compiler Version 3.0. How ever, we do not give any guaranty for working of the programs in the examples.