@@ -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 \n Message:" + ex . Message ) ;
695+ #if DEBUG
696+ Console . WriteLine ( "Init SerialPort Exception:" + PortName + "\r \n Message:" + 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 \n Message:" + ex . Message ) ;
729+ #if DEBUG
730+ Console . WriteLine ( "Open SerialPort Exception:" + PortName + "\r \n Message:" + 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 \n Message:" + ex . Message +
785- "\r \n StackTrace:" + ex . StackTrace ) ;
792+ #if DEBUG
793+ Console . WriteLine ( "Close SerialPort Exception:" + PortName + "\r \n Message:" + ex . Message + "\r \n StackTrace:" + ex . StackTrace ) ;
794+ #endif
786795 throw new Exception ( ex . Message , ex ) ;
787796 }
788797 }
0 commit comments