Watch The Video Tutorial

Introduction

A serial port interface can be used for serial communication of data (send and receive) where data is sent or received one bit at a time between a personal computer (PC) and various devices supporting this type of protocol like PIC microcontrollers, GPS, GSM modem etc. While other interfaces like Ethernet, FireWire, and USB all send data as a serial stream, the term “serial port” usually identifies hardware more or less compliant to the RS-232 standard, intended to interface with a modem or with a similar communication device.  RS232 Serial communication is still widely used in industrial application and with many electronic devices like modems but it is being replaced by the USB in portable devices.

The Universal Asynchronous Receiver/Transmitter (UART) controller is the key component of the serial communications between a device and a PC or between devices.
Most all microcontrollers have an internal UART at a specific pins of the microcontrollers but this feature can also be implemented at any pin with a software in most of the compilers.
Communication can be “full duplex” (both send and receive at the same time) or “half duplex” (devices take turns transmitting and receiving).

Most microcontrollers with UART uses TTL (Transistor-transistor Logic) level UART. It is the simplest form of UART.
Logic 1 is represented by 5V and logic 0 by 0V.
Logic                                               Voltage 
Low                                                     0V
High                                                    5V

Voltage level for TTL level UART

The TTL level UART is commonly used in the communications between microcontrollers and ICs. Only 2 wires are required for the full duplex communications as illustrated in the picture below on figure 1.

Serial Communication Between 2 devices

Figure 1: Serial Communication Between 2 devices

 The RS-232

The RS-232 is a standard for serial binary data signals connecting between a Data Terminal Equipment (DTE) and a Data Communication Equipment (DCE).
One of the significant differences between TTL level UART and RS-232 is the voltage level. Valid signals in RS-232 are ±3 to – ±15V, and signals near 0V is not a valid RS-232 level.
Logic                                             Voltage 
Low                                                   +3 to +15V
High                                                  -3 to -15V

Voltage level for RS-232

Besides voltage level, the RS-232 also has a few extra pins specifically designed for the communication between PC and modem. The pinouts of the DB-9 and their functions are shown below in table 1.

Pin Signal abreviation Signal Name DTE (PC)
1 DCD Data Carrier Detect In
2 RXD Receive Data In
3 TXD Transmit Data Out
4 DTR Data Terminal Ready out
5 GND Signal Ground
6 DSR Data Set Ready in
7 RTS Request to Send out
8 CTS Clear to Send In
9 RI Ring Indicator In

Table 1: Pinout and diagram of DE9 connector (DB9 connector), commonly used for serial ports (RS-232).

The maximum cable length for RS-232 is about 15m (50ft), but in practise depends on baud rate, cable specific capacitance and ambient noise.
The table below contains some rules-of-thumb for recommended distances.

Baud Rate Maximum cable length
56000 2.6m (8.5ft)
38400 3.7m (12ft)
19200 7.6m (25ft)
9600 15m (50ft)
4800 30m (98ft)
2400 60m (196ft)

Table 2: Recommended distances

 Interfacing between TTL level UART and RS-232

From the above discussions, we know that microcontrollers use TTL level UART (5V for logic 1 and 0V for logic 0) while the PC serial port uses RS-232. Since both standards uses similar software protocol, both of them are able to communicate via UART. However, because of the differences in voltage level and polarity, we will need a level shifter to interface the TTL level UART with the RS-232. Nowadays, this can be easily done with the commonly available IC such as the MAX2322 from Maxim.
The diagram below on figure 2 illustrates how the MAX232 IC can be connected to the UART of a microcontroller (TX and RX pins) and a personal computer with DB9 connector.

SThe use of MAX232 IC to convert TTL levels from microcontroller to RS232 level

Figure 2: The use of MAX232 IC to convert TTL levels from microcontroller to RS232 level

Flowcode provides a component for RS232 Serial which makes everything easy, it behaves like a virtual terminal to simulate the Send and the Receive process as well.

–> To insert an RS232 Serial component, on the components tool bar click on RS232 under the Comms group. An RS232 component (RS232(0)) will be inserted on the panel.

Inserting an RS232 Component

Figure 3: Inserting an RS232 Component

—>> select the RS232 Component then click on the “….” next to the Ext Properties to edit the RS232 Serial communication properties.
The two most important settings here is to set the BAUD rate. The default is 9600 but from the drop down box, you can select a different BAUD rate if needed. The other important setting is the TX / RX. Here you can choose between a hardware UART if available (most of PIC Microcontrollers nowadays have at least one hardware UART and some have even more than one) and a software UART (for PIC which don’t have a hardware UART or if the hardware UART pins have already been used for something else). It is always recommended to use the hardware UARTs for serial communication if available as they have dedicated buffers that allow for data to be sent and received while you are processing other portions of your program. If a software UART is selected pins for the RX and TX lines are available in the components connections properties. RS232 Component Properties

Figure 4: RS232 Component Properties

The other settings, you could in most of the cases leave them with their defaults value.

Data bits: This specifies the number of data bits to use. The default value is 8 bytes and note that the 7-bit operation is not supported when using the hardware UARTs.

Receive Type: Defines the value for the timeout return value, the Byte (Legacy) sets the INVALID_RETURN timeout value as 255 allowing acceptable data values 0-254 (8 bit mode). The Int (Extended) sets the INVALID_RETURN timout value as 512 allowing acceptable data values 0-255 (8 bit mode) or 0-511 (9 bit mode).

RX Timeout: This configures the receive macro timeout structure as a number of iterations (Legacy) or as an actual time to wait for data (Milliseconds).

Flow Control: If flow control is enabled then the RTS and CTS pins become active in the send and receive functions.

Echo Mode: This mode allows any incoming data to be echoed straight back out simply by calling one of the receive functions.

Display Characters or Bytes: Defines whether sent or received values are shown as numbers or ASCII characters in the Flowcode simulation.

Show Simulation: Enables the RS232 component to be present in the Flowcode panel window during simulation.

—>> select the RS232 Component again and click on the “….” next to connections properties to open the RS232 connection properties.
Note that the properties is available only if you selected to use Software UART in the Ext Properties above. The connection properties is not available with Hardware UART as the compiler will know internally which pins are used for The Hardware UART selected (UART1, UART2, …).

 RS232 Connection Properties

Figure 5: RS232 Connection Properties

Component Macros

Drag and drop the Component Macros from the Icons toolbar. Component Macro

Figure 6: Component Macro

Double click the component macro to open its properties. Click on the RS232(0) to see its macros.

The RS232 Component provides the following macros:

RS232 Macro Properties

Figure 7: RS232 Macro Properties

SendRS232Char(Char): This macro Sends a char via the RS232 connection. It takes one parameter: The char value to be sent in single quote or a char variable.

SendRS232String(String): This macro Sends a string of characters via the RS232 connection. It takes one parameter: The string value to be sent in double quotes or a string variable.

char ReceiveRS232Char(timeout): This macro is used to receive the next char of data from the RS232 connection. Timeout specifies how long to wait for a char of data as the program cannot just sit there for ever waiting for a character to be sent. The macro returns the data value that was sent or INVALID_RETURN if there was no data received.

char ReceiveRS232Char(timeout): This macro is used to receive the next char of data from the RS232 connection. Timeout specifies how long to wait for a char of data as the program cannot just sit there for ever waiting for a character to be sent. The macro returns the data value that was sent or INVALID_RETURN if there was no data received.

char ReceiveRS232Char(timeout, String, Length): This macro is used to receive a string of character data from the RS232 connection. Timeout specifies how long to wait for a char of data as the program cannot just sit there for ever waiting for a character to be sent. String defines the string variable to store the incoming data. Length defines the maximum number of characters to wait for. The macro returns the number of characters received or INVALID_RETURN if there was no data received.

Let us create a simple program to demonstrate how to use the RS232 Serial Communication with Flowcode. Upon receiving data via RS232, the Microcontroller immediately sends it back to the sender. The PIC18F2220 is used, the Internal Oscillator is selected, Clock frequency of 8MHz and the MCLR is disabled. 

RS232 Simple Echo

Figure 8: RS232 Simple Echo

You can download the full project files (Flowcode and Proteus Schematic design) below here. 
All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).  

Download Flowcode RS232 Simple Echo