Skip to content

Commit

Permalink
Add GodSerialPort to event action as signature param for initial a list.
Browse files Browse the repository at this point in the history
  • Loading branch information
seayxu committed Aug 28, 2017
1 parent d960761 commit ff2d249
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion src/GodSharp.SerialPort/GodSharp.SerialPort.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>GodSharp.SerialPort</id>
<version>1.1.1</version>
<version>1.1.2</version>
<title>GodSharp.SerialPort</title>
<authors>seayxu</authors>
<owners>seayxu</owners>
Expand All @@ -14,6 +14,9 @@
<releaseNotes>
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.
Expand Down
4 changes: 2 additions & 2 deletions src/GodSharp.SerialPort/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
47 changes: 28 additions & 19 deletions src/GodSharp.Shared/SerialPort/GodSerialPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace GodSharp
/// </summary>
/// <example>
/// GodSerialPort serial= new GodSerialPort("COM1",9600);
/// serial.UseDataReceived((bytes)=>{});
/// serial.UseDataReceived((sp,bytes)=>{});
/// serial.Open();
/// </example>
public class GodSerialPort
Expand All @@ -45,17 +45,17 @@ public class GodSerialPort
/// <summary>
/// The method of execution that data has been received through a port represented by the SerialPort object.
/// </summary>
private Action<byte[]> onData;
private Action<GodSerialPort,byte[]> onData;

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

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

/// <summary>
/// Gets or sets the data format.
Expand Down Expand Up @@ -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
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -653,7 +651,7 @@ private void SerialPort_PinChanged(object sender, SerialPinChangedEventArgs e)
/// Use DataReceived event with data received action.
/// </summary>
/// <param name="action">The action which process data.</param>
public void UseDataReceived(Action<byte[]> action)
public void UseDataReceived(Action<GodSerialPort, byte[]> action)
{
onData = action;
serialPort.DataReceived += SerialPort_DataReceived;
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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;
Expand All @@ -742,7 +748,7 @@ public bool Open()
/// Set the method when [error].
/// </summary>
/// <param name="action">The action.</param>
public void OnError(Action<SerialError> action)
public void OnError(Action<GodSerialPort, SerialError> action)
{
this.onError = action;
}
Expand All @@ -753,7 +759,7 @@ public void OnError(Action<SerialError> action)
/// Set the method when [pin changed].
/// </summary>
/// <param name="action">The action.</param>
public void OnPinChange(Action<SerialPinChange> action)
public void OnPinChange(Action<GodSerialPort, SerialPinChange> action)
{
this.onPinChange = action;
}
Expand All @@ -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
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/GodSharp.SerialPort.ConsoleSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ff2d249

Please sign in to comment.