@@ -290,8 +290,8 @@ public class ModbusServer
290
290
public bool FunctionCode16Disabled { get ; set ; }
291
291
public bool FunctionCode23Disabled { get ; set ; }
292
292
public bool PortChanged { get ; set ; }
293
- object lockCoils ;
294
- object lockHoldingRegisters ;
293
+ object lockCoils = new object ( ) ;
294
+ object lockHoldingRegisters = new object ( ) ;
295
295
296
296
#region events
297
297
public delegate void CoilsChanged ( int coil , int numberOfCoils ) ;
@@ -455,168 +455,171 @@ private void numberOfClientsChanged()
455
455
if ( numberOfConnectedClientsChanged != null )
456
456
numberOfConnectedClientsChanged ( ) ;
457
457
}
458
- #endregion
459
-
458
+ #endregion
459
+
460
+ object lockProcessReceivedData = new object ( ) ;
460
461
#region Method ProcessReceivedData
461
462
private void ProcessReceivedData ( object networkConnectionParameter )
462
463
{
463
- Byte [ ] bytes = new byte [ ( ( NetworkConnectionParameter ) networkConnectionParameter ) . bytes . Length ] ;
464
- if ( debug ) StoreLogData . Instance . Store ( "Received Data: " + BitConverter . ToString ( bytes ) , System . DateTime . Now ) ;
465
- NetworkStream stream = ( ( NetworkConnectionParameter ) networkConnectionParameter ) . stream ;
466
- int portIn = ( ( NetworkConnectionParameter ) networkConnectionParameter ) . portIn ;
467
- IPAddress ipAddressIn = ( ( NetworkConnectionParameter ) networkConnectionParameter ) . ipAddressIn ;
468
-
469
-
470
- Array . Copy ( ( ( NetworkConnectionParameter ) networkConnectionParameter ) . bytes , 0 , bytes , 0 , ( ( NetworkConnectionParameter ) networkConnectionParameter ) . bytes . Length ) ;
471
-
472
- ModbusProtocol receiveDataThread = new ModbusProtocol ( ) ;
473
- ModbusProtocol sendDataThread = new ModbusProtocol ( ) ;
474
-
475
- try
464
+ lock ( lockProcessReceivedData )
476
465
{
477
- UInt16 [ ] wordData = new UInt16 [ 1 ] ;
478
- byte [ ] byteData = new byte [ 2 ] ;
479
- receiveDataThread . timeStamp = DateTime . Now ;
480
- receiveDataThread . request = true ;
481
- if ( ! serialFlag )
482
- {
483
- //Lese Transaction identifier
484
- byteData [ 1 ] = bytes [ 0 ] ;
485
- byteData [ 0 ] = bytes [ 1 ] ;
486
- Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
487
- receiveDataThread . transactionIdentifier = wordData [ 0 ] ;
488
-
489
- //Lese Protocol identifier
490
- byteData [ 1 ] = bytes [ 2 ] ;
491
- byteData [ 0 ] = bytes [ 3 ] ;
492
- Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
493
- receiveDataThread . protocolIdentifier = wordData [ 0 ] ;
466
+ Byte [ ] bytes = new byte [ ( ( NetworkConnectionParameter ) networkConnectionParameter ) . bytes . Length ] ;
467
+ if ( debug ) StoreLogData . Instance . Store ( "Received Data: " + BitConverter . ToString ( bytes ) , System . DateTime . Now ) ;
468
+ NetworkStream stream = ( ( NetworkConnectionParameter ) networkConnectionParameter ) . stream ;
469
+ int portIn = ( ( NetworkConnectionParameter ) networkConnectionParameter ) . portIn ;
470
+ IPAddress ipAddressIn = ( ( NetworkConnectionParameter ) networkConnectionParameter ) . ipAddressIn ;
494
471
495
- //Lese length
496
- byteData [ 1 ] = bytes [ 4 ] ;
497
- byteData [ 0 ] = bytes [ 5 ] ;
498
- Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
499
- receiveDataThread . length = wordData [ 0 ] ;
500
- }
501
-
502
- //Lese unit identifier
503
- receiveDataThread . unitIdentifier = bytes [ 6 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
504
- //Check UnitIdentifier
505
- if ( ( receiveDataThread . unitIdentifier != this . unitIdentifier ) & ( receiveDataThread . unitIdentifier != 0 ) )
506
- return ;
507
472
508
- // Lese function code
509
- receiveDataThread . functionCode = bytes [ 7 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
473
+ Array . Copy ( ( ( NetworkConnectionParameter ) networkConnectionParameter ) . bytes , 0 , bytes , 0 , ( ( NetworkConnectionParameter ) networkConnectionParameter ) . bytes . Length ) ;
510
474
511
- // Lese starting address
512
- byteData [ 1 ] = bytes [ 8 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
513
- byteData [ 0 ] = bytes [ 9 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
514
- Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
515
- receiveDataThread . startingAdress = wordData [ 0 ] ;
475
+ ModbusProtocol receiveDataThread = new ModbusProtocol ( ) ;
476
+ ModbusProtocol sendDataThread = new ModbusProtocol ( ) ;
516
477
517
- if ( receiveDataThread . functionCode <= 4 )
518
- {
519
- // Lese quantity
520
- byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
521
- byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
522
- Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
523
- receiveDataThread . quantity = wordData [ 0 ] ;
524
- }
525
- if ( receiveDataThread . functionCode == 5 )
526
- {
527
- receiveDataThread . receiveCoilValues = new ushort [ 1 ] ;
528
- // Lese Value
529
- byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
530
- byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
531
- Buffer . BlockCopy ( byteData , 0 , receiveDataThread . receiveCoilValues , 0 , 2 ) ;
532
- }
533
- if ( receiveDataThread . functionCode == 6 )
534
- {
535
- receiveDataThread . receiveRegisterValues = new ushort [ 1 ] ;
536
- // Lese Value
537
- byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
538
- byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
539
- Buffer . BlockCopy ( byteData , 0 , receiveDataThread . receiveRegisterValues , 0 , 2 ) ;
540
- }
541
- if ( receiveDataThread . functionCode == 15 )
478
+ try
542
479
{
543
- // Lese quantity
544
- byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
545
- byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
546
- Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
547
- receiveDataThread . quantity = wordData [ 0 ] ;
480
+ UInt16 [ ] wordData = new UInt16 [ 1 ] ;
481
+ byte [ ] byteData = new byte [ 2 ] ;
482
+ receiveDataThread . timeStamp = DateTime . Now ;
483
+ receiveDataThread . request = true ;
484
+ if ( ! serialFlag )
485
+ {
486
+ //Lese Transaction identifier
487
+ byteData [ 1 ] = bytes [ 0 ] ;
488
+ byteData [ 0 ] = bytes [ 1 ] ;
489
+ Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
490
+ receiveDataThread . transactionIdentifier = wordData [ 0 ] ;
491
+
492
+ //Lese Protocol identifier
493
+ byteData [ 1 ] = bytes [ 2 ] ;
494
+ byteData [ 0 ] = bytes [ 3 ] ;
495
+ Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
496
+ receiveDataThread . protocolIdentifier = wordData [ 0 ] ;
497
+
498
+ //Lese length
499
+ byteData [ 1 ] = bytes [ 4 ] ;
500
+ byteData [ 0 ] = bytes [ 5 ] ;
501
+ Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
502
+ receiveDataThread . length = wordData [ 0 ] ;
503
+ }
548
504
549
- receiveDataThread . byteCount = bytes [ 12 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
505
+ //Lese unit identifier
506
+ receiveDataThread . unitIdentifier = bytes [ 6 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
507
+ //Check UnitIdentifier
508
+ if ( ( receiveDataThread . unitIdentifier != this . unitIdentifier ) & ( receiveDataThread . unitIdentifier != 0 ) )
509
+ return ;
550
510
551
- if ( ( receiveDataThread . byteCount % 2 ) != 0 )
552
- receiveDataThread . receiveCoilValues = new ushort [ receiveDataThread . byteCount / 2 + 1 ] ;
553
- else
554
- receiveDataThread . receiveCoilValues = new ushort [ receiveDataThread . byteCount / 2 ] ;
555
- // Lese Value
556
- Buffer . BlockCopy ( bytes , 13 - 6 * Convert . ToInt32 ( serialFlag ) , receiveDataThread . receiveCoilValues , 0 , receiveDataThread . byteCount ) ;
557
- }
558
- if ( receiveDataThread . functionCode == 16 )
559
- {
560
- // Lese quantity
561
- byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
562
- byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
511
+ // Lese function code
512
+ receiveDataThread . functionCode = bytes [ 7 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
513
+
514
+ // Lese starting address
515
+ byteData [ 1 ] = bytes [ 8 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
516
+ byteData [ 0 ] = bytes [ 9 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
563
517
Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
564
- receiveDataThread . quantity = wordData [ 0 ] ;
518
+ receiveDataThread . startingAdress = wordData [ 0 ] ;
565
519
566
- receiveDataThread . byteCount = bytes [ 12 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
567
- receiveDataThread . receiveRegisterValues = new ushort [ receiveDataThread . quantity ] ;
568
- for ( int i = 0 ; i < receiveDataThread . quantity ; i ++ )
520
+ if ( receiveDataThread . functionCode <= 4 )
521
+ {
522
+ // Lese quantity
523
+ byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
524
+ byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
525
+ Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
526
+ receiveDataThread . quantity = wordData [ 0 ] ;
527
+ }
528
+ if ( receiveDataThread . functionCode == 5 )
529
+ {
530
+ receiveDataThread . receiveCoilValues = new ushort [ 1 ] ;
531
+ // Lese Value
532
+ byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
533
+ byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
534
+ Buffer . BlockCopy ( byteData , 0 , receiveDataThread . receiveCoilValues , 0 , 2 ) ;
535
+ }
536
+ if ( receiveDataThread . functionCode == 6 )
569
537
{
538
+ receiveDataThread . receiveRegisterValues = new ushort [ 1 ] ;
570
539
// Lese Value
571
- byteData [ 1 ] = bytes [ 13 + i * 2 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
572
- byteData [ 0 ] = bytes [ 14 + i * 2 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
573
- Buffer . BlockCopy ( byteData , 0 , receiveDataThread . receiveRegisterValues , i * 2 , 2 ) ;
540
+ byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
541
+ byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
542
+ Buffer . BlockCopy ( byteData , 0 , receiveDataThread . receiveRegisterValues , 0 , 2 ) ;
574
543
}
544
+ if ( receiveDataThread . functionCode == 15 )
545
+ {
546
+ // Lese quantity
547
+ byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
548
+ byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
549
+ Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
550
+ receiveDataThread . quantity = wordData [ 0 ] ;
575
551
576
- }
577
- if ( receiveDataThread . functionCode == 23 )
578
- {
579
- // Lese starting Address Read
580
- byteData [ 1 ] = bytes [ 8 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
581
- byteData [ 0 ] = bytes [ 9 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
582
- Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
583
- receiveDataThread . startingAddressRead = wordData [ 0 ] ;
584
- // Lese quantity Read
585
- byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
586
- byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
587
- Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
588
- receiveDataThread . quantityRead = wordData [ 0 ] ;
589
- // Lese starting Address Write
590
- byteData [ 1 ] = bytes [ 12 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
591
- byteData [ 0 ] = bytes [ 13 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
592
- Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
593
- receiveDataThread . startingAddressWrite = wordData [ 0 ] ;
594
- // Lese quantity Write
595
- byteData [ 1 ] = bytes [ 14 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
596
- byteData [ 0 ] = bytes [ 15 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
597
- Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
598
- receiveDataThread . quantityWrite = wordData [ 0 ] ;
552
+ receiveDataThread . byteCount = bytes [ 12 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
599
553
600
- receiveDataThread . byteCount = bytes [ 16 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
601
- receiveDataThread . receiveRegisterValues = new ushort [ receiveDataThread . quantityWrite ] ;
602
- for ( int i = 0 ; i < receiveDataThread . quantityWrite ; i ++ )
603
- {
554
+ if ( ( receiveDataThread . byteCount % 2 ) != 0 )
555
+ receiveDataThread . receiveCoilValues = new ushort [ receiveDataThread . byteCount / 2 + 1 ] ;
556
+ else
557
+ receiveDataThread . receiveCoilValues = new ushort [ receiveDataThread . byteCount / 2 ] ;
604
558
// Lese Value
605
- byteData [ 1 ] = bytes [ 17 + i * 2 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
606
- byteData [ 0 ] = bytes [ 18 + i * 2 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
607
- Buffer . BlockCopy ( byteData , 0 , receiveDataThread . receiveRegisterValues , i * 2 , 2 ) ;
559
+ Buffer . BlockCopy ( bytes , 13 - 6 * Convert . ToInt32 ( serialFlag ) , receiveDataThread . receiveCoilValues , 0 , receiveDataThread . byteCount ) ;
560
+ }
561
+ if ( receiveDataThread . functionCode == 16 )
562
+ {
563
+ // Lese quantity
564
+ byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
565
+ byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
566
+ Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
567
+ receiveDataThread . quantity = wordData [ 0 ] ;
568
+
569
+ receiveDataThread . byteCount = bytes [ 12 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
570
+ receiveDataThread . receiveRegisterValues = new ushort [ receiveDataThread . quantity ] ;
571
+ for ( int i = 0 ; i < receiveDataThread . quantity ; i ++ )
572
+ {
573
+ // Lese Value
574
+ byteData [ 1 ] = bytes [ 13 + i * 2 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
575
+ byteData [ 0 ] = bytes [ 14 + i * 2 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
576
+ Buffer . BlockCopy ( byteData , 0 , receiveDataThread . receiveRegisterValues , i * 2 , 2 ) ;
577
+ }
578
+
579
+ }
580
+ if ( receiveDataThread . functionCode == 23 )
581
+ {
582
+ // Lese starting Address Read
583
+ byteData [ 1 ] = bytes [ 8 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
584
+ byteData [ 0 ] = bytes [ 9 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
585
+ Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
586
+ receiveDataThread . startingAddressRead = wordData [ 0 ] ;
587
+ // Lese quantity Read
588
+ byteData [ 1 ] = bytes [ 10 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
589
+ byteData [ 0 ] = bytes [ 11 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
590
+ Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
591
+ receiveDataThread . quantityRead = wordData [ 0 ] ;
592
+ // Lese starting Address Write
593
+ byteData [ 1 ] = bytes [ 12 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
594
+ byteData [ 0 ] = bytes [ 13 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
595
+ Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
596
+ receiveDataThread . startingAddressWrite = wordData [ 0 ] ;
597
+ // Lese quantity Write
598
+ byteData [ 1 ] = bytes [ 14 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
599
+ byteData [ 0 ] = bytes [ 15 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
600
+ Buffer . BlockCopy ( byteData , 0 , wordData , 0 , 2 ) ;
601
+ receiveDataThread . quantityWrite = wordData [ 0 ] ;
602
+
603
+ receiveDataThread . byteCount = bytes [ 16 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
604
+ receiveDataThread . receiveRegisterValues = new ushort [ receiveDataThread . quantityWrite ] ;
605
+ for ( int i = 0 ; i < receiveDataThread . quantityWrite ; i ++ )
606
+ {
607
+ // Lese Value
608
+ byteData [ 1 ] = bytes [ 17 + i * 2 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
609
+ byteData [ 0 ] = bytes [ 18 + i * 2 - 6 * Convert . ToInt32 ( serialFlag ) ] ;
610
+ Buffer . BlockCopy ( byteData , 0 , receiveDataThread . receiveRegisterValues , i * 2 , 2 ) ;
611
+ }
608
612
}
609
613
}
610
- }
611
- catch ( Exception exc )
612
- { }
613
- this . CreateAnswer ( receiveDataThread , sendDataThread , stream , portIn , ipAddressIn ) ;
614
+ catch ( Exception exc )
615
+ { }
616
+ this . CreateAnswer ( receiveDataThread , sendDataThread , stream , portIn , ipAddressIn ) ;
614
617
//this.sendAnswer();
615
618
this . CreateLogData ( receiveDataThread , sendDataThread ) ;
616
619
617
620
if ( logDataChanged != null )
618
621
logDataChanged ( ) ;
619
-
622
+ }
620
623
}
621
624
#endregion
622
625
0 commit comments