@@ -66,7 +66,7 @@ internal class TCPHandler
66
66
public event NumberOfClientsChanged numberOfClientsChanged ;
67
67
68
68
TcpListener server = null ;
69
- NetworkConnectionParameter networkConnectionParameter ;
69
+
70
70
71
71
private List < Client > tcpClientLastRequestList = new List < Client > ( ) ;
72
72
@@ -154,6 +154,7 @@ private int GetAndCleanNumberOfConnectedClients(Client client)
154
154
155
155
private void ReadCallback ( IAsyncResult asyncResult )
156
156
{
157
+ NetworkConnectionParameter networkConnectionParameter = new NetworkConnectionParameter ( ) ;
157
158
Client client = asyncResult . AsyncState as Client ;
158
159
client . Ticks = DateTime . Now . Ticks ;
159
160
NumberOfConnectedClients = GetAndCleanNumberOfConnectedClients ( client ) ;
@@ -289,6 +290,8 @@ public class ModbusServer
289
290
public bool FunctionCode16Disabled { get ; set ; }
290
291
public bool FunctionCode23Disabled { get ; set ; }
291
292
public bool PortChanged { get ; set ; }
293
+ object lockCoils ;
294
+ object lockHoldingRegisters ;
292
295
293
296
#region events
294
297
public delegate void CoilsChanged ( int coil , int numberOfCoils ) ;
@@ -767,7 +770,8 @@ private void ReadCoils(ModbusProtocol receiveData, ModbusProtocol sendData, Netw
767
770
sendData . byteCount = ( byte ) ( receiveData . quantity / 8 + 1 ) ;
768
771
769
772
sendData . sendCoilValues = new bool [ receiveData . quantity ] ;
770
- Array . Copy ( coils , receiveData . startingAdress + 1 , sendData . sendCoilValues , 0 , receiveData . quantity ) ;
773
+ lock ( lockCoils )
774
+ Array . Copy ( coils , receiveData . startingAdress + 1 , sendData . sendCoilValues , 0 , receiveData . quantity ) ;
771
775
}
772
776
if ( true )
773
777
{
@@ -1016,7 +1020,8 @@ private void ReadHoldingRegisters(ModbusProtocol receiveData, ModbusProtocol sen
1016
1020
{
1017
1021
sendData . byteCount = ( byte ) ( 2 * receiveData . quantity ) ;
1018
1022
sendData . sendRegisterValues = new Int16 [ receiveData . quantity ] ;
1019
- Buffer . BlockCopy ( holdingRegisters , receiveData . startingAdress * 2 + 2 , sendData . sendRegisterValues , 0 , receiveData . quantity * 2 ) ;
1023
+ lock ( lockHoldingRegisters )
1024
+ Buffer . BlockCopy ( holdingRegisters , receiveData . startingAdress * 2 + 2 , sendData . sendRegisterValues , 0 , receiveData . quantity * 2 ) ;
1020
1025
}
1021
1026
if ( sendData . exceptionCode > 0 )
1022
1027
sendData . length = 0x03 ;
@@ -1250,11 +1255,13 @@ private void WriteSingleCoil(ModbusProtocol receiveData, ModbusProtocol sendData
1250
1255
{
1251
1256
if ( receiveData . receiveCoilValues [ 0 ] == 0xFF00 )
1252
1257
{
1253
- coils [ receiveData . startingAdress + 1 ] = true ;
1258
+ lock ( lockCoils )
1259
+ coils [ receiveData . startingAdress + 1 ] = true ;
1254
1260
}
1255
1261
if ( receiveData . receiveCoilValues [ 0 ] == 0x0000 )
1256
1262
{
1257
- coils [ receiveData . startingAdress + 1 ] = false ;
1263
+ lock ( lockCoils )
1264
+ coils [ receiveData . startingAdress + 1 ] = false ;
1258
1265
}
1259
1266
}
1260
1267
if ( sendData . exceptionCode > 0 )
@@ -1376,7 +1383,8 @@ private void WriteSingleRegister(ModbusProtocol receiveData, ModbusProtocol send
1376
1383
}
1377
1384
if ( sendData . exceptionCode == 0 )
1378
1385
{
1379
- holdingRegisters [ receiveData . startingAdress + 1 ] = unchecked ( ( short ) receiveData . receiveRegisterValues [ 0 ] ) ;
1386
+ lock ( lockHoldingRegisters )
1387
+ holdingRegisters [ receiveData . startingAdress + 1 ] = unchecked ( ( short ) receiveData . receiveRegisterValues [ 0 ] ) ;
1380
1388
}
1381
1389
if ( sendData . exceptionCode > 0 )
1382
1390
sendData . length = 0x03 ;
@@ -1498,24 +1506,27 @@ private void WriteMultipleCoils(ModbusProtocol receiveData, ModbusProtocol sendD
1498
1506
}
1499
1507
if ( sendData . exceptionCode == 0 )
1500
1508
{
1501
- for ( int i = 0 ; i < receiveData . quantity ; i ++ )
1502
- {
1503
- int shift = i % 16 ;
1509
+ lock ( lockCoils )
1510
+ for ( int i = 0 ; i < receiveData . quantity ; i ++ )
1511
+ {
1512
+ int shift = i % 16 ;
1504
1513
/* if ((i == receiveData.quantity - 1) & (receiveData.quantity % 2 != 0))
1505
1514
{
1506
1515
if (shift < 8)
1507
1516
shift = shift + 8;
1508
1517
else
1509
1518
shift = shift - 8;
1510
1519
}*/
1511
- int mask = 0x1 ;
1512
- mask = mask << ( shift ) ;
1513
- if ( ( receiveData . receiveCoilValues [ i / 16 ] & ( ushort ) mask ) == 0 )
1514
- coils [ receiveData . startingAdress + i + 1 ] = false ;
1515
- else
1516
- coils [ receiveData . startingAdress + i + 1 ] = true ;
1520
+ int mask = 0x1 ;
1521
+ mask = mask << ( shift ) ;
1522
+ if ( ( receiveData . receiveCoilValues [ i / 16 ] & ( ushort ) mask ) == 0 )
1523
+
1524
+ coils [ receiveData . startingAdress + i + 1 ] = false ;
1525
+ else
1526
+
1527
+ coils [ receiveData . startingAdress + i + 1 ] = true ;
1517
1528
1518
- }
1529
+ }
1519
1530
}
1520
1531
if ( sendData . exceptionCode > 0 )
1521
1532
sendData . length = 0x03 ;
@@ -1635,10 +1646,11 @@ private void WriteMultipleRegisters(ModbusProtocol receiveData, ModbusProtocol s
1635
1646
}
1636
1647
if ( sendData . exceptionCode == 0 )
1637
1648
{
1638
- for ( int i = 0 ; i < receiveData . quantity ; i ++ )
1639
- {
1640
- holdingRegisters [ receiveData . startingAdress + i + 1 ] = unchecked ( ( short ) receiveData . receiveRegisterValues [ i ] ) ;
1641
- }
1649
+ lock ( lockHoldingRegisters )
1650
+ for ( int i = 0 ; i < receiveData . quantity ; i ++ )
1651
+ {
1652
+ holdingRegisters [ receiveData . startingAdress + i + 1 ] = unchecked ( ( short ) receiveData . receiveRegisterValues [ i ] ) ;
1653
+ }
1642
1654
}
1643
1655
if ( sendData . exceptionCode > 0 )
1644
1656
sendData . length = 0x03 ;
@@ -1758,12 +1770,14 @@ private void ReadWriteMultipleRegisters(ModbusProtocol receiveData, ModbusProtoc
1758
1770
if ( sendData . exceptionCode == 0 )
1759
1771
{
1760
1772
sendData . sendRegisterValues = new Int16 [ receiveData . quantityRead ] ;
1761
- Buffer . BlockCopy ( holdingRegisters , receiveData . startingAddressRead * 2 + 2 , sendData . sendRegisterValues , 0 , receiveData . quantityRead * 2 ) ;
1773
+ lock ( lockHoldingRegisters )
1774
+ Buffer . BlockCopy ( holdingRegisters , receiveData . startingAddressRead * 2 + 2 , sendData . sendRegisterValues , 0 , receiveData . quantityRead * 2 ) ;
1762
1775
1763
- for ( int i = 0 ; i < receiveData . quantityWrite ; i ++ )
1764
- {
1765
- holdingRegisters [ receiveData . startingAddressWrite + i + 1 ] = unchecked ( ( short ) receiveData . receiveRegisterValues [ i ] ) ;
1766
- }
1776
+ lock ( holdingRegisters )
1777
+ for ( int i = 0 ; i < receiveData . quantityWrite ; i ++ )
1778
+ {
1779
+ holdingRegisters [ receiveData . startingAddressWrite + i + 1 ] = unchecked ( ( short ) receiveData . receiveRegisterValues [ i ] ) ;
1780
+ }
1767
1781
sendData . byteCount = ( byte ) ( 2 * receiveData . quantityRead ) ;
1768
1782
}
1769
1783
if ( sendData . exceptionCode > 0 )
0 commit comments