serial comm app

This commit is contained in:
2023-05-16 11:14:28 +09:00
parent 16d0ca5d8a
commit bce6521e43
21 changed files with 1594 additions and 4 deletions

View File

@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SerialCommApp.SeriallLibs
{
public class PMessageHistory<T>
{
private List<T> _messageList;
private int _size;
public PMessageHistory(int size)
{
_size = size;
_messageList = new List<T>(_size);
}
public void Add(T message)
{
if (_messageList.Count == 0)
_messageList.Add(message);
else
_messageList.Insert(0, message);
if (_messageList.Count <= _size)
return;
while (_messageList.Count > _size)
{
_messageList.RemoveAt(_messageList.Count);
}
}
public T this[int index]
{
get { return _messageList[index]; }
}
public void Clear()
{
_messageList = new List<T>(_size);
}
public int Count { get { return _messageList.Count; } }
}
}

View File

@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SerialCommApp.SeriallLibs
{
public class PSerialComm
{
private readonly string CRLF = "\r\n";
private SerialPort _serialPort;
private string _stringBuffer;
private object _receiveLock = new object();
public event EventHandler<string> OnDataReceived;
public PMessageHistory<string> History { get; set; }
public PSerialCommSetting SerialCommSetting { get; set; }
public bool IsOpen { get { return _serialPort.IsOpen; } }
public string PortName
{
get
{
return _serialPort.PortName;
}
set
{
_serialPort.PortName = value;
}
}
public PSerialComm()
{
InitInstance();
}
public PSerialComm(string port)
{
InitInstance();
this.PortName = port;
}
private void InitInstance()
{
if (_serialPort != null)
_serialPort.Close();
_serialPort = new SerialPort();
_serialPort.DataReceived += SerialPort_DataReceived;
SerialCommSetting = new PSerialCommSetting();
this.History = new PMessageHistory<string>(10);
}
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort serialPort = sender as SerialPort;
if (serialPort == null)
return;
string data = serialPort.ReadExisting();
if (data.Length < 1)
return;
HandleData(data);
}
public static string[] GetPortNames()
{
return SerialPort.GetPortNames();
}
public void Write(string message)
{
_serialPort.Write(message);
}
private void HandleData(string data)
{
_stringBuffer = string.Concat(_stringBuffer, data);
if (this.OnDataReceived == null)
return;
while (true)
{
int subStringIdx = _stringBuffer.IndexOf(CRLF);
if (subStringIdx < 0)
break;
string message = _stringBuffer.Substring(0, subStringIdx);
History.Add(message);
if (this.OnDataReceived != null)
this.OnDataReceived(this, message);
_stringBuffer = _stringBuffer.Remove(0, subStringIdx + CRLF.Length);
}
}
public void Open()
{
if (string.IsNullOrEmpty(this.PortName))
throw new Exception("Port name is cannot be empty.");
if (!_serialPort.IsOpen)
_serialPort.Open();
}
public void Close()
{
if (string.IsNullOrEmpty(this.PortName))
throw new Exception("Port name is cannot be empty.");
if (_serialPort.IsOpen)
_serialPort.Close();
}
}
}

View File

@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SerialCommApp.SeriallLibs
{
[DefaultProperty("Serial Communication")]
public class PSerialCommSetting : ICloneable
{
public enum PSerialBaudRate
{
B1200 = 1200,
B2400 = 2400,
B4800 = 4800,
B9600 = 9600,
B19200 = 19200,
B38400 = 38400,
B57600 = 57600,
B115200 = 115200,
}
public enum PSerailDataBit
{
D8 = 8,
D7 = 7,
}
[Category("Communication")]
public PSerialBaudRate BaudRate { get; set; }
[Category("Communication")]
public PSerailDataBit DataBit { get; set; }
[Category("Communication")]
public Parity Parity { get; set; }
[Category("Communication")]
public StopBits StopBits { get; set; }
public PSerialCommSetting(PSerialBaudRate baudRate = PSerialBaudRate.B9600, PSerailDataBit dataBit = PSerailDataBit.D8, Parity parity = Parity.None, StopBits stopBits = StopBits.None)
{
this.BaudRate = baudRate;
this.DataBit = dataBit;
this.Parity = parity;
this.StopBits = stopBits;
}
public object Clone()
{
PSerialCommSetting cloned = new PSerialCommSetting();
cloned.BaudRate = this.BaudRate;
cloned.DataBit = this.DataBit;
cloned.Parity = this.Parity;
cloned.StopBits = this.StopBits;
return cloned;
}
}
}