From ff2d2490c34c677fd91276f46f4346d20ab16b74 Mon Sep 17 00:00:00 2001 From: seayxu Date: Mon, 28 Aug 2017 11:32:41 +0800 Subject: [PATCH] Add GodSerialPort to event action as signature param for initial a list. --- README.md | 7 ++- .../GodSharp.SerialPort.nuspec | 5 +- .../Properties/AssemblyInfo.cs | 4 +- .../SerialPort/GodSerialPort.cs | 47 +++++++++++-------- .../Program.cs | 2 +- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index f82cc61..ebb091b 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ GodSerialPort serial = new GodSerialPort("COM1", 9600); **Notice**:*This is not need when you read data by read method.* ``` -serial.UseDataReceived((bytes)=>{}); +serial.UseDataReceived((sp,bytes)=>{}); ``` 3. Open SerialPort object. @@ -60,7 +60,7 @@ class Program } GodSerialPort gsp = new GodSerialPort("COM"+num, 9600); - gsp.UseDataReceived((bytes) => { + gsp.UseDataReceived((sp,bytes) => { string buffer = string.Join(" ", bytes); Console.WriteLine("receive data:" + buffer); }); @@ -98,6 +98,9 @@ class Program # Notes +## 1.1.2 +- 1.Add GodSerialPort to event action as signature param for initial a list. + ## 1.1.1 - 1.Add constructor and change the constructor signature. - 2.Add `PortUtil` class. diff --git a/src/GodSharp.SerialPort/GodSharp.SerialPort.nuspec b/src/GodSharp.SerialPort/GodSharp.SerialPort.nuspec index 41d47b3..ea749c9 100644 --- a/src/GodSharp.SerialPort/GodSharp.SerialPort.nuspec +++ b/src/GodSharp.SerialPort/GodSharp.SerialPort.nuspec @@ -2,7 +2,7 @@ GodSharp.SerialPort - 1.1.1 + 1.1.2 GodSharp.SerialPort seayxu seayxu @@ -14,6 +14,9 @@ An easy-to-use .NET SerialPort class.(.NET Framework >= 3.5) + 1.1.2 + - 1.Add GodSerialPort to event action as signature param. + 1.1.1 - 1.Add constructor and change the constructor signature. - 2.Add PortUtil class. diff --git a/src/GodSharp.SerialPort/Properties/AssemblyInfo.cs b/src/GodSharp.SerialPort/Properties/AssemblyInfo.cs index e649669..464f03a 100644 --- a/src/GodSharp.SerialPort/Properties/AssemblyInfo.cs +++ b/src/GodSharp.SerialPort/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.1.*")] -//[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.1.2.*")] +[assembly: AssemblyFileVersion("1.1.0.0")] diff --git a/src/GodSharp.Shared/SerialPort/GodSerialPort.cs b/src/GodSharp.Shared/SerialPort/GodSerialPort.cs index 2ed59ba..82b8640 100644 --- a/src/GodSharp.Shared/SerialPort/GodSerialPort.cs +++ b/src/GodSharp.Shared/SerialPort/GodSerialPort.cs @@ -18,7 +18,7 @@ namespace GodSharp /// /// /// GodSerialPort serial= new GodSerialPort("COM1",9600); - /// serial.UseDataReceived((bytes)=>{}); + /// serial.UseDataReceived((sp,bytes)=>{}); /// serial.Open(); /// public class GodSerialPort @@ -45,17 +45,17 @@ public class GodSerialPort /// /// The method of execution that data has been received through a port represented by the SerialPort object. /// - private Action onData; + private Action onData; /// /// The method of execution that an error has occurred with a port represented by a SerialPort object. /// - private Action onError; + private Action onError; /// /// The method of execution that a non-data signal event has occurred on the port represented by the SerialPort object. /// - private Action onPinChange; + private Action onPinChange; /// /// Gets or sets the data format. @@ -591,9 +591,7 @@ private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs if (serialPort.IsOpen) { byte[] bytes = this.TryRead(); - this.onData?.Invoke(bytes); - this.DiscardOutBuffer(); - this.DiscardInBuffer(); + this.onData?.Invoke(this, bytes); } else { @@ -619,7 +617,7 @@ private void SerialPort_ErrorReceived(object sender, SerialErrorReceivedEventArg { try { - this.onError?.Invoke(e.EventType); + this.onError?.Invoke(this, e.EventType); } catch (Exception ex) { @@ -639,7 +637,7 @@ private void SerialPort_PinChanged(object sender, SerialPinChangedEventArgs e) { try { - this.onPinChange?.Invoke(e.EventType); + this.onPinChange?.Invoke(this, e.EventType); } catch (Exception ex) { @@ -653,7 +651,7 @@ private void SerialPort_PinChanged(object sender, SerialPinChangedEventArgs e) /// Use DataReceived event with data received action. /// /// The action which process data. - public void UseDataReceived(Action action) + public void UseDataReceived(Action action) { onData = action; serialPort.DataReceived += SerialPort_DataReceived; @@ -694,7 +692,9 @@ public void Init() } catch (Exception ex) { - Console.WriteLine("Init SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message); +#if DEBUG + Console.WriteLine("Init SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message); +#endif throw new Exception(ex.Message, ex); } } @@ -718,18 +718,24 @@ public bool Open() } else { - Console.WriteLine("the port is opened!"); +#if DEBUG + Console.WriteLine("the port is opened!"); +#endif return true; } } catch (Exception ex) { - Console.WriteLine("Open SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message); +#if DEBUG + Console.WriteLine("Open SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message); +#endif } if (serialPort.IsOpen) { - Console.WriteLine("successed to open the port!"); +#if DEBUG + Console.WriteLine("successed to open the port!"); +#endif rst = true; } return rst; @@ -742,7 +748,7 @@ public bool Open() /// Set the method when [error]. /// /// The action. - public void OnError(Action action) + public void OnError(Action action) { this.onError = action; } @@ -753,7 +759,7 @@ public void OnError(Action action) /// Set the method when [pin changed]. /// /// The action. - public void OnPinChange(Action action) + public void OnPinChange(Action action) { this.onPinChange = action; } @@ -770,7 +776,9 @@ public bool Close() { if (!serialPort.IsOpen) { - Console.WriteLine("the port is already closed!"); +#if DEBUG + Console.WriteLine("the port is already closed!"); +#endif return true; } else @@ -781,8 +789,9 @@ public bool Close() } catch (Exception ex) { - Console.WriteLine("Close SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message + - "\r\nStackTrace:" + ex.StackTrace); +#if DEBUG + Console.WriteLine("Close SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message + "\r\nStackTrace:" + ex.StackTrace); +#endif throw new Exception(ex.Message, ex); } } diff --git a/test/GodSharp.SerialPort.ConsoleSample/Program.cs b/test/GodSharp.SerialPort.ConsoleSample/Program.cs index ad30c13..6bca2d3 100644 --- a/test/GodSharp.SerialPort.ConsoleSample/Program.cs +++ b/test/GodSharp.SerialPort.ConsoleSample/Program.cs @@ -17,7 +17,7 @@ static void Main(string[] args) } GodSerialPort gsp = new GodSerialPort("COM"+num, 9600); - gsp.UseDataReceived((bytes) => { + gsp.UseDataReceived((sp,bytes) => { if (bytes!=null&&bytes.Length>0) { string buffer = string.Join(" ", bytes);