C#串口发送接受数据 _.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C#串口发送接受数据

C#串口发送接受数据

 2010/11/16 11:45:55  devilhand  http://devilhand.javaeye.com  我要评论(0)
  • 摘要:发送串口数据:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO.Ports;namespaceSendData{classProgram{staticvoidMain(string[]args){SerialPortport=newSerialPort();Console.WriteLine("串口(如COM1):");port
  • 标签: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();
            }
        }
    }
}

?

上一篇: C# 制作外挂常用的API 下一篇: rss 订阅
发表评论
用户名: 匿名