@@ -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,71 @@ 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 int [ ] StrToValues ( FunctionProperties functionProperties , string str )
212
+ {
213
+ int [ ] values = { 1 } ;
214
+
215
+ return values ;
216
+ }
217
+
218
+ public FunctionProperties FindPropertyFromGrid ( int gridRow )
219
+ {
220
+ foreach ( ConnectionProperties connection in connectionPropertiesList )
221
+ {
222
+ foreach ( FunctionProperties functionProperty in connection . FunctionPropertiesList )
223
+ {
224
+ if ( functionProperty . DataGridRow == gridRow )
225
+ {
226
+ return functionProperty ;
227
+ }
228
+ }
229
+ }
230
+
231
+ return null ;
232
+ }
233
+
234
+ public void WriteToServer ( FunctionProperties prop , int [ ] values )
235
+ {
236
+ string text = "" ;
237
+ text += "property " + prop . StartingAdress + "\n " + "type " + prop . FunctionCodeWrite . ToString ( ) + "\n " + "new value: " + prop . values . ToString ( ) + "\n " ;
238
+ text += "connection " + prop . Connection . ConnectionName ;
239
+ MessageBox . Show ( text , "updating register" ) ;
240
+
241
+ int startingAddress = prop . StartingAdress ;
242
+ switch ( prop . FunctionCodeWrite )
243
+ {
244
+ case FunctionCodeWr . WriteHoldingRegisters :
245
+ prop . Connection . modbusClient . WriteMultipleRegisters ( startingAddress , values ) ;
246
+ break ;
247
+
248
+ }
249
+
250
+
251
+ }
252
+
207
253
public void WriteXML ( DataGridView dataGridView )
208
254
{
209
255
XmlDocument xmlDocument = new XmlDocument ( ) ;
@@ -233,8 +279,11 @@ public void WriteXML(DataGridView dataGridView)
233
279
for ( int j = 0 ; j < this . connectionPropertiesList [ i ] . FunctionPropertiesList . Count ; j ++ )
234
280
{
235
281
xmlNodeFunctionCodes = xmlDocument . CreateElement ( "functionCodes" ) ;
236
- xmlNodeFunctionCodesProp = xmlDocument . CreateElement ( "functionCode" ) ;
237
- xmlNodeFunctionCodesProp . InnerText = this . connectionPropertiesList [ i ] . FunctionPropertiesList [ j ] . FunctionCode . ToString ( ) ;
282
+ xmlNodeFunctionCodesProp = xmlDocument . CreateElement ( "functionCodeRead" ) ;
283
+ xmlNodeFunctionCodesProp . InnerText = this . connectionPropertiesList [ i ] . FunctionPropertiesList [ j ] . FunctionCodeRead . ToString ( ) ;
284
+ xmlNodeFunctionCodes . AppendChild ( xmlNodeFunctionCodesProp ) ;
285
+ xmlNodeFunctionCodesProp = xmlDocument . CreateElement ( "functionCodeWrite" ) ;
286
+ xmlNodeFunctionCodesProp . InnerText = this . connectionPropertiesList [ i ] . FunctionPropertiesList [ j ] . FunctionCodeWrite . ToString ( ) ;
238
287
xmlNodeFunctionCodes . AppendChild ( xmlNodeFunctionCodesProp ) ;
239
288
xmlNodeFunctionCodesProp = xmlDocument . CreateElement ( "quantity" ) ;
240
289
xmlNodeFunctionCodesProp . InnerText = this . connectionPropertiesList [ i ] . FunctionPropertiesList [ j ] . Quantity . ToString ( ) ;
@@ -295,10 +344,13 @@ public void ReadXML(DataGridView dataGridView)
295
344
xmlNodeList = xmlDocument . GetElementsByTagName ( "connection" ) ;
296
345
//connectionPropertiesList = new List<ConnectionProperties>();
297
346
this . connectionPropertiesList . Clear ( ) ;
347
+ int slotId = 0 ;
298
348
299
349
foreach ( XmlNode xmlNode in xmlNodeList )
300
350
{
301
351
ConnectionProperties connectionProperty = new ConnectionProperties ( ) ;
352
+ AddConnection ( connectionProperty ) ;
353
+
302
354
connectionProperty . ConnectionName = ( xmlNode [ "connectionName" ] . InnerText ) ;
303
355
connectionProperty . ModbusTCPAddress = ( xmlNode [ "ipAddress" ] . InnerText ) ;
304
356
connectionProperty . Port = Int32 . Parse ( xmlNode [ "port" ] . InnerText ) ;
@@ -309,33 +361,56 @@ public void ReadXML(DataGridView dataGridView)
309
361
{
310
362
xmlNodeList2 = xmlNode3 . ChildNodes ;
311
363
FunctionProperties functionProperty = new FunctionProperties ( ) ;
364
+
312
365
foreach ( XmlNode xmlNode2 in xmlNodeList2 )
313
366
{
314
- if ( xmlNode2 . Name == "functionCode " )
367
+ if ( xmlNode2 . Name == "functionCodeRead " )
315
368
switch ( xmlNode2 . InnerText )
316
369
{
317
370
case "ReadCoils" :
318
- functionProperty . FunctionCode = FunctionCode . ReadCoils ;
371
+ functionProperty . FunctionCodeRead = FunctionCodeRd . ReadCoils ;
319
372
break ;
320
373
case "ReadDiscreteInputs" :
321
- functionProperty . FunctionCode = FunctionCode . ReadDiscreteInputs ;
374
+ functionProperty . FunctionCodeRead = FunctionCodeRd . ReadDiscreteInputs ;
322
375
break ;
323
376
case "ReadHoldingRegisters" :
324
- functionProperty . FunctionCode = FunctionCode . ReadHoldingRegisters ;
377
+ functionProperty . FunctionCodeRead = FunctionCodeRd . ReadHoldingRegisters ;
325
378
break ;
326
379
case "ReadInputRegisters" :
327
- functionProperty . FunctionCode = FunctionCode . ReadInputRegisters ;
380
+ functionProperty . FunctionCodeRead = FunctionCodeRd . ReadInputRegisters ;
328
381
break ;
329
382
}
383
+ if ( xmlNode2 . Name == "functionCodeWrite" )
384
+ {
385
+ functionProperty . FunctionCodeWrite = FunctionCodeWr . WriteNone ;
386
+ switch ( xmlNode2 . InnerText )
387
+ {
388
+ case "WriteCoils" :
389
+ functionProperty . FunctionCodeWrite = FunctionCodeWr . WriteNone ;
390
+ break ;
391
+ case "WriteDiscreteInputs" :
392
+ functionProperty . FunctionCodeWrite = FunctionCodeWr . WriteNone ;
393
+ break ;
394
+ case "WriteHoldingRegisters" :
395
+ functionProperty . FunctionCodeWrite = FunctionCodeWr . WriteHoldingRegisters ;
396
+ break ;
397
+ case "WriteInputRegisters" :
398
+ functionProperty . FunctionCodeWrite = FunctionCodeWr . WriteNone ;
399
+ break ;
400
+ }
401
+
402
+ }
330
403
if ( xmlNode2 . Name == "startingAddress" )
331
404
functionProperty . StartingAdress = Int32 . Parse ( xmlNode2 . InnerText ) ;
332
405
if ( xmlNode2 . Name == "quantity" )
333
406
functionProperty . Quantity = Int32 . Parse ( xmlNode2 . InnerText ) ;
334
407
}
335
- connectionProperty . FunctionPropertiesList . Add ( functionProperty ) ;
408
+ //connectionProperty.FunctionPropertiesList.Add(functionProperty);
409
+ this . AddFunctionProperty ( functionProperty , slotId ) ;
410
+
336
411
xmlNode3 = xmlNode3 . NextSibling ;
337
412
}
338
- AddConnection ( connectionProperty ) ;
413
+ slotId ++ ;
339
414
//this.connectionPropertiesList.Add(connectionProperty);
340
415
}
341
416
if ( connectionPropertiesListChanged != null )
@@ -361,37 +436,59 @@ public void ReadXML(DataGridView dataGridView)
361
436
if ( xmlNode [ "columnDataType" ] != null )
362
437
dataGridView [ 3 , dataGridView . Rows . Count - 1 ] . Value = xmlNode [ "columnDataType" ] . InnerText ;
363
438
}
439
+
440
+ // trigger update of values manually
441
+ //this.valuesChanged(this);
442
+
364
443
dataGridView . AllowUserToAddRows = true ;
365
444
}
366
445
}
367
446
368
447
369
- public enum FunctionCode : int
448
+ public enum FunctionCodeRd : int
370
449
{
371
450
ReadCoils = 1 ,
372
451
ReadDiscreteInputs = 2 ,
373
452
ReadHoldingRegisters = 3 ,
374
453
ReadInputRegisters = 4 ,
375
454
} ;
376
-
377
-
378
- public class FunctionProperties
455
+
456
+ public enum FunctionCodeWr : int
457
+ {
458
+ WriteNone = 0 ,
459
+ WriteCoils = 1 ,
460
+ WriteDiscreteInputs = 2 ,
461
+ WriteHoldingRegisters = 3 ,
462
+ WriteInputRegisters = 4 ,
463
+ } ;
464
+
465
+
466
+ public class FunctionProperties
379
467
{
380
468
381
- FunctionCode funtionCode = FunctionCode . ReadCoils ;
382
-
383
- [ Browsable ( true ) ]
469
+ FunctionCodeRd functionCodeRd = FunctionCodeRd . ReadCoils ;
470
+ [ Browsable ( true ) ]
384
471
[ Category ( "Function code properties" ) ]
385
- [ Description ( "Function Code" ) ]
386
- [ DisplayName ( "Function Code" ) ]
387
- public FunctionCode FunctionCode
472
+ [ Description ( "Function Code Read " ) ]
473
+ [ DisplayName ( "Function Code Read " ) ]
474
+ public FunctionCodeRd FunctionCodeRead
388
475
{
389
- get { return funtionCode ; }
390
- set { funtionCode = value ; }
476
+ get { return functionCodeRd ; }
477
+ set { functionCodeRd = value ; }
391
478
}
392
-
393
-
394
- int startingAdress = 0 ;
479
+
480
+ FunctionCodeWr functionCodeWr = FunctionCodeWr . WriteNone ;
481
+ [ Browsable ( true ) ]
482
+ [ Category ( "Function code properties" ) ]
483
+ [ Description ( "Function Code Write" ) ]
484
+ [ DisplayName ( "Function Code Write" ) ]
485
+ public FunctionCodeWr FunctionCodeWrite
486
+ {
487
+ get { return functionCodeWr ; }
488
+ set { functionCodeWr = value ; }
489
+ }
490
+
491
+ int startingAdress = 0 ;
395
492
[ Browsable ( true ) ]
396
493
[ Category ( "Function code properties" ) ]
397
494
[ Description ( "Starting Address" ) ]
@@ -413,7 +510,29 @@ public int Quantity
413
510
get { return quantity ; }
414
511
set { quantity = value ; }
415
512
}
416
-
417
- public object values ;
513
+
514
+ int DataGridRowIdx = - 1 ;
515
+ [ Browsable ( false ) ]
516
+ [ Category ( "Function code properties" ) ]
517
+ [ Description ( "Data Grid Row Idx" ) ]
518
+ [ DisplayName ( "Data Grid Row Idx" ) ]
519
+ public int DataGridRow
520
+ {
521
+ get { return DataGridRowIdx ; }
522
+ set { DataGridRowIdx = value ; }
523
+ }
524
+
525
+ ConnectionProperties connection = null ;
526
+ [ Browsable ( false ) ]
527
+ [ Category ( "Function code properties" ) ]
528
+ [ Description ( "connection" ) ]
529
+ [ DisplayName ( "connection" ) ]
530
+ public ConnectionProperties Connection
531
+ {
532
+ get { return connection ; }
533
+ set { connection = value ; }
534
+ }
535
+
536
+ public object values ;
418
537
}
419
538
}
0 commit comments