`

C#串口发送接受数据

    博客分类:
  • C#
阅读更多
发送串口数据:

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

namespace SendData
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort port = new SerialPort();

            Console.WriteLine("串口(如COM1):");
            port.PortName = Console.ReadLine();

            Console.WriteLine("波特率:");
            port.BaudRate = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("数据位:");
            port.DataBits = Convert.ToInt32(Console.ReadLine());

            int stopBits = 0;
            Console.WriteLine("停止位:");
            stopBits = Convert.ToInt32(Console.ReadLine());
            switch (stopBits)
            {
                case 0:
                    port.StopBits = StopBits.None;
                    break;
                case 1:
                    port.StopBits = StopBits.One;
                    break;
                case 2:
                    port.StopBits = StopBits.Two;
                    break;
                default:
                    port.StopBits = StopBits.None;
                    break;
            }

            try
            {
                port.Open();
                string sendData="";
                bool exitFlag=false;
                while (exitFlag == false)
                {
                    Console.WriteLine("要发送的数据:");
                    sendData = Console.ReadLine();
                    if (sendData.CompareTo("exit") == 0) 
                        break;
                    else
                        port.WriteLine(sendData);
                }
                port.Close();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
}
接受串口数据:

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

namespace RecvData
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort port = new SerialPort();

            Console.WriteLine("串口(如COM1):");
            port.PortName = Console.ReadLine();

            Console.WriteLine("波特率:");
            port.BaudRate = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("数据位:");
            port.DataBits = Convert.ToInt32(Console.ReadLine());

            int stopBits = 0;
            Console.WriteLine("停止位:");
            stopBits = Convert.ToInt32(Console.ReadLine());
            switch (stopBits)
            {
                case 0:
                    port.StopBits = StopBits.None;
                    break;
                case 1:
                    port.StopBits = StopBits.One;
                    break;
                case 2:
                    port.StopBits = StopBits.Two;
                    break;
                default:
                    port.StopBits = StopBits.None;
                    break;
            }

            try
            {
                port.Open();
                bool exitFlag = false;
                int n = 0;
                while (exitFlag == false)
                {
                    Console.WriteLine(port.ReadLine());
                    n++;
                    if (n == 999999)
                    {
                        Console.WriteLine("是否继续?(y/s)");
                        string ans = Console.ReadLine();
                        if (ans.CompareTo("y") == 0)
                            exitFlag = true;
                        else
                            n = 0;
                    }
                }
                port.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics