惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
MyScale Blog
MyScale Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
N
Netflix TechBlog - Medium
MongoDB | Blog
MongoDB | Blog
I
InfoQ
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security

博客园 - 昨夜飘风

WPF 通过共享内存播放视频 C# 模拟网页拖拽 .net4.0中wpf单例启动 wpf 自定义ContextMenu样式,可以调节ContextMenu的宽度 Oracle 开发经验 WPF跑马灯效果 android私有文件夹的访问 [转]C#内存操作 C# 制作外挂常用的API silverlight 动画 性能优化 SilverLight 动画缓冲(Animation Easing) - 昨夜飘风 体面地处理程序的未捕获异常 C# 访问USB(HID)设备 收藏一个地址(居然说我标题已经存在) 收藏一个地址 (2009-06-24) SqlCommandBuilder使用的注意事项 助睡眠音乐 (2009-06-22) 班德瑞官方试听网站 (2009-06-18)今日收获 工厂方法 (2009-06-17)今日收获-只读列隐藏-win2003 dll文件下载
C#串口发送接受数据
昨夜飘风 · 2010-04-22 · via 博客园 - 昨夜飘风

发送串口数据:

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();
            }
        }
    }
}