communicating-with-serial-port-in-C-Sharp

转载:http://www.c-sharpcorner.com/uploadfile/eclipsed4utoo/communicating-with-serial-port-in-C-Sharp/


This article will demonstrate how to write and receive data from a device connected to a serial port in C# and .NET. We will be writing the received data to a TextBox on a form, so this will also deal with threading.

In the past, to communicate with a Serial Port using .Net 1.1, you had to either use the Windows API, or use a third-party control. With .Net 2.0, Microsoft added this support with the inclusion of the SerialPort class as part of the System.IO.Ports namespace. Implementation of the SerialPort class is very straight-forward. To create an instance of the SerialPort class, you simply pass the SerialPort options to the constructor of the class:

// all of the options for a serial device

    // ---- can be sent through the constructor of the SerialPort class

    // ---- PortName = "COM1", Baud Rate = 19200, Parity = None,

    // ---- Data Bits = 8, Stop Bits = One, Handshake = None

    SerialPort _serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);

    _serialPort.Handshake = Handshake.None;

 

To receive data, we will need to create an EventHandler for the "SerialDataReceivedEventHandler":

// "sp_DataReceived" is a custom method that I have created

    _serialPort.DataReceived += newSerialDataReceivedEventHandler(sp_DataReceived);

You can also set other options, such as the ReadTimeout and WriteTimeout:

// milliseconds _serialPort.ReadTimeout = 500;

_serialPort.WriteTimeout = 500;

Once you are ready to use the Serial Port, you will need to open it: 

// Opens serial port 
_serialPort.Open(); 

Now we are ready to receive data. However, to write this data to the TextBox on a form, we need to create a delegate. .Net does not allow cross-thread action, so we need to use a delegate. The delegate is used to write to the UI thread from a non-UI thread.

// delegate is used to write to a UI control from a non-UI thread

private delegate void SetTextDeleg(string text);

We will now create the "sp_DataReceived" method that will be executed when data is received through the serial port:

void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)

{

    Thread.Sleep(500);

    string data = _serialPort.ReadLine();

    // Invokes the delegate on the UI thread, and sends the data that was received to the invoked method.

    // ---- The "si_DataReceived" method will be executed on the UI thread which allows populating of the textbox.

    this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });

}

Now we create our "si_DataReceived" method:

private void si_DataReceived(string data) { textBox1.Text = data.Trim(); }

We can now receive data from a serial port device and display it on a form. Some devices will send data without being prompted. However, some devices need to be send certain commands, and it will reply with the data that the command calls for. For these devices, you will write data to the serial port, and use the previous code to get the data that will be sent back. In my example, I will be communicating with a scale. For this particular scale, sending the command "SI\r\n" will force it to return the weight of whatever is on the scale. This command is specific for this scale. You will need to read the documentation of your serial device to find commands that it will receive. To write to the serial port, I have created a "Start" button on the form. I have added code to it's Click_Event:

private void btnStart_Click(object sender, EventArgs e)

{

    // Makes sure serial port is open before trying to write

    try

    {

        if(!(_serialPort.IsOpen))

        _serialPort.Open();

        _serialPort.Write("SI\r\n");

    }

    catch (Exception ex)

    {

        MessageBox.Show("Error opening/writing to serial port :: " + ex.Message,"Error!");

    }

}

And that is all you need to do. I have attached the Visual Studio 2005 solution.

Article Extensions
Contents added by  Arjun Walmiki on  Dec 04, 2012
this is my code but not receive data from com port:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.IO;

namespace PortChat
{
    public class PortChat
    {
       // private static StreamWriter sw = new StreamWriter("filename.txt");
        static bool _continue;
        static SerialPort _serialPort;
        
        public static void Main(string[] args)
        {
            string name;
            string message;
            //string path = "";
            Write
            //string path = "example.txt";
            //string[] myMenu = { "A", "B", "C" };
            //File.WriteAllLines(path, myMenu);

            Read
            //string[] readMenu = File.ReadAllLines(path);
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);
            
            // Create a new SerialPort object with default settings.
            _serialPort = new SerialPort();

            // Allow the user to set the appropriate properties.
            _serialPort.PortName = SetPortName(_serialPort.PortName);
            _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
            _serialPort.Parity = SetPortParity(_serialPort.Parity);
            _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
            _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
            _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

            // Set the read/write timeouts
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;
            _serialPort.Open();
            _continue = true;
            readThread.Start();
            _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
           
            

            //_serialPort.WriteLine("$");//Command to start Data Stream
            Wait for data or user input to continue.
            //Console.ReadLine();
           // Console.WriteLine("FILEPATH");
           // path = Console.ReadLine();
            Console.Write("Name: ");
            name = Console.ReadLine();
            Console.WriteLine("Type QUIT to exit");
            while (_continue)
            {
                message = Console.ReadLine();

                if (stringComparer.Equals("quit", message))
                {
                    _continue = false;
                }
                else
                {
                    _serialPort.WriteLine(String.Format("<{0}>: {1}", name, message));
                }
            }

            readThread.Join();
            _serialPort.Close();
        }
        static void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort _serialPort = (SerialPort)sender;
            Console.WriteLine(_serialPort.ReadExisting());
        }

        public static void Read()
        {
            while (_continue)
            {
                try
                {
        
                    string message = _serialPort.ReadLine();
                    Console.WriteLine(message);
                }
                catch (TimeoutException) { }
            }
        }
        public static string SetPortName(string defaultPortName)
        {
            string portName="";

            Console.WriteLine("Available Ports:");
            foreach (string s in SerialPort.GetPortNames())
            {
                Console.WriteLine("   {0}", s);
                portName = s;
            }
            //my code
            _serialPort.PortName = portName;
            defaultPortName = portName;
            Console.Write("COM port({0}): ", defaultPortName);
            portName = Console.ReadLine();

            if (portName == "")
            {
                portName = defaultPortName;
            }
            return portName;
        }
        public static int SetPortBaudRate(int defaultPortBaudRate)
        {
            string baudRate;

            Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
            baudRate = Console.ReadLine();

            if (baudRate == "")
            {
                baudRate = defaultPortBaudRate.ToString();
            }

            return int.Parse(baudRate);
        }
        public static Parity SetPortParity(Parity defaultPortParity)
        {
            string parity;

            Console.WriteLine("Available Parity options:");
            foreach (string s in Enum.GetNames(typeof(Parity)))
            {
                Console.WriteLine("   {0}", s);
            }

            Console.Write("Parity({0}):", defaultPortParity.ToString());
            parity = Console.ReadLine();

            if (parity == "")
            {
                parity = defaultPortParity.ToString();
            }

            return (Parity)Enum.Parse(typeof(Parity), parity);
        }
        
        public static int SetPortDataBits(int defaultPortDataBits)
        {
            string dataBits;

            Console.Write("Data Bits({0}): ", defaultPortDataBits);
            dataBits = Console.ReadLine();

            if (dataBits == "")
            {
                dataBits = defaultPortDataBits.ToString();
            }

                return int.Parse(dataBits);
        }
        public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
        {
            string stopBits;

            Console.WriteLine("Available Stop Bits options:");
            foreach (string s in Enum.GetNames(typeof(StopBits)))
            {
                Console.WriteLine("   {0}", s);
            }

            Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString());
            stopBits = Console.ReadLine();

            if (stopBits == "")
            {
                stopBits = defaultPortStopBits.ToString();
            }

            return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
        }
        public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
        {
            string handshake;

            Console.WriteLine("Available Handshake options:");
            foreach (string s in Enum.GetNames(typeof(Handshake)))
            {
                Console.WriteLine("   {0}", s);
            }

            Console.Write("Handshake({0}):", defaultPortHandshake.ToString());
            handshake = Console.ReadLine();

            if (handshake == "")
            {
                handshake = defaultPortHandshake.ToString();
            }

            return (Handshake)Enum.Parse(typeof(Handshake), handshake);
        }
    }
}
Contents added by  Mahesh Chand on  Aug 03, 2010
Here is complete code sample from MSDN.


 using System;
using System.IO.Ports;
using System.Threading;

public class PortChat
{
 static bool _continue;
 static SerialPort _serialPort;

 public static void Main()
    {
 string name;
 string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);

 // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

 // Allow the user to set the appropriate properties.
        _serialPort.PortName = SetPortName(_serialPort.PortName);
        _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
        _serialPort.Parity = SetPortParity(_serialPort.Parity);
        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

 // Set the read/write timeouts
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;

        _serialPort.Open();
        _continue = true;
        readThread.Start();

        Console.Write("Name: ");
        name = Console.ReadLine();

        Console.WriteLine("Type QUIT to exit");

 while (_continue)
        {
            message = Console.ReadLine();

 if (stringComparer.Equals("quit", message))
            {
                _continue = false;
            }
 else
            {
                _serialPort.WriteLine(
                    String.Format("<{0}>: {1}", name, message) );
            }
        }

        readThread.Join();
        _serialPort.Close();
    }

 public static void Read()
    {
 while (_continue)
        {
 try
            {
 string message = _serialPort.ReadLine();
                Console.WriteLine(message);
            }
 catch (TimeoutException) { }
        }
    }

 public static string SetPortName(string defaultPortName)
    {
 string portName;

        Console.WriteLine("Available Ports:");
 foreach (string s in SerialPort.GetPortNames())
        {
            Console.WriteLine("   {0}", s);
        }

        Console.Write("COM port({0}): ", defaultPortName);
        portName = Console.ReadLine();

 if (portName == "")
        {
            portName = defaultPortName;
        }
 return portName;
    }

 public static int SetPortBaudRate(int defaultPortBaudRate)
    {
 string baudRate;

        Console.Write("Baud Rate({0}): ", defaultPortBaudRate);
        baudRate = Console.ReadLine();

 if (baudRate == "")
        {
            baudRate = defaultPortBaudRate.ToString();
        }

 return int.Parse(baudRate);
    }

 public static Parity SetPortParity(Parity defaultPortParity)
    {
 string parity;

        Console.WriteLine("Available Parity options:");
 foreach (string s in Enum.GetNames(typeof(Parity)))
        {
            Console.WriteLine("   {0}", s);
        }

        Console.Write("Parity({0}):", defaultPortParity.ToString());
        parity = Console.ReadLine();

 if (parity == "")
        {
            parity = defaultPortParity.ToString();
        }

 return (Parity)Enum.Parse(typeof(Parity), parity);
    }

 public static int SetPortDataBits(int defaultPortDataBits)
    {
 string dataBits;

        Console.Write("Data Bits({0}): ", defaultPortDataBits);
        dataBits = Console.ReadLine();

 if (dataBits == "")
        {
            dataBits = defaultPortDataBits.ToString();
        }

 return int.Parse(dataBits);
    }

 public static StopBits SetPortStopBits(StopBits defaultPortStopBits)
    {
 string stopBits;

        Console.WriteLine("Available Stop Bits options:");
 foreach (string s in Enum.GetNames(typeof(StopBits)))
        {
            Console.WriteLine("   {0}", s);
        }

        Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString());
        stopBits = Console.ReadLine();

 if (stopBits == "")
        {
            stopBits = defaultPortStopBits.ToString();
        }

 return (StopBits)Enum.Parse(typeof(StopBits), stopBits);
    }

 public static Handshake SetPortHandshake(Handshake defaultPortHandshake)
    {
 string handshake;

        Console.WriteLine("Available Handshake options:");
 foreach (string s in Enum.GetNames(typeof(Handshake)))
        {
            Console.WriteLine("   {0}", s);
        }

        Console.Write("Handshake({0}):", defaultPortHandshake.ToString());
        handshake = Console.ReadLine();

 if (handshake == "")
        {
            handshake = defaultPortHandshake.ToString();
        }

 return (Handshake)Enum.Parse(typeof(Handshake), handshake);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牙科就诊管理系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线查看数据。管理员管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等功能。牙科就诊管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 管理员在后台主要管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等。 牙医列表页面,此页面提供给管理员的功能有:查看牙医、新增牙医、修改牙医、删除牙医等。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。药品管理页面,此页面提供给管理员的功能有:新增药品,修改药品,删除药品。药品类型管理页面,此页面提供给管理员的功能有:新增药品类型,修改药品类型,删除药品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值