@@ -66,7 +66,7 @@ internal class TCPHandler
6666 public event NumberOfClientsChanged numberOfClientsChanged ;
6767
6868 TcpListener server = null ;
69- NetworkConnectionParameter networkConnectionParameter ;
69+
7070
7171 private List < Client > tcpClientLastRequestList = new List < Client > ( ) ;
7272
@@ -154,6 +154,7 @@ private int GetAndCleanNumberOfConnectedClients(Client client)
154154
155155 private void ReadCallback ( IAsyncResult asyncResult )
156156 {
157+ NetworkConnectionParameter networkConnectionParameter = new NetworkConnectionParameter ( ) ;
157158 Client client = asyncResult . AsyncState as Client ;
158159 client . Ticks = DateTime . Now . Ticks ;
159160 NumberOfConnectedClients = GetAndCleanNumberOfConnectedClients ( client ) ;
@@ -289,6 +290,8 @@ public class ModbusServer
289290 public bool FunctionCode16Disabled { get ; set ; }
290291 public bool FunctionCode23Disabled { get ; set ; }
291292 public bool PortChanged { get ; set ; }
293+ object lockCoils ;
294+ object lockHoldingRegisters ;
292295
293296 #region events
294297 public delegate void CoilsChanged ( int coil , int numberOfCoils ) ;
@@ -767,7 +770,8 @@ private void ReadCoils(ModbusProtocol receiveData, ModbusProtocol sendData, Netw
767770 sendData . byteCount = ( byte ) ( receiveData . quantity / 8 + 1 ) ;
768771
769772 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 ) ;
771775 }
772776 if ( true )
773777 {
@@ -1016,7 +1020,8 @@ private void ReadHoldingRegisters(ModbusProtocol receiveData, ModbusProtocol sen
10161020 {
10171021 sendData . byteCount = ( byte ) ( 2 * receiveData . quantity ) ;
10181022 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 ) ;
10201025 }
10211026 if ( sendData . exceptionCode > 0 )
10221027 sendData . length = 0x03 ;
@@ -1250,11 +1255,13 @@ private void WriteSingleCoil(ModbusProtocol receiveData, ModbusProtocol sendData
12501255 {
12511256 if ( receiveData . receiveCoilValues [ 0 ] == 0xFF00 )
12521257 {
1253- coils [ receiveData . startingAdress + 1 ] = true ;
1258+ lock ( lockCoils )
1259+ coils [ receiveData . startingAdress + 1 ] = true ;
12541260 }
12551261 if ( receiveData . receiveCoilValues [ 0 ] == 0x0000 )
12561262 {
1257- coils [ receiveData . startingAdress + 1 ] = false ;
1263+ lock ( lockCoils )
1264+ coils [ receiveData . startingAdress + 1 ] = false ;
12581265 }
12591266 }
12601267 if ( sendData . exceptionCode > 0 )
@@ -1376,7 +1383,8 @@ private void WriteSingleRegister(ModbusProtocol receiveData, ModbusProtocol send
13761383 }
13771384 if ( sendData . exceptionCode == 0 )
13781385 {
1379- holdingRegisters [ receiveData . startingAdress + 1 ] = unchecked ( ( short ) receiveData . receiveRegisterValues [ 0 ] ) ;
1386+ lock ( lockHoldingRegisters )
1387+ holdingRegisters [ receiveData . startingAdress + 1 ] = unchecked ( ( short ) receiveData . receiveRegisterValues [ 0 ] ) ;
13801388 }
13811389 if ( sendData . exceptionCode > 0 )
13821390 sendData . length = 0x03 ;
@@ -1498,24 +1506,27 @@ private void WriteMultipleCoils(ModbusProtocol receiveData, ModbusProtocol sendD
14981506 }
14991507 if ( sendData . exceptionCode == 0 )
15001508 {
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 ;
15041513 /* if ((i == receiveData.quantity - 1) & (receiveData.quantity % 2 != 0))
15051514 {
15061515 if (shift < 8)
15071516 shift = shift + 8;
15081517 else
15091518 shift = shift - 8;
15101519 }*/
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 ;
15171528
1518- }
1529+ }
15191530 }
15201531 if ( sendData . exceptionCode > 0 )
15211532 sendData . length = 0x03 ;
@@ -1635,10 +1646,11 @@ private void WriteMultipleRegisters(ModbusProtocol receiveData, ModbusProtocol s
16351646 }
16361647 if ( sendData . exceptionCode == 0 )
16371648 {
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+ }
16421654 }
16431655 if ( sendData . exceptionCode > 0 )
16441656 sendData . length = 0x03 ;
@@ -1758,12 +1770,14 @@ private void ReadWriteMultipleRegisters(ModbusProtocol receiveData, ModbusProtoc
17581770 if ( sendData . exceptionCode == 0 )
17591771 {
17601772 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 ) ;
17621775
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+ }
17671781 sendData . byteCount = ( byte ) ( 2 * receiveData . quantityRead ) ;
17681782 }
17691783 if ( sendData . exceptionCode > 0 )
0 commit comments