Skip to content

Commit ff2d249

Browse files
committed
Add GodSerialPort to event action as signature param for initial a list.
1 parent d960761 commit ff2d249

File tree

5 files changed

+40
-25
lines changed

5 files changed

+40
-25
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ GodSerialPort serial = new GodSerialPort("COM1", 9600);
1919

2020
**Notice**:*This is not need when you read data by read method.*
2121
```
22-
serial.UseDataReceived((bytes)=>{});
22+
serial.UseDataReceived((sp,bytes)=>{});
2323
```
2424

2525
3. Open SerialPort object.
@@ -60,7 +60,7 @@ class Program
6060
}
6161
6262
GodSerialPort gsp = new GodSerialPort("COM"+num, 9600);
63-
gsp.UseDataReceived((bytes) => {
63+
gsp.UseDataReceived((sp,bytes) => {
6464
string buffer = string.Join(" ", bytes);
6565
Console.WriteLine("receive data:" + buffer);
6666
});
@@ -98,6 +98,9 @@ class Program
9898

9999
# Notes
100100

101+
## 1.1.2
102+
- 1.Add GodSerialPort to event action as signature param for initial a list.
103+
101104
## 1.1.1
102105
- 1.Add constructor and change the constructor signature.
103106
- 2.Add `PortUtil` class.

src/GodSharp.SerialPort/GodSharp.SerialPort.nuspec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package >
33
<metadata>
44
<id>GodSharp.SerialPort</id>
5-
<version>1.1.1</version>
5+
<version>1.1.2</version>
66
<title>GodSharp.SerialPort</title>
77
<authors>seayxu</authors>
88
<owners>seayxu</owners>
@@ -14,6 +14,9 @@
1414
<releaseNotes>
1515
An easy-to-use .NET SerialPort class.(.NET Framework >= 3.5)
1616

17+
1.1.2
18+
- 1.Add GodSerialPort to event action as signature param.
19+
1720
1.1.1
1821
- 1.Add constructor and change the constructor signature.
1922
- 2.Add PortUtil class.

src/GodSharp.SerialPort/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.1.*")]
35-
//[assembly: AssemblyFileVersion("1.0.0.0")]
34+
[assembly: AssemblyVersion("1.1.2.*")]
35+
[assembly: AssemblyFileVersion("1.1.0.0")]

src/GodSharp.Shared/SerialPort/GodSerialPort.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace GodSharp
1818
/// </summary>
1919
/// <example>
2020
/// GodSerialPort serial= new GodSerialPort("COM1",9600);
21-
/// serial.UseDataReceived((bytes)=>{});
21+
/// serial.UseDataReceived((sp,bytes)=>{});
2222
/// serial.Open();
2323
/// </example>
2424
public class GodSerialPort
@@ -45,17 +45,17 @@ public class GodSerialPort
4545
/// <summary>
4646
/// The method of execution that data has been received through a port represented by the SerialPort object.
4747
/// </summary>
48-
private Action<byte[]> onData;
48+
private Action<GodSerialPort,byte[]> onData;
4949

5050
/// <summary>
5151
/// The method of execution that an error has occurred with a port represented by a SerialPort object.
5252
/// </summary>
53-
private Action<SerialError> onError;
53+
private Action<GodSerialPort,SerialError> onError;
5454

5555
/// <summary>
5656
/// The method of execution that a non-data signal event has occurred on the port represented by the SerialPort object.
5757
/// </summary>
58-
private Action<SerialPinChange> onPinChange;
58+
private Action<GodSerialPort,SerialPinChange> onPinChange;
5959

6060
/// <summary>
6161
/// Gets or sets the data format.
@@ -591,9 +591,7 @@ private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs
591591
if (serialPort.IsOpen)
592592
{
593593
byte[] bytes = this.TryRead();
594-
this.onData?.Invoke(bytes);
595-
this.DiscardOutBuffer();
596-
this.DiscardInBuffer();
594+
this.onData?.Invoke(this, bytes);
597595
}
598596
else
599597
{
@@ -619,7 +617,7 @@ private void SerialPort_ErrorReceived(object sender, SerialErrorReceivedEventArg
619617
{
620618
try
621619
{
622-
this.onError?.Invoke(e.EventType);
620+
this.onError?.Invoke(this, e.EventType);
623621
}
624622
catch (Exception ex)
625623
{
@@ -639,7 +637,7 @@ private void SerialPort_PinChanged(object sender, SerialPinChangedEventArgs e)
639637
{
640638
try
641639
{
642-
this.onPinChange?.Invoke(e.EventType);
640+
this.onPinChange?.Invoke(this, e.EventType);
643641
}
644642
catch (Exception ex)
645643
{
@@ -653,7 +651,7 @@ private void SerialPort_PinChanged(object sender, SerialPinChangedEventArgs e)
653651
/// Use DataReceived event with data received action.
654652
/// </summary>
655653
/// <param name="action">The action which process data.</param>
656-
public void UseDataReceived(Action<byte[]> action)
654+
public void UseDataReceived(Action<GodSerialPort, byte[]> action)
657655
{
658656
onData = action;
659657
serialPort.DataReceived += SerialPort_DataReceived;
@@ -694,7 +692,9 @@ public void Init()
694692
}
695693
catch (Exception ex)
696694
{
697-
Console.WriteLine("Init SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message);
695+
#if DEBUG
696+
Console.WriteLine("Init SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message);
697+
#endif
698698
throw new Exception(ex.Message, ex);
699699
}
700700
}
@@ -718,18 +718,24 @@ public bool Open()
718718
}
719719
else
720720
{
721-
Console.WriteLine("the port is opened!");
721+
#if DEBUG
722+
Console.WriteLine("the port is opened!");
723+
#endif
722724
return true;
723725
}
724726
}
725727
catch (Exception ex)
726728
{
727-
Console.WriteLine("Open SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message);
729+
#if DEBUG
730+
Console.WriteLine("Open SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message);
731+
#endif
728732
}
729733

730734
if (serialPort.IsOpen)
731735
{
732-
Console.WriteLine("successed to open the port!");
736+
#if DEBUG
737+
Console.WriteLine("successed to open the port!");
738+
#endif
733739
rst = true;
734740
}
735741
return rst;
@@ -742,7 +748,7 @@ public bool Open()
742748
/// Set the method when [error].
743749
/// </summary>
744750
/// <param name="action">The action.</param>
745-
public void OnError(Action<SerialError> action)
751+
public void OnError(Action<GodSerialPort, SerialError> action)
746752
{
747753
this.onError = action;
748754
}
@@ -753,7 +759,7 @@ public void OnError(Action<SerialError> action)
753759
/// Set the method when [pin changed].
754760
/// </summary>
755761
/// <param name="action">The action.</param>
756-
public void OnPinChange(Action<SerialPinChange> action)
762+
public void OnPinChange(Action<GodSerialPort, SerialPinChange> action)
757763
{
758764
this.onPinChange = action;
759765
}
@@ -770,7 +776,9 @@ public bool Close()
770776
{
771777
if (!serialPort.IsOpen)
772778
{
773-
Console.WriteLine("the port is already closed!");
779+
#if DEBUG
780+
Console.WriteLine("the port is already closed!");
781+
#endif
774782
return true;
775783
}
776784
else
@@ -781,8 +789,9 @@ public bool Close()
781789
}
782790
catch (Exception ex)
783791
{
784-
Console.WriteLine("Close SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message +
785-
"\r\nStackTrace:" + ex.StackTrace);
792+
#if DEBUG
793+
Console.WriteLine("Close SerialPort Exception:" + PortName + "\r\nMessage:" + ex.Message + "\r\nStackTrace:" + ex.StackTrace);
794+
#endif
786795
throw new Exception(ex.Message, ex);
787796
}
788797
}

test/GodSharp.SerialPort.ConsoleSample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static void Main(string[] args)
1717
}
1818

1919
GodSerialPort gsp = new GodSerialPort("COM"+num, 9600);
20-
gsp.UseDataReceived((bytes) => {
20+
gsp.UseDataReceived((sp,bytes) => {
2121
if (bytes!=null&&bytes.Length>0)
2222
{
2323
string buffer = string.Join(" ", bytes);

0 commit comments

Comments
 (0)