Click here to Skip to main content
Click here to Skip to main content
Go to top

A PicRS232 control with a PIC microcontroller serial port

, 11 Dec 2009
Rate this:
Please Sign up or sign in to vote.
You can have a small home automation system controlling the serial port and a PIC microcontroller.

html

Introduction

This article demonstrates an application called PicRS232 on monitoring the serial port. In the serial port, we connect a PIC microcontroller which can control motors, fans, lamps turning on and off etc.

Background

The PIC microcontroller internally knows some bytes it receives from the serial port. For example, if you send the letter "t" in hexadecimal as 0x74, you can turn on or off an LED. Within the PIC, you can choose which byte of the ASCII table to recognize an action when it detects one.

It basically works like this:

Visual C# <-----> PC <-----> MAX232 <-----> PIC <-----> any peripheral.

Using the code

Send frames

In this case, I put an example of sending a byte out the serial port by pressing a button.

private void button_t_Click(object sender, EventArgs e)
{
    byte[] mBuffer = new byte[1];
    mBuffer[0] = 0x74; //ASCII letter "t".
    serialPort1.Write(mBuffer, 0, mBuffer.Length);
}

You can also send multiple bytes into frames.

private void button_t_Click(object sender, EventArgs e)
{
    byte[] mBuffer = new byte[5];
    mBuffer[0] = 0x74;
    mBuffer[1] = 0x75;
    mBuffer[2] = 0x89;
    mBuffer[3] = 0x20;
    mBuffer[4] = 0x6C;
    serialPort1.Write(mBuffer, 0, mBuffer.Length);
}

If you decide to send many bytes in a row, better use this code in ASCII:

private void button_b_Click(object sender, EventArgs e)
{
    byte[] mBuffer = Encoding.ASCII.GetBytes("Hello World");
    serialPort1.Write(mBuffer, 0, mBuffer.Length);
}

Here is the complete source code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO.Ports; // Do not forget.

namespace PicRS232
{
    public partial class Form1_Principal : Form
    {
        // Use a string as the receive buffer.
        string Recibidos;
        public Form1_Principal()
        {
            InitializeComponent();
            // Open port while running the application.
            if (!serialPort1.IsOpen)
            {
                try
                {
                    serialPort1.Open();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            // Run the Reception function by triggering the Event 'DataReived'.
            serialPort1.DataReceived += new
            System.IO.Ports.SerialDataReceivedEventHandler(Recepcion);
        }
        // Upon receiving the data.
        private void Recepcion(object sender, 
                System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            // Collecting the characters received to our 'buffer' (string).
            Recibidos += serialPort1.ReadExisting();
            // Invoke or call the process of hatching.
            this.Invoke(new EventHandler(Actualizar));
        }
        // Process the data received in the full frame buffer and extract.
        private void Actualizar(object s, EventArgs e)
        {
            // Assign the value of the plot to the RichTextBox.
            richTextBox_visualizar_mensaje.Text = Recibidos;
        }
        private void button_t_Click(object sender, EventArgs e)
        {
            byte[] mBuffer = new byte[1];
            mBuffer[0] = 0x74; //ASCII letter "t".
            serialPort1.Write(mBuffer, 0, mBuffer.Length);
        }

        private void button_b_Click(object sender, EventArgs e)
        {
            byte[] miBuffer = new byte[1];
            miBuffer[0] = 0x62; //ASCII letter "b".
            serialPort1.Write(miBuffer, 0, miBuffer.Length);
        }

        private void button_a_Click(object sender, EventArgs e)
        {
            byte[] mBuffer = new byte[1];
            mBuffer[0] = 0x61; //ASCII letter "a".
            serialPort1.Write(mBuffer, 0, mBuffer.Length);
        }

        private void button_l_Click(object sender, EventArgs e)
        {
            byte[] mBuffer = new byte[1];
            mBuffer[0] = 0x6C; //ASCII letter "l".
            serialPort1.Write(mBuffer, 0, mBuffer.Length);
        }

        private void button_Espacio_Click(object sender, EventArgs e)
        {
            byte[] mBuffer = new byte[1];
            mBuffer[0] = 0x20; //ASCII letter "Space".
            serialPort1.Write(mBuffer, 0, mBuffer.Length);
        }

        // Show local time.
        private void timer1_Tick(object sender, EventArgs e)
        {
            statusStrip1.Items[0].Text = DateTime.Now.ToLongTimeString();
        }

        // We always show the message below.
        private void richTextBox_visualizar_mensaje_TextChanged(object sender, EventArgs e)
        {
            richTextBox_visualizar_mensaje.SelectionStart = 
                        richTextBox_visualizar_mensaje.TextLength;
            richTextBox_visualizar_mensaje.ScrollToCaret();
        }
    }
}

Points of interest

Some time ago, I made a manual on how to control the serial port to control a PIC microcontroller. You can see it here.

RS232 specifications, introduction

Communication as defined in the RS232 standard is an asynchronous serial communication method. The word serial means that the information is sent one bit at a time. The word asynchronous tells us that the information is not sent in predefined time slots. Data transfer can start at any given time, and it is the task of the receiver to detect when a message starts and ends. Asynchronous communication has some advantages and disadvantages, which are both discussed in the next paragraph.

RS232 bit streams

The RS232 standard describes a communication method where information is sent bit by bit on a physical channel. The information must be broken up in data words. The length of a data word is variable. On PCs, a length between 5 and 8 bits can be selected. This length is the net information length of each word. For proper transfer, additional bits are added for synchronization and error checking purposes. It is important that the transmitter and receiver use the same number of bits. Otherwise, the data word may be misinterpreted, or not recognized at all.

With synchronous communication, a clock or trigger signal must be present which indicates the beginning of each transfer. The absence of a clock signal makes an asynchronous communication channel cheaper to operate. We only need lesser lines in the cable. A disadvantage is that the receiver can start receiving the information at the wrong moment. Resynchronization is then needed, which costs time. All data received in the resynchronization period is lost. Another disadvantage is that extra bits are needed in the data stream to indicate the start and end of useful information. These extra bits take up bandwidth.

Data bits are sent with a predefined frequency, the baud rate. Both the transmitter and receiver must be programmed to use the same bit frequency. After the first bit is received, the receiver calculates at which moments the other data bits will be received. It will check the line voltage levels at those moments.

With RS232, the line voltage level can have two states. The on state is also known as mark, the off state as space. No other line states are possible. When the line is idle, it is kept in the mark state.

Start bit

RS232 defines an asynchronous type of communication. This means that sending a data word can start at any moment. If starting at any moment is possible, this can pose some problems for the receiver to know which is the first bit to receive. To overcome this problem, each data word starts with an attention bit. This attention bit, also known as the start bit, is always identified by the space line level. Because the line is in mark state when idle, the start bit is easily recognized by the receiver.

Data bits

Directly following the start bit, the data bits are sent. A bit value 1 causes the line to go in mark state, the bit value 0 is represented by a space. The least significant bit is always the first bit sent.

Parity bit

For error detecting purposes, it is possible to add an extra bit to the data word automatically. The transmitter calculates the value of the bit depending on the information sent. The receiver performs the same calculation, and checks if the actual parity bit value corresponds to the calculated value. This is further discussed in another paragraph.

Stop bits

Suppose that the receiver has missed the start bit because of noise on the transmission line. It starts on the first following data bit with a space value. This causes garbled date to reach the receiver. A mechanism must be present to resynchronize the communication. To do this, framing is introduced. Framing means that all the data bits and parity bit are contained in a frame of start and stop bits. The period of time lying between the start and stop bits is a constant defined by the baud rate and the number of data and parity bits. The start bit always has a space value, the stop bit always a mark value. If the receiver detects a value other than mark when the stop bit should be present on the line, it knows that there is a synchronization failure. This causes a framing error condition in the receiving UART. The device then tries to resynchronize on new incoming bits.

For resynchronizing, the receiver scans the incoming data for valid start and stop bit pairs. This works as long as there is enough variation in the bit patterns of the data words. If a data value of zero is sent repeatedly, resynchronization is not possible, for example.

The stop bit identifying the end of a data frame can have different lengths. Actually, it is not a real bit, but a minimum period of time the line must be idle (mark state) at the end of each word. On PCs this period can have three lengths: the time equal to 1, 1.5, or 2 bits. 1.5 bits is only used with data words of 5 bits length, and 2 only for longer words. A stop bit length of 1 bit is possible for all data word sizes.

RS232 physical properties

The RS232 standard describes a communication method capable of communicating in different environments. This has had its impact on the maximum allowable voltages etc., on the pins. In the original definition, the technical possibilities of that time were taken into account. The maximum baud rate defined for example is 20 kbps. With current devices like the 16550A UART, maximum speeds of 1.5 Mbps are allowed.

Voltages

The signal level of the RS232 pins can have two states. A high bit or mark state is identified by a negative voltage, and a low bit or space state uses a positive value. This might be a bit confusing, because in normal circumstances, high logical values are defined by high voltages also. The voltage limits are shown below.

RS232 voltage values:

Level

Transmitter
capable (V)

Receiver
capable (V)

Space state (0)

+5 ... +15

+3 ... +25

Mark state (1)

-5 ... -15

-3 ... -25

Undefined

-

-3 ... +3

More information about the voltage levels of RS232 and other serial interfaces can be found in the interface comparison table.

The maximum voltage swing the computer can generate on its port can have influence on the maximum cable length and communication speed that is allowed. Also, if the voltage difference is small, data distortion will occur sooner. For example, my Toshiba laptop mark's voltage is -9.3 V, compared to -11.5 V on my desktop computer. The laptop has difficulties to communicate with Mitsubishi PLCs in industrial environments with high noise levels, where as a desktop computer has no data errors at all using the same cable. Thus, even far beyond the minimum voltage levels, an extra 2 volts can make a huge difference in communication quality.

Despite the high voltages present, it is not possible to destroy the serial port by short circuiting. Only, applying external voltages with high currents may eventually burn out the driver chips. Still then, the UART won't be damaged in most cases.

Maximum cable lengths

Cable length is one of the most discussed items in the RS232 world. The standard has a clear answer: the maximum cable length is 50 feet, or the cable length equal to a capacitance of 2500 pF. The latter rule is often forgotten. This means that using a cable with low capacitance allows you to span longer distances without going beyond the limitations of the standard. If for example a UTP CAT-5 cable is used with a typical capacitance of 17 pF/ft, the maximum allowed cable length is 147 feet.

The cable length mentioned in the standard allows maximum communication speed to occur. If speed is reduced by a factor of 2 or 4, the maximum length increases dramatically. Texas Instruments has done some practical experiments years ago at different baud rates to test the maximum allowed cable lengths. Keep in mind that the RS232 standard was originally developed for 20 kbps. By halving the maximum communication speed, the allowed cable length increases by a factor of ten!

RS232 cable length according to Texas Instruments

Maximum cable length (ft)

19200
50
9600
500
4800
1000
2400
3000

Error detection

One way of detecting errors was already discussed. It is the frame detection mechanism which is used to test if the incoming bits are properly surrounded by a start and stop bit pair. For further error checking, a parity bit can be used. The use of this bit is, however, not mandatory. If the existence of wrong bits is rare (when communicating with an internal modem, for example), or if a higher level protocol is used for error detection and correction (Z-modem, RAS, etc.), communication speed can be increased by not using the parity feature present on the UART.

Parity is a simple way to encode a data word to have a mechanism to detect an error in the information. The method used with serial communications adds one bit to each data word. The value of this bit depends on the value of the data word. It is necessary that both the transmitter and receiver use the same algorithm to calculate the value of the parity bit. Otherwise, the receiver may detect errors which are not present.

Even parity

Basically, the parity bit can be calculated in two ways. When even parity is used, the number of information bits sent will always contain an even number of logical 1s. If the number of high data bits is odd, a high value parity bit is added; otherwise, a low bit will be used.

Odd parity

The odd parity system is quite similar to the even parity system, but in this situation, the number of high bits will always be odd.

Disadvantages of the parity system

The parity system using one bit for each data word is not capable of finding all errors. Only errors which cause an odd number of bits to flip will be detected. The second problem is that there is no way to know which bit is false. If necessary, a higher level protocol is necessary to inform the sender that this information must be resent. Therefore, on noisy lines, often other detection systems are used to assure that the sent information is received correctly.

History

You can find more updates and more projects on my blog.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Metaconta
Software Developer (Junior) Electronics PIC
Spain Spain
http://electronica-pic.blogspot.com
 
I like the Visual Studio programming related to electronic and PIC microcontroller.

Comments and Discussions

 
Generalopen an excel template with microsoft visual c# express 2008 Pinmemberlena241518-Feb-11 0:25 
GeneralRe: open an excel template with microsoft visual c# express 2008 PinmemberMetaconta18-Feb-11 2:22 
GeneralRe: open an excel template with microsoft visual c# express 2008 Pinmemberlena241520-Feb-11 4:26 
GeneralRe: open an excel template with microsoft visual c# express 2008 PinmemberMetaconta20-Feb-11 7:10 
Generalcode in VB.NET PinmemberMember 1031595017-Oct-13 11:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 11 Dec 2009
Article Copyright 2009 by Metaconta
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid