添加项目文件。
This commit is contained in:
@@ -0,0 +1,239 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Runtime.Remoting.Messaging;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace SimpleCommunication
|
||||
{
|
||||
|
||||
public class Serial : IDisposable
|
||||
{
|
||||
#region DataMember & Ctor
|
||||
public SerialPort serialPort;
|
||||
private string[] port;
|
||||
public SerialPort Comm { get; private set; }
|
||||
private List<string> list = new List<string>(4096);
|
||||
|
||||
//在事务处理结束后才触发下列事件
|
||||
public event DlgNoParam ComOpenEvent;
|
||||
public event DlgOneParam<string> RecvMsgEvent;
|
||||
public event DlgOneParam<string> SendMsgEvent;
|
||||
public event DlgNoParam ComDisconnectEvent;
|
||||
|
||||
public bool IsOpen => (serialPort != null && serialPort.IsOpen) ? true : false;
|
||||
|
||||
|
||||
|
||||
public Serial()
|
||||
{
|
||||
serialPort = new SerialPort();
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 获取窗口名
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string getUserPortName()
|
||||
{
|
||||
return serialPort.PortName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载串口
|
||||
/// </summary>
|
||||
/// <param name="ComPort"></param>
|
||||
public void LoadComSerial(ComboBox ComPort)
|
||||
{
|
||||
ComPort.Items.Clear();
|
||||
list.Clear();
|
||||
if (!IsOpen)
|
||||
{
|
||||
port = SerialPort.GetPortNames();
|
||||
for (int i = 0; i < port.Count(); i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
SerialPort serialPort = new SerialPort(port[i]);
|
||||
serialPort.Open();
|
||||
serialPort.Close();
|
||||
list.Add(port[i]);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
port = list.ToArray();
|
||||
Array.Sort(port);
|
||||
ComboBox.ObjectCollection items = ComPort.Items;
|
||||
object[] items2 = port;
|
||||
items.AddRange(items2);
|
||||
if (port.Length != 0)
|
||||
{
|
||||
ComPort.Text = port[0];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ComPort.Text = getUserPortName();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 打开串口
|
||||
/// </summary>
|
||||
/// <param name="portName"></param>
|
||||
/// <param name="baudRate"></param>
|
||||
/// <param name="parity"></param>
|
||||
/// <param name="dataBits"></param>
|
||||
/// <param name="stopBits"></param>
|
||||
public void OpenSerialPort(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
|
||||
{
|
||||
serialPort.PortName = portName;
|
||||
serialPort.BaudRate = baudRate;
|
||||
serialPort.Parity = parity;
|
||||
serialPort.DataBits = dataBits;
|
||||
serialPort.StopBits = stopBits;
|
||||
try
|
||||
{
|
||||
serialPort.ReadBufferSize = 4096;
|
||||
serialPort.DataReceived += ComReceived;
|
||||
serialPort.Open();
|
||||
if (ComOpenEvent != null)
|
||||
{
|
||||
ComOpenEvent();
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
MessageBox.Show("串口被占用,请重新选择!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭串口
|
||||
/// </summary>
|
||||
public void CloseSeriaPort()
|
||||
{
|
||||
serialPort.DataReceived -= ComReceived;
|
||||
serialPort.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ComReceived(object sender, SerialDataReceivedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (serialPort.IsOpen)
|
||||
{
|
||||
string text = "";
|
||||
Thread.Sleep(50);
|
||||
int length = serialPort.BytesToRead;
|
||||
byte[] data = new byte[length];
|
||||
serialPort.Read(data, 0, length);
|
||||
//对串口接收数据的处理,可对data进行解析
|
||||
string data1 = string.Empty;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
data1 += Convert.ToString(data[i], 16).ToUpper();
|
||||
//data.AppendText(str.Length == 1 ? "0" + str + " " : str + " ");//将接收到的数据以十六进制显示到文本框内
|
||||
}
|
||||
|
||||
//int bytesToRead = serialPort.BytesToRead;
|
||||
//string data = string.Empty;
|
||||
//while (serialPort.BytesToRead > 0)
|
||||
//{
|
||||
// data += serialPort.ReadExisting(); //数据读取,直到读完缓冲区数据
|
||||
//}
|
||||
|
||||
//for (int i = 0; i < bytesToRead; i++)
|
||||
//{
|
||||
// int utf = serialPort.ReadByte();
|
||||
// //text = serialPort.ReadTo("\r");
|
||||
// string text2 = char.ConvertFromUtf32(utf);
|
||||
// text += text2;
|
||||
//}
|
||||
|
||||
if (this.RecvMsgEvent != null)
|
||||
{
|
||||
this.RecvMsgEvent(data1);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 同步发送返回数据
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public string SendReturnData(string str)
|
||||
{
|
||||
string text = "";
|
||||
try
|
||||
{
|
||||
if (serialPort.IsOpen)
|
||||
{
|
||||
serialPort.Write(str);
|
||||
Thread.Sleep(100);
|
||||
text = serialPort.ReadTo("\r");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送字符串
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
public void SendString(string str)
|
||||
{
|
||||
try
|
||||
{
|
||||
serialPort.Write(str);
|
||||
if (this.SendMsgEvent != null)
|
||||
{
|
||||
this.SendMsgEvent(str);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//MessageBox.Show(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// 在已连接条件下关闭本地连接和资源释放时调用
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (serialPort != null&& serialPort.IsOpen)
|
||||
{
|
||||
CloseSeriaPort();
|
||||
if (this.ComDisconnectEvent != null)
|
||||
{
|
||||
this.ComDisconnectEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user