C#多线程串口通信

上传者: shangsongwww | 上传时间: 2021-09-06 18:03:34 | 文件大小: 67KB | 文件类型: ZIP
这是一个非常经典的C#串口多线程实例,把部分代码发送来让大家看看 using System; using System.IO; using System.IO.Ports; using System.Collections; using System.Threading; namespace Termie { /// CommPort class creates a singleton instance /// of SerialPort (System.IO.Ports) /// When ready, you open the port. /// /// CommPort com = CommPort.Instance; /// com.StatusChanged += OnStatusChanged; /// com.DataReceived += OnDataReceived; /// com.Open(); /// /// Notice that delegates are used to handle status and data events. /// When settings are changed, you close and reopen the port. /// /// CommPort com = CommPort.Instance; /// com.Close(); /// com.PortName = "COM4"; /// com.Open(); /// /// public sealed class CommPort { SerialPort _serialPort; Thread _readThread; volatile bool _keepReading; //begin Singleton pattern static readonly CommPort instance = new CommPort(); // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static CommPort() { } CommPort() { _serialPort = new SerialPort(); _readThread = null; _keepReading = false; } public static CommPort Instance { get { return instance; } } //end Singleton pattern //begin Observer pattern public delegate void EventHandler(string param); public EventHandler StatusChanged; public EventHandler DataReceived; //end Observer pattern private void StartReading() { if (!_keepReading) { _keepReading = true; _readThread = new Thread(ReadPort); _readThread.Start(); } } private void StopReading() { if (_keepReading) { _keepReading = false; _readThread.Join(); //block until exits _readThread = null; } }

文件下载

资源详情

[{"title":"( 21 个子文件 67KB ) C#多线程串口通信","children":[{"title":"Termie","children":[{"title":"Form1.Designer.cs <span style='color:#111;'> 30.10KB </span>","children":null,"spread":false},{"title":"Form2.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"license.txt <span style='color:#111;'> 1.43KB </span>","children":null,"spread":false},{"title":"Program.cs <span style='color:#111;'> 498B </span>","children":null,"spread":false},{"title":"Settings.cs <span style='color:#111;'> 4.01KB </span>","children":null,"spread":false},{"title":"IniFile.cs <span style='color:#111;'> 1.58KB </span>","children":null,"spread":false},{"title":"Termie.sln <span style='color:#111;'> 905B </span>","children":null,"spread":false},{"title":"AboutBox.Designer.cs <span style='color:#111;'> 10.29KB </span>","children":null,"spread":false},{"title":"AboutBox.cs <span style='color:#111;'> 4.76KB </span>","children":null,"spread":false},{"title":"Form2.cs <span style='color:#111;'> 5.18KB </span>","children":null,"spread":false},{"title":"Form1.cs <span style='color:#111;'> 17.31KB </span>","children":null,"spread":false},{"title":"Termie.ico <span style='color:#111;'> 80.79KB </span>","children":null,"spread":false},{"title":"Form2.Designer.cs <span style='color:#111;'> 15.40KB </span>","children":null,"spread":false},{"title":"Form1.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"Termie.csproj <span style='color:#111;'> 4.40KB </span>","children":null,"spread":false},{"title":"AboutBox.resx <span style='color:#111;'> 48.55KB </span>","children":null,"spread":false},{"title":"CommPort.cs <span style='color:#111;'> 6.34KB </span>","children":null,"spread":false},{"title":"Properties","children":[{"title":"Resources.resx <span style='color:#111;'> 5.48KB </span>","children":null,"spread":false},{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.37KB </span>","children":null,"spread":false},{"title":"Settings.Designer.cs <span style='color:#111;'> 1.07KB </span>","children":null,"spread":false},{"title":"Resources.Designer.cs <span style='color:#111;'> 2.77KB </span>","children":null,"spread":false}],"spread":false}],"spread":false}],"spread":true}]

评论信息

  • u014721944 :
    这个可以用,非常好
    2014-06-10
  • xiaozhulo :
    忽悠人的了
    2014-02-16
  • lxc72705160 :
    还行,可以参考一下
    2014-02-08

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明