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

推荐订阅源

WordPress大学
WordPress大学
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
GbyAI
GbyAI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园_首页
D
Docker
S
Security @ Cisco Blogs
K
Kaspersky official blog
爱范儿
爱范儿
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
V
V2EX
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Troy Hunt's Blog
Cloudbric
Cloudbric
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Hacker News
The Hacker News
美团技术团队
S
SegmentFault 最新的问题
L
Lohrmann on Cybersecurity
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Martin Fowler
Martin Fowler
Google Online Security Blog
Google Online Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tor Project blog
Vercel News
Vercel News
The Cloudflare Blog
G
Google Developers Blog
T
Threat Research - Cisco Blogs
AI
AI
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
Scott Helme
Scott Helme
S
Schneier on Security
大猫的无限游戏
大猫的无限游戏
The GitHub Blog
The GitHub Blog
S
Securelist
IT之家
IT之家
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 昨夜飘风

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