@@ -29,6 +29,7 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
29
using System . Xml . Linq ;
30
30
using System . Linq ;
31
31
using System . Xml ;
32
+ using EasyModbus ;
32
33
33
34
namespace EasyModbusAdvancedClient
34
35
{
@@ -94,6 +95,9 @@ public void EditConnection(ConnectionProperties connectionProperty, int connecti
94
95
95
96
public void AddFunctionProperty ( FunctionProperties functionProperty , int connectionNumber )
96
97
{
98
+ // create link to connection
99
+ functionProperty . Connection = connectionPropertiesList [ connectionNumber ] ;
100
+ // add to list
97
101
connectionPropertiesList [ connectionNumber ] . FunctionPropertiesList . Add ( functionProperty ) ;
98
102
if ( connectionPropertiesListChanged != null )
99
103
connectionPropertiesListChanged ( this ) ;
@@ -126,18 +130,18 @@ public void GetValues(ConnectionProperties connectionProperties, int functionPro
126
130
modbusClient . Connect ( ) ;
127
131
}
128
132
129
- switch ( connectionProperties . FunctionPropertiesList [ functionPropertyID ] . FunctionCode )
133
+ switch ( connectionProperties . FunctionPropertiesList [ functionPropertyID ] . FunctionCodeRead )
130
134
{
131
- case FunctionCode . ReadCoils :
135
+ case FunctionCodeRd . ReadCoils :
132
136
connectionProperties . FunctionPropertiesList [ functionPropertyID ] . values = modbusClient . ReadCoils ( connectionProperties . FunctionPropertiesList [ functionPropertyID ] . StartingAdress , connectionProperties . FunctionPropertiesList [ functionPropertyID ] . Quantity ) ;
133
137
break ;
134
- case FunctionCode . ReadDiscreteInputs :
138
+ case FunctionCodeRd . ReadDiscreteInputs :
135
139
connectionProperties . FunctionPropertiesList [ functionPropertyID ] . values = modbusClient . ReadDiscreteInputs ( connectionProperties . FunctionPropertiesList [ functionPropertyID ] . StartingAdress , connectionProperties . FunctionPropertiesList [ functionPropertyID ] . Quantity ) ;
136
140
break ;
137
- case FunctionCode . ReadHoldingRegisters :
141
+ case FunctionCodeRd . ReadHoldingRegisters :
138
142
connectionProperties . FunctionPropertiesList [ functionPropertyID ] . values = modbusClient . ReadHoldingRegisters ( connectionProperties . FunctionPropertiesList [ functionPropertyID ] . StartingAdress , connectionProperties . FunctionPropertiesList [ functionPropertyID ] . Quantity ) ;
139
143
break ;
140
- case FunctionCode . ReadInputRegisters :
144
+ case FunctionCodeRd . ReadInputRegisters :
141
145
connectionProperties . FunctionPropertiesList [ functionPropertyID ] . values = modbusClient . ReadInputRegisters ( connectionProperties . FunctionPropertiesList [ functionPropertyID ] . StartingAdress , connectionProperties . FunctionPropertiesList [ functionPropertyID ] . Quantity ) ;
142
146
break ;
143
147
default : break ;
@@ -157,18 +161,18 @@ public void GetValues(ConnectionProperties connectionProperties)
157
161
modbusClient . Connect ( ) ;
158
162
}
159
163
foreach ( FunctionProperties functionProperty in connectionProperties . FunctionPropertiesList )
160
- switch ( functionProperty . FunctionCode )
164
+ switch ( functionProperty . FunctionCodeRead )
161
165
{
162
- case FunctionCode . ReadCoils :
166
+ case FunctionCodeRd . ReadCoils :
163
167
functionProperty . values = modbusClient . ReadCoils ( functionProperty . StartingAdress , functionProperty . Quantity ) ;
164
168
break ;
165
- case FunctionCode . ReadDiscreteInputs :
169
+ case FunctionCodeRd . ReadDiscreteInputs :
166
170
functionProperty . values = modbusClient . ReadDiscreteInputs ( functionProperty . StartingAdress , functionProperty . Quantity ) ;
167
171
break ;
168
- case FunctionCode . ReadHoldingRegisters :
172
+ case FunctionCodeRd . ReadHoldingRegisters :
169
173
functionProperty . values = modbusClient . ReadHoldingRegisters ( functionProperty . StartingAdress , functionProperty . Quantity ) ;
170
174
break ;
171
- case FunctionCode . ReadInputRegisters :
175
+ case FunctionCodeRd . ReadInputRegisters :
172
176
functionProperty . values = modbusClient . ReadInputRegisters ( functionProperty . StartingAdress , functionProperty . Quantity ) ;
173
177
break ;
174
178
default : break ;
@@ -181,29 +185,84 @@ public void GetValues(ConnectionProperties connectionProperties)
181
185
public event ConnectionPropertiesListChanged connectionPropertiesListChanged ;
182
186
183
187
184
- public static string getAddress ( FunctionCode functionCode , int startingAddress , int quantity , int elementCount )
188
+ public static string getAddress ( FunctionCodeRd functionCode , int startingAddress , int quantity , int elementCount )
185
189
{
186
190
string returnValue = null ;
187
191
if ( ( startingAddress + elementCount ) <= ( startingAddress + quantity ) )
188
192
switch ( functionCode )
189
193
{
190
- case FunctionCode . ReadCoils :
194
+ case FunctionCodeRd . ReadCoils :
191
195
returnValue = "0x" + ( startingAddress + elementCount + 1 ) . ToString ( ) ;
192
196
break ;
193
- case FunctionCode . ReadDiscreteInputs :
197
+ case FunctionCodeRd . ReadDiscreteInputs :
194
198
returnValue = "1x" + ( startingAddress + elementCount + 1 ) . ToString ( ) ;
195
199
break ;
196
- case FunctionCode . ReadHoldingRegisters :
200
+ case FunctionCodeRd . ReadHoldingRegisters :
197
201
returnValue = "4x" + ( startingAddress + elementCount + 1 ) . ToString ( ) ;
198
202
break ;
199
- case FunctionCode . ReadInputRegisters :
203
+ case FunctionCodeRd . ReadInputRegisters :
200
204
returnValue = "3x" + ( startingAddress + elementCount + 1 ) . ToString ( ) ;
201
205
break ;
202
206
default : break ;
203
207
}
204
208
return returnValue ;
205
209
}
206
210
211
+ public static int [ ] StrToValues ( FunctionProperties functionProperties , string str )
212
+ {
213
+ int [ ] values = { } ;
214
+ switch ( functionProperties . FunctionCodeWrite )
215
+ {
216
+ case FunctionCodeWr . WriteHoldingRegisters :
217
+ int value = 0 ;
218
+ if ( Int32 . TryParse ( str , out value ) )
219
+ {
220
+ // add
221
+ int [ ] x = { value } ;
222
+ return x ;
223
+
224
+ }
225
+
226
+ break ;
227
+ }
228
+ return values ;
229
+ }
230
+
231
+ public FunctionProperties FindPropertyFromGrid ( int gridRow )
232
+ {
233
+ foreach ( ConnectionProperties connection in connectionPropertiesList )
234
+ {
235
+ foreach ( FunctionProperties functionProperty in connection . FunctionPropertiesList )
236
+ {
237
+ if ( functionProperty . DataGridRow == gridRow )
238
+ {
239
+ return functionProperty ;
240
+ }
241
+ }
242
+ }
243
+
244
+ return null ;
245
+ }
246
+
247
+ public void WriteToServer ( FunctionProperties prop , int [ ] values )
248
+ {
249
+ string text = "" ;
250
+ text += "property " + prop . StartingAdress + "\n " + "type " + prop . FunctionCodeWrite . ToString ( ) + "\n " + "new value: " + prop . values . ToString ( ) + "\n " ;
251
+ text += "connection " + prop . Connection . ConnectionName ;
252
+ MessageBox . Show ( text , "updating register" ) ;
253
+
254
+ int startingAddress = prop . StartingAdress ;
255
+ switch ( prop . FunctionCodeWrite )
256
+ {
257
+ case FunctionCodeWr . WriteHoldingRegisters :
258
+ prop . Connection . modbusClient . WriteMultipleRegisters ( startingAddress , values ) ;
259
+ break ;
260
+
261
+ }
262
+
263
+
264
+ }
265
+
207
266
public void WriteXML ( DataGridView dataGridView )
208
267
{
209
268
XmlDocument xmlDocument = new XmlDocument ( ) ;
@@ -233,8 +292,11 @@ public void WriteXML(DataGridView dataGridView)
233
292
for ( int j = 0 ; j < this . connectionPropertiesList [ i ] . FunctionPropertiesList . Count ; j ++ )
234
293
{
235
294
xmlNodeFunctionCodes = xmlDocument . CreateElement ( "functionCodes" ) ;
236
- xmlNodeFunctionCodesProp = xmlDocument . CreateElement ( "functionCode" ) ;
237
- xmlNodeFunctionCodesProp . InnerText = this . connectionPropertiesList [ i ] . FunctionPropertiesList [ j ] . FunctionCode . ToString ( ) ;
295
+ xmlNodeFunctionCodesProp = xmlDocument . CreateElement ( "functionCodeRead" ) ;
296
+ xmlNodeFunctionCodesProp . InnerText = this . connectionPropertiesList [ i ] . FunctionPropertiesList [ j ] . FunctionCodeRead . ToString ( ) ;
297
+ xmlNodeFunctionCodes . AppendChild ( xmlNodeFunctionCodesProp ) ;
298
+ xmlNodeFunctionCodesProp = xmlDocument . CreateElement ( "functionCodeWrite" ) ;
299
+ xmlNodeFunctionCodesProp . InnerText = this . connectionPropertiesList [ i ] . FunctionPropertiesList [ j ] . FunctionCodeWrite . ToString ( ) ;
238
300
xmlNodeFunctionCodes . AppendChild ( xmlNodeFunctionCodesProp ) ;
239
301
xmlNodeFunctionCodesProp = xmlDocument . CreateElement ( "quantity" ) ;
240
302
xmlNodeFunctionCodesProp . InnerText = this . connectionPropertiesList [ i ] . FunctionPropertiesList [ j ] . Quantity . ToString ( ) ;
@@ -295,10 +357,13 @@ public void ReadXML(DataGridView dataGridView)
295
357
xmlNodeList = xmlDocument . GetElementsByTagName ( "connection" ) ;
296
358
//connectionPropertiesList = new List<ConnectionProperties>();
297
359
this . connectionPropertiesList . Clear ( ) ;
360
+ int slotId = 0 ;
298
361
299
362
foreach ( XmlNode xmlNode in xmlNodeList )
300
363
{
301
364
ConnectionProperties connectionProperty = new ConnectionProperties ( ) ;
365
+ AddConnection ( connectionProperty ) ;
366
+
302
367
connectionProperty . ConnectionName = ( xmlNode [ "connectionName" ] . InnerText ) ;
303
368
connectionProperty . ModbusTCPAddress = ( xmlNode [ "ipAddress" ] . InnerText ) ;
304
369
connectionProperty . Port = Int32 . Parse ( xmlNode [ "port" ] . InnerText ) ;
@@ -309,33 +374,56 @@ public void ReadXML(DataGridView dataGridView)
309
374
{
310
375
xmlNodeList2 = xmlNode3 . ChildNodes ;
311
376
FunctionProperties functionProperty = new FunctionProperties ( ) ;
377
+
312
378
foreach ( XmlNode xmlNode2 in xmlNodeList2 )
313
379
{
314
- if ( xmlNode2 . Name == "functionCode " )
380
+ if ( xmlNode2 . Name == "functionCodeRead " )
315
381
switch ( xmlNode2 . InnerText )
316
382
{
317
383
case "ReadCoils" :
318
- functionProperty . FunctionCode = FunctionCode . ReadCoils ;
384
+ functionProperty . FunctionCodeRead = FunctionCodeRd . ReadCoils ;
319
385
break ;
320
386
case "ReadDiscreteInputs" :
321
- functionProperty . FunctionCode = FunctionCode . ReadDiscreteInputs ;
387
+ functionProperty . FunctionCodeRead = FunctionCodeRd . ReadDiscreteInputs ;
322
388
break ;
323
389
case "ReadHoldingRegisters" :
324
- functionProperty . FunctionCode = FunctionCode . ReadHoldingRegisters ;
390
+ functionProperty . FunctionCodeRead = FunctionCodeRd . ReadHoldingRegisters ;
325
391
break ;
326
392
case "ReadInputRegisters" :
327
- functionProperty . FunctionCode = FunctionCode . ReadInputRegisters ;
393
+ functionProperty . FunctionCodeRead = FunctionCodeRd . ReadInputRegisters ;
328
394
break ;
329
395
}
396
+ if ( xmlNode2 . Name == "functionCodeWrite" )
397
+ {
398
+ functionProperty . FunctionCodeWrite = FunctionCodeWr . WriteNone ;
399
+ switch ( xmlNode2 . InnerText )
400
+ {
401
+ case "WriteCoils" :
402
+ functionProperty . FunctionCodeWrite = FunctionCodeWr . WriteNone ;
403
+ break ;
404
+ case "WriteDiscreteInputs" :
405
+ functionProperty . FunctionCodeWrite = FunctionCodeWr . WriteNone ;
406
+ break ;
407
+ case "WriteHoldingRegisters" :
408
+ functionProperty . FunctionCodeWrite = FunctionCodeWr . WriteHoldingRegisters ;
409
+ break ;
410
+ case "WriteInputRegisters" :
411
+ functionProperty . FunctionCodeWrite = FunctionCodeWr . WriteNone ;
412
+ break ;
413
+ }
414
+
415
+ }
330
416
if ( xmlNode2 . Name == "startingAddress" )
331
417
functionProperty . StartingAdress = Int32 . Parse ( xmlNode2 . InnerText ) ;
332
418
if ( xmlNode2 . Name == "quantity" )
333
419
functionProperty . Quantity = Int32 . Parse ( xmlNode2 . InnerText ) ;
334
420
}
335
- connectionProperty . FunctionPropertiesList . Add ( functionProperty ) ;
421
+ //connectionProperty.FunctionPropertiesList.Add(functionProperty);
422
+ this . AddFunctionProperty ( functionProperty , slotId ) ;
423
+
336
424
xmlNode3 = xmlNode3 . NextSibling ;
337
425
}
338
- AddConnection ( connectionProperty ) ;
426
+ slotId ++ ;
339
427
//this.connectionPropertiesList.Add(connectionProperty);
340
428
}
341
429
if ( connectionPropertiesListChanged != null )
@@ -361,37 +449,59 @@ public void ReadXML(DataGridView dataGridView)
361
449
if ( xmlNode [ "columnDataType" ] != null )
362
450
dataGridView [ 3 , dataGridView . Rows . Count - 1 ] . Value = xmlNode [ "columnDataType" ] . InnerText ;
363
451
}
452
+
453
+ // trigger update of values manually
454
+ //this.valuesChanged(this);
455
+
364
456
dataGridView . AllowUserToAddRows = true ;
365
457
}
366
458
}
367
459
368
460
369
- public enum FunctionCode : int
461
+ public enum FunctionCodeRd : int
370
462
{
371
463
ReadCoils = 1 ,
372
464
ReadDiscreteInputs = 2 ,
373
465
ReadHoldingRegisters = 3 ,
374
466
ReadInputRegisters = 4 ,
375
467
} ;
376
-
377
-
378
- public class FunctionProperties
468
+
469
+ public enum FunctionCodeWr : int
470
+ {
471
+ WriteNone = 0 ,
472
+ WriteCoils = 1 ,
473
+ WriteDiscreteInputs = 2 ,
474
+ WriteHoldingRegisters = 3 ,
475
+ WriteInputRegisters = 4 ,
476
+ } ;
477
+
478
+
479
+ public class FunctionProperties
379
480
{
380
481
381
- FunctionCode funtionCode = FunctionCode . ReadCoils ;
382
-
383
- [ Browsable ( true ) ]
482
+ FunctionCodeRd functionCodeRd = FunctionCodeRd . ReadCoils ;
483
+ [ Browsable ( true ) ]
384
484
[ Category ( "Function code properties" ) ]
385
- [ Description ( "Function Code" ) ]
386
- [ DisplayName ( "Function Code" ) ]
387
- public FunctionCode FunctionCode
485
+ [ Description ( "Function Code Read " ) ]
486
+ [ DisplayName ( "Function Code Read " ) ]
487
+ public FunctionCodeRd FunctionCodeRead
388
488
{
389
- get { return funtionCode ; }
390
- set { funtionCode = value ; }
489
+ get { return functionCodeRd ; }
490
+ set { functionCodeRd = value ; }
391
491
}
392
-
393
-
394
- int startingAdress = 0 ;
492
+
493
+ FunctionCodeWr functionCodeWr = FunctionCodeWr . WriteNone ;
494
+ [ Browsable ( true ) ]
495
+ [ Category ( "Function code properties" ) ]
496
+ [ Description ( "Function Code Write" ) ]
497
+ [ DisplayName ( "Function Code Write" ) ]
498
+ public FunctionCodeWr FunctionCodeWrite
499
+ {
500
+ get { return functionCodeWr ; }
501
+ set { functionCodeWr = value ; }
502
+ }
503
+
504
+ int startingAdress = 0 ;
395
505
[ Browsable ( true ) ]
396
506
[ Category ( "Function code properties" ) ]
397
507
[ Description ( "Starting Address" ) ]
@@ -413,7 +523,29 @@ public int Quantity
413
523
get { return quantity ; }
414
524
set { quantity = value ; }
415
525
}
416
-
417
- public object values ;
526
+
527
+ int DataGridRowIdx = - 1 ;
528
+ [ Browsable ( false ) ]
529
+ [ Category ( "Function code properties" ) ]
530
+ [ Description ( "Data Grid Row Idx" ) ]
531
+ [ DisplayName ( "Data Grid Row Idx" ) ]
532
+ public int DataGridRow
533
+ {
534
+ get { return DataGridRowIdx ; }
535
+ set { DataGridRowIdx = value ; }
536
+ }
537
+
538
+ ConnectionProperties connection = null ;
539
+ [ Browsable ( false ) ]
540
+ [ Category ( "Function code properties" ) ]
541
+ [ Description ( "connection" ) ]
542
+ [ DisplayName ( "connection" ) ]
543
+ public ConnectionProperties Connection
544
+ {
545
+ get { return connection ; }
546
+ set { connection = value ; }
547
+ }
548
+
549
+ public object values ;
418
550
}
419
551
}
0 commit comments