C#多线程串口通信

4星(超过85%的资源)
所需积分/C币:50 2013-12-15 21:14:47 67KB ZIP
237
收藏 收藏
举报

这是一个非常经典的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; } }

...展开详情
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
u014721944 这个可以用,非常好
2014-06-10
回复
suners 忽悠人的了
2014-02-16
回复
lxc72705160 还行,可以参考一下
2014-02-08
回复
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
  • 领英

  • GitHub

  • 脉脉勋章

  • 签到王者

  • 技术圈认证(专家版)

关注 私信
上传资源赚钱or赚积分
最新推荐
C#多线程串口通信 50积分/C币 立即下载
1/0