常州市润邦电子科技有限公司

热保护器测试专业供应商

 

C# 串口

Serial Communication using C# and Whidbey

By Tapan Dantre | 20 Oct 2004
In this article, I will give you an introduction on how to do serial port communication on .NET platform using C#.

Introduction

In this article, I will give you an introduction on how to do serial port communication on .NET platform using C#. The .NET framework version 2.0 (beta) provides features for serial communication. The framework provides System.IO.Ports namespace. The new framework provides classes with the ability to access the serial ports on a computer, and to communicate with serial I/O devices. We will be using RS 232 C standard for communication between PCs. In full duplex mode, here I am not going to use any handshaking or flow control, I will use null modem connection for communication.

The Namespace

The System.IO.Ports namespace contains classes for controlling serial ports. The most important class is the SerialPort class.

The SerialPort Class

SerialPort class provides a framework for synchronous and event-driven I/O, access to pin and break states, and access to serial driver properties. It can be used to wrap Stream objects, allowing the serial port to be accessed by classes that use streams. That is, SerialPort class represents a serial port resource.

Creating SerialPort Object

By creating an object of this type, we will be able to programmatically control all aspects of serial communication.

The methods of SerialPort class that we will use are:

  • ReadLine(): Reads up to the NewLine value in the input buffer. If a New Line is not found before timeout, this method returns a null value.
  • WriteLine(string): Writes the specified string and the New Line value to the output buffer. The written output includes the New Line string.
  • Open(): Opens a new serial port connection.
  • Close(): Closes the port connection.

To create a SerialPort object, all we need to do is:

Collapse | Copy Code
//create a Serial Port object
SerialPort sp = new SerialPort ();

In all, there are seven public constructors for creating SerialPort. But I am using the parameter less constructor, the constructor uses default property values. For example, the value of DataBits defaults to 8, and StopBits defaults to 1. The default communication port will be COM1.

The public properties of SerialPort class that we will use are:

  • BaudRate: Gets or sets the serial baud rate.
  • StopBits: Gets or sets the standard number of stopbits per byte.
  • ReadTimeout: Gets or sets the number of milliseconds before a timeout occurs when a read operation does not finish.

There are many public properties, but except these three, all properties will have default values.

About Serial Port (Hardware)

The serial port on your PC is a full-duplex device meaning that it can send and receive data at the same time. In order to be able to do this, it uses separate lines for transmitting and receiving data. Some types of serial devices support only one-way communication, and therefore, use only two wires in the cable - the transmit line and the signal ground.

Data Transfer

In serial communication, a byte of data is transferred through a single wire one bit at a time. The packets contain a start bit, data, and stop bit. Once the start bit has been sent, the transmitter sends the actual data bits. There may either be 5, 6, 7, or 8 data bits, depending on the number you have selected. Both receiver and the transmitter must agree on the number of data bits, as well as the baud rate.

Null Modem

A Null Modem cable simply crosses the receive and transmit lines so that transmit on one end is connected to receive on the other end and vice versa. In addition to transmit and receive, DTR & DSR, as well as RTS & CTS are also crossed in a Null Modem connection.

Here is the pin diagram:

Sample screenshot

As we are not using any handshake, we will use three wire connections in which we will connect pin 2 of one connector to pin 3 of the other. For both the connectors, connect pin 5 (ground) of both the connectors with each other (common ground).

If you want, you can use only one PC as both transmitter and receiver for this communication. Then, just take a DB 9 connector and a small wire, and connect pin no 2 and 3 using the wire that will connect a loop back. That is, whatever you will send, the same data you will be receiving.

The Example Application

The main form:

Sample screenshot

Here, if you want to work with default values for the public properties, then press Save Status. If you want to change the value for specified properties, then press Property.

Then the following dialog will pop:

Screenshot - fig2.JPG

Here, you can select values for baud rate and stop bit. To save these changes, press OK.

If you directly press save status or save the changed values for properties, the following form will be displayed:

Sample screenshot

Which will display the values for some of the properties.

For starting communication, press Start Comm. As soon as you press the button, the following form will be displayed:

Screenshot - fig4.JPG

Type data in textbox which is to be transferred, followed by a new line, and press Send button. As soon as you press the button, the data will be send to the send buffer and later to the COM port.

For reading the data form COM port, press Read button. If there is any data in read buffer, it will be displayed in the textbox. If there no data to be read for 500 ms, then an error message will be displayed informing the same.

Here is a code example for the above application:

Code for the Main app

Collapse | Copy Code
#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
 
#endregion
 
namespace Serialexpample
{
    partial class Form1 : Form
    {
        //create instance of property page
        //property page is used to set values for stop bits and 
        //baud rate

        PropertyPage pp = new PropertyPage();

        //create an Serial Port object
        SerialPort sp = new SerialPort();

        public Form1()
        {
            InitializeComponent();
        }
            
        private void propertyButton_Click(object sender, EventArgs e)
        {
            //show property dialog
            pp.ShowDialog();

            propertyButton.Hide();
        }

        private void sendButton_Click(object sender, EventArgs e)
        {
            try
            {
                //write line to serial port
                sp.WriteLine(textBox.Text);
                //clear the text box
                textBox.Text = "";
            }
            catch (System.Exception ex)
            {
                baudRatelLabel.Text = ex.Message;
            }

        }

        private void ReadButton_Click(object sender, EventArgs e)
        {
            try
            {
                //clear the text box
                textBox.Text = "";
                //read serial port and displayed the data in text box
                textBox.Text = sp.ReadLine();
            }
            catch(System.Exception ex)
            {
                baudRatelLabel.Text = ex.Message;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("Do u want to Close the App");
            sp.Close();
        }

        private void startCommButton_Click(object sender, EventArgs e)
        {
            startCommButton.Hide();
            sendButton.Show();
            readButton.Show();
            textBox.Show();
        }

        //when we want to save the status(value)
        private void saveStatusButton_Click_1(object sender, EventArgs e)
        {
            //display values
            //if no property is set the default values
            if (pp.bRate == "" && pp.sBits == "")
            {
                dataBitLabel.Text = "BaudRate = " + sp.BaudRate.ToString();
                readTimeOutLabel.Text = "StopBits = " + sp.StopBits.ToString();
            }
            else
            {
                dataBitLabel.Text = "BaudRate = " + pp.bRate;
                readTimeOutLabel.Text = "StopBits = " + pp.sBits;
            }

            parityLabel.Text = "DataBits = " + sp.DataBits.ToString();
            stopBitLabel.Text = "Parity = " + sp.Parity.ToString();
            readTimeOutLabel.Text = "ReadTimeout = " + 
                      sp.ReadTimeout.ToString();

            if (propertyButton.Visible == true)
                propertyButton.Hide();
            saveStatusButton.Hide();
            startCommButton.Show();

            try
            {
                //open serial port
                sp.Open();
                //set read time out to 500 ms
                sp.ReadTimeout = 500;
            }
            catch (System.Exception ex)
            {
                baudRatelLabel.Text = ex.Message;
            }
        }
    }
}

Code for Property Dialog

Collapse | Copy Code
#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
#endregion
 
namespace Serialexpample
{
    partial class PropertyPage : Form
    {
        //variables for storing values of baud rate and stop bits
        private string baudR="";
        private string stopB="";

        //property for setting and getting baud rate and stop bits
        public string bRate
        {
            get
            {
                return baudR;
            }
            set
            {
                baudR = value;
            }
        }

        public string sBits
        {
            get
            {
                return stopB;
            }
            set
            {
                stopB = value;
            }
        }

        public PropertyPage()
        {
            InitializeComponent();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.bRate = "";
            this.sBits = "";
            //close form
            this.Close();
        }

        private void okButton_Click_1(object sender, EventArgs e)
        {
            //here we set the value for stop bits and baud rate.
            this.bRate = BaudRateComboBox.Text;
            this.sBits = stopBitComboBox.Text;
            //
            this.Close();

        }
    }
}

Note:

  • Both receiver and the transmitter must agree on the number of data bits, as well as the baud rate.
  • All the connection for the DB 9 connector should be done accordingly.
  • The article is based on pre-released documentation (.NET Framework 2.0 Beta) and is subject to change in future release.

License

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

About the Author

Tapan Dantre

Web Developer

India India

Member


Sign Up to vote for this article
Add a reason or comment to your vote: x

Comments and Discussions

You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  Msgs 1 to 10 of 248 (Total in Forum: 248) (Refresh) FirstPrevNext
General Any Serial Port member Orhan Albay 2:59 21 Oct '10  
Hello everybody,

I have developed a serial port programming language.

It is a handy, simple serial port programming language specially designed to simplify the development of RS232 based applications. It makes it easy not only to communicate with the serial port but also data parsing and extraction.

It is opensource and I appreciate if you test and let me know your thoughts.
You can download the application from the following link:

http://sourceforge.net/projects/anyserialport/[^]

And here is the user guide link:

http://www.anyserialport.com/help[^]

Thank you for taking your time.

Orhan
General My vote of 4 member HimanshuJain 0:28 13 Oct '10  
Article looks excellent but I am not able to see source code for download here
General My vote of 5 member srinivasks33 6:23 11 Oct '10  
good one
General My vote of 1 member Serja Ru 21:48 9 Jul '10  
Bad!
General Device not responding member Riho Pihlak 14:31 2 Apr '10  
How to detect if the device does not respond to a sent message? Are there any examples?
General printing problems member yogeshptl 22:02 14 Dec '09  
sir my printer leave spaces while printing?
General My vote of 2 member funnydaredevil 6:28 26 Oct '09  
so simple things in so laborious code
General hello, i have a problem reading the COM1 member ablanco93 11:18 4 Sep '09  
Hello, first of all thank you for your tutorial, it has been very helpful to me, thanks. So i made my own code from the information that you gave us. The problem that i have, it's that i can't read anything from the port, i can write, but not read, i realized that i can write because i use this program http://www.eltima.com/products/serial-port-monitor/[^] and, in this program, i can see what i'm writing. I check my wired and it works fine sending and reciving with the program that i downloaded, but with my program do not work. So, here's my code, and thanks for the time and everything.

private void write_button(object sender, EventArgs e)
{ Smile
//configuring the serial port
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
//opening the serial port
serialPort1.Open();
//write data to serial port Writes the specified string and the New Line value to the output buffer.
//The written output includes the New Line string.
serialPort1.Write(textBox1.Text);
//close the port
textBox1.Text = "";
serialPort1.Close();

}

private void read_button(object sender, EventArgs e)
{

//open serial port
serialPort1.Open();
//set read time out to 500 ms
serialPort1.ReadTimeout = 500;
//clear the text box

try
{
//clear the text box
textBox2.Text = "";
//reads characters from the input buffer until the NewLine character is encountered;
//read serial port and displayed the data in text box
textBox2.Text = serialPort1.ReadLine();
serialPort1.Close();
}
catch (System.Exception ex)
{
MessageBox.Show("Can't read");
}


}
Smile Thumbs Up
Answer Re: hello, i have a problem reading the COM1 member Tapan Dantre 22:51 7 Sep '09  
1) Are getting exception
2) Do the serial port config(stmt below) in init or load method
//configuring the serial port
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;

3) Instead of serialPort1.ReadLine(); try using ReadExisting() method.

This might help

Thanks,

Tapan
General Re: hello, i have a problem reading the COM1 member jawed.ace 9:26 9 Apr '10  
Hi,
Tapan Dantre thanks for your articles and answer.
i would like to add few point in the above answer.
please check the BaudRate also.
cuz sometimes the device connected through serial port would be having different Baud rate.
i have faced the same issue.then i figured out that the baudrate 9600 is not for my device
then i set it as 115200 and its started communicating through serial port.
Thanks!!

Md.Jawed
http://jawedm.blogspot.com

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Oct 2004

Copyright 2004 by Tapan Dantre
Everything else Copyright © CodeProject, 1999-2010
Web18 | Advertise on the Code Project

posted on 2010-11-08 16:33  常州市润邦电子科技  阅读(848)  评论(0编辑  收藏  举报

导航