@@ -52,6 +52,15 @@ public int ConnectTimeout {
52
52
set { connectTimeout = value ; }
53
53
}
54
54
55
+ private int pollerDelayMs = 0 ;
56
+ /// <summary>
57
+ /// Delay for each poller cycle in milliseconds, default = 0
58
+ /// </summary>
59
+ public int PollerDelayMs {
60
+ get { return pollerDelayMs ; }
61
+ set { pollerDelayMs = value ; }
62
+ }
63
+
55
64
/// <summary>
56
65
/// The host ip endpoint, leave it null to use an automatic interface
57
66
/// </summary>
@@ -101,6 +110,9 @@ private set {
101
110
private int stationNumber ;
102
111
private int cycleTimeMs = 25 ;
103
112
113
+ private int bytesTotalCountedUpstream = 0 ;
114
+ private int bytesTotalCountedDownstream = 0 ;
115
+
104
116
/// <summary>
105
117
/// The current IP of the PLC connection
106
118
/// </summary>
@@ -125,13 +137,40 @@ private set {
125
137
}
126
138
}
127
139
140
+ private int bytesPerSecondUpstream = 0 ;
141
+ /// <summary>
142
+ /// The current transmission speed in bytes per second
143
+ /// </summary>
144
+ public int BytesPerSecondUpstream {
145
+ get { return bytesPerSecondUpstream ; }
146
+ private set {
147
+ bytesPerSecondUpstream = value ;
148
+ PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( nameof ( BytesPerSecondUpstream ) ) ) ;
149
+ }
150
+ }
151
+
152
+ private int bytesPerSecondDownstream = 0 ;
153
+ /// <summary>
154
+ /// The current transmission speed in bytes per second
155
+ /// </summary>
156
+ public int BytesPerSecondDownstream {
157
+ get { return bytesPerSecondDownstream ; }
158
+ private set {
159
+ bytesPerSecondDownstream = value ;
160
+ PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( nameof ( BytesPerSecondDownstream ) ) ) ;
161
+ }
162
+ }
163
+
128
164
internal NetworkStream stream ;
129
165
internal TcpClient client ;
130
166
internal readonly SerialQueue queue = new SerialQueue ( ) ;
131
167
private int RecBufferSize = 128 ;
132
168
internal int SendExceptionsInRow = 0 ;
133
169
internal bool ImportantTaskRunning = false ;
134
170
171
+ private Stopwatch speedStopwatchUpstr ;
172
+ private Stopwatch speedStopwatchDownstr ;
173
+
135
174
#region Initialization
136
175
137
176
/// <summary>
@@ -736,13 +775,30 @@ private async Task<string> SendSingleBlock (string _blockString) {
736
775
737
776
var message = _blockString . ToHexASCIIBytes ( ) ;
738
777
778
+ //time measuring
779
+ if ( speedStopwatchUpstr == null ) {
780
+ speedStopwatchUpstr = Stopwatch . StartNew ( ) ;
781
+ }
782
+
783
+ if ( speedStopwatchUpstr . Elapsed . TotalSeconds >= 1 ) {
784
+ speedStopwatchUpstr . Restart ( ) ;
785
+ bytesTotalCountedUpstream = 0 ;
786
+ }
787
+
739
788
//send request
740
789
using ( var sendStream = new MemoryStream ( message ) ) {
741
790
await sendStream . CopyToAsync ( stream ) ;
742
791
Logger . Log ( $ "[--------------------------------]", LogLevel . Critical , this ) ;
743
792
Logger . Log ( $ "--> OUT MSG: { _blockString } ", LogLevel . Critical , this ) ;
744
793
}
745
794
795
+ //calc upstream speed
796
+ bytesTotalCountedUpstream += message . Length ;
797
+
798
+ var perSecUpstream = ( double ) ( ( bytesTotalCountedUpstream / speedStopwatchUpstr . Elapsed . TotalMilliseconds ) * 1000 ) ;
799
+ if ( perSecUpstream <= 10000 )
800
+ BytesPerSecondUpstream = ( int ) Math . Round ( perSecUpstream , MidpointRounding . AwayFromZero ) ;
801
+
746
802
//await result
747
803
StringBuilder response = new StringBuilder ( ) ;
748
804
try {
@@ -755,6 +811,17 @@ private async Task<string> SendSingleBlock (string _blockString) {
755
811
while ( ! endLineCode && ! startMsgCode ) {
756
812
757
813
do {
814
+
815
+ //time measuring
816
+ if ( speedStopwatchDownstr == null ) {
817
+ speedStopwatchDownstr = Stopwatch . StartNew ( ) ;
818
+ }
819
+
820
+ if ( speedStopwatchDownstr . Elapsed . TotalSeconds >= 1 ) {
821
+ speedStopwatchDownstr . Restart ( ) ;
822
+ bytesTotalCountedDownstream = 0 ;
823
+ }
824
+
758
825
int bytes = await stream . ReadAsync ( responseBuffer , 0 , responseBuffer . Length ) ;
759
826
760
827
endLineCode = responseBuffer . Any ( x => x == 0x0D ) ;
@@ -777,8 +844,18 @@ private async Task<string> SendSingleBlock (string _blockString) {
777
844
}
778
845
779
846
if ( ! string . IsNullOrEmpty ( response . ToString ( ) ) ) {
847
+
780
848
Logger . Log ( $ "<-- IN MSG: { response } ", LogLevel . Critical , this ) ;
849
+
850
+ bytesTotalCountedDownstream += Encoding . ASCII . GetByteCount ( response . ToString ( ) ) ;
851
+
852
+ var perSecDownstream = ( double ) ( ( bytesTotalCountedDownstream / speedStopwatchDownstr . Elapsed . TotalMilliseconds ) * 1000 ) ;
853
+
854
+ if ( perSecUpstream <= 10000 )
855
+ BytesPerSecondDownstream = ( int ) Math . Round ( perSecUpstream , MidpointRounding . AwayFromZero ) ;
856
+
781
857
return response . ToString ( ) ;
858
+
782
859
} else {
783
860
return null ;
784
861
}
0 commit comments