实例介绍
【实例截图】

【核心代码】
/**
* Copyright (c) 2014-2015, Wenhuix, All rights reserved.
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of COMTOOL nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.ComponentModel;
using System.Threading;
namespace COMTOOL
{
public delegate void SerialPortEventHandler(Object sender, SerialPortEventArgs e);
public class SerialPortEventArgs : EventArgs
{
public bool isOpend = false;
public Byte[] receivedBytes = null;
}
public class ComModel
{
private SerialPort sp = new SerialPort();
public event SerialPortEventHandler comReceiveDataEvent = null;
public event SerialPortEventHandler comOpenEvent = null;
public event SerialPortEventHandler comCloseEvent = null;
private Object thisLock = new Object();
/// <summary>
/// When serial received data, will call this method
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (sp.BytesToRead <= 0)
{
return;
}
//Thread Safety explain in MSDN:
// Any public static (Shared in Visual Basic) members of this type are thread safe.
// Any instance members are not guaranteed to be thread safe.
// So, we need to synchronize I/O
lock (thisLock)
{
int len = sp.BytesToRead;
Byte[] data = new Byte[len];
try
{
sp.Read(data, 0, len);
}
catch (System.Exception)
{
//catch read exception
}
SerialPortEventArgs args = new SerialPortEventArgs();
args.receivedBytes = data;
if (comReceiveDataEvent != null)
{
comReceiveDataEvent.Invoke(this, args);
}
}
}
/// <summary>
/// Send bytes to device
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public bool Send(Byte[] bytes)
{
if (!sp.IsOpen)
{
return false;
}
try
{
sp.Write(bytes, 0, bytes.Length);
}
catch (System.Exception)
{
return false; //write failed
}
return true; //write successfully
}
/// <summary>
/// Open Serial port
/// </summary>
/// <param name="portName"></param>
/// <param name="baudRate"></param>
/// <param name="dataBits"></param>
/// <param name="stopBits"></param>
/// <param name="parity"></param>
/// <param name="handshake"></param>
public void Open(string portName, String baudRate,
string dataBits, string stopBits, string parity,
string handshake)
{
if (sp.IsOpen)
{
Close();
}
sp.PortName = portName;
sp.BaudRate = Convert.ToInt32(baudRate);
sp.DataBits = Convert.ToInt16(dataBits);
/**
* If the Handshake property is set to None the DTR and RTS pins
* are then freed up for the common use of Power, the PC on which
* this is being typed gives 10.99 volts on the DTR pin & 10.99
* volts again on the RTS pin if set to true. If set to false
* it gives -9.95 volts on the DTR, -9.94 volts on the RTS.
* These values are between 3 to 25 and -3 to -25 volts this
* give a dead zone to allow for noise immunity.
* http://www.codeproject.com/Articles/678025/Serial-Comms-in-Csharp-for-Beginners
*/
if (handshake == "None")
{
//Never delete this property
sp.RtsEnable = true;
sp.DtrEnable = true;
}
SerialPortEventArgs args = new SerialPortEventArgs();
try
{
sp.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits);
sp.Parity = (Parity)Enum.Parse(typeof(Parity), parity);
sp.Handshake = (Handshake)Enum.Parse(typeof(Handshake), handshake);
sp.WriteTimeout = 1000; /*Write time out*/
sp.Open();
sp.DataReceived = new SerialDataReceivedEventHandler(DataReceived);
args.isOpend = true;
}
catch (System.Exception)
{
args.isOpend = false;
}
if (comOpenEvent != null)
{
comOpenEvent.Invoke(this, args);
}
}
/**
* Take care to avoid deadlock when calling Close on the SerialPort
* in response to a GUI event.
* An app involving the UI and the SerialPort freezes up when closing the SerialPort
* Deadlock can occur if Control.Invoke() is used in serial port event handlers
*
* The typical scenario we encounter is occasional deadlock in an app
* that has a data received handler trying to update the GUI at the
* same time the GUI thread is trying to close the SerialPort (for
* example, in response to the user clicking a Close button).
*
* The reason deadlock happens is that Close() waits for events to
* finish executing before it closes the port. You can address this
* problem in your apps in two ways:
*
* (1)In your event handlers, replace every Control.Invoke call with
* Control.BeginInvoke, which executes asynchronously and avoids
* the deadlock condition. This is commonly used for deadlock avoidance
* when working with GUIs.
*
* (2)Call serialPort.Close() on a separate thread. You may prefer this
* because this is less invasive than updating your Invoke calls.
*/
/// <summary>
/// Close serial port
/// </summary>
public void Close()
{
Thread closeThread = new Thread(new ThreadStart(CloseSpThread));
closeThread.Start();
}
/// <summary>
/// Close serial port thread
/// </summary>
private void CloseSpThread()
{
SerialPortEventArgs args = new SerialPortEventArgs();
args.isOpend = false;
try
{
sp.Close(); //close the serial port
sp.DataReceived -= new SerialDataReceivedEventHandler(DataReceived);
}
catch (Exception)
{
args.isOpend = true;
}
if (comCloseEvent != null)
{
comCloseEvent.Invoke(this, args);
}
}
}
}
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论