@@ -321,7 +321,7 @@ public BooleanRegisterDefinition? SelectedCoil
321321 /// Gets the collection of coil definitions from the slave service
322322 /// </summary>
323323 public ObservableCollection < BooleanRegisterDefinition > CoilDefinitions => _slaveService . CoilDefinitions ;
324-
324+
325325 /// <summary>
326326 /// Gets the command to add a new coil in slave mode
327327 /// </summary>
@@ -1559,7 +1559,7 @@ private void ExportEvents()
15591559 FileName = $ "ModbusTerm_Log_{ DateTime . Now : yyyy-MM-dd_HHmm} "
15601560 } ;
15611561
1562- // Show the dialog and get result
1562+ // Show dialog and get result
15631563 if ( saveFileDialog . ShowDialog ( ) == true )
15641564 {
15651565 string filePath = saveFileDialog . FileName ;
@@ -1982,12 +1982,20 @@ private void AddRegister()
19821982 {
19831983 try
19841984 {
1985- // Create a new register with default values (address will be set by UpdateHoldingRegisterAddresses)
1985+ // Find the next available address based on existing registers and their register counts
1986+ ushort nextAddress = 0 ;
1987+ if ( _slaveService . RegisterDefinitions . Count > 0 )
1988+ {
1989+ var lastRegister = _slaveService . RegisterDefinitions . OrderBy ( r => r . Address ) . Last ( ) ;
1990+ nextAddress = ( ushort ) ( lastRegister . Address + lastRegister . RegisterCount ) ;
1991+ }
1992+
1993+ // Create a new register with the calculated address
19861994 var newRegister = new RegisterDefinition
19871995 {
1988- Address = 0 , // Temporary address, will be updated
1996+ Address = nextAddress ,
19891997 Value = 0 ,
1990- Name = $ "Register 0 ",
1998+ Name = $ "Register { nextAddress } ",
19911999 Description = "New holding register" ,
19922000 DataType = ModbusDataType . UInt16 ,
19932001 // Ensure the new register doesn't have IsRecentlyModified set
@@ -2000,7 +2008,7 @@ private void AddRegister()
20002008
20012009 try
20022010 {
2003- // Add to the service's collection (this must happen first)
2011+ // Add to the service's collection
20042012 _slaveService . RegisterDefinitions . Add ( newRegister ) ;
20052013
20062014 // Hook up property change notification
@@ -2009,12 +2017,6 @@ private void AddRegister()
20092017 notifyPropertyChanged . PropertyChanged += Register_PropertyChanged ;
20102018 }
20112019
2012- // Update all register addresses after adding
2013- UpdateHoldingRegisterAddresses ( ) ;
2014-
2015- // Update the register name with the correct address
2016- newRegister . Name = $ "Register { newRegister . Address } ";
2017-
20182020 // Update the service's data store
20192021 _slaveService . UpdateRegisterValue ( newRegister ) ;
20202022
@@ -2045,12 +2047,20 @@ private void AddInputRegister()
20452047 {
20462048 try
20472049 {
2048- // Create a new register with default values (address will be set by UpdateInputRegisterAddresses)
2050+ // Find the next available address based on existing registers and their register counts
2051+ ushort nextAddress = 0 ;
2052+ if ( _slaveService . InputRegisterDefinitions . Count > 0 )
2053+ {
2054+ var lastRegister = _slaveService . InputRegisterDefinitions . OrderBy ( r => r . Address ) . Last ( ) ;
2055+ nextAddress = ( ushort ) ( lastRegister . Address + lastRegister . RegisterCount ) ;
2056+ }
2057+
2058+ // Create a new register with the calculated address
20492059 var newRegister = new RegisterDefinition
20502060 {
2051- Address = 0 , // Temporary address, will be updated
2061+ Address = nextAddress ,
20522062 Value = 0 ,
2053- Name = $ "Input 0 ",
2063+ Name = $ "Input { nextAddress } ",
20542064 Description = "New input register" ,
20552065 DataType = ModbusDataType . UInt16
20562066 } ;
@@ -2064,12 +2074,6 @@ private void AddInputRegister()
20642074 notifyPropertyChanged . PropertyChanged += InputRegister_PropertyChanged ;
20652075 }
20662076
2067- // Update all input register addresses after adding
2068- UpdateInputRegisterAddresses ( ) ;
2069-
2070- // Update the register name with the correct address
2071- newRegister . Name = $ "Input { newRegister . Address } ";
2072-
20732077 // Update the service's data store
20742078 _slaveService . UpdateInputRegisterValue ( newRegister ) ;
20752079
@@ -2111,9 +2115,6 @@ private void RemoveInputRegister()
21112115 notifyPropertyChanged . PropertyChanged -= InputRegister_PropertyChanged ;
21122116 }
21132117
2114- // Notify UI
2115- OnPropertyChanged ( nameof ( HasInputRegisters ) ) ;
2116-
21172118 OnCommunicationEvent ( this , CommunicationEvent . CreateInfoEvent ( $ "Removed input register at address { registerToRemove . Address } ") ) ;
21182119 }
21192120 catch ( Exception ex )
@@ -2145,9 +2146,6 @@ private void ClearInputRegisters()
21452146 _slaveService . InputRegisterDefinitions . Remove ( register ) ;
21462147 }
21472148
2148- // Notify UI
2149- OnPropertyChanged ( nameof ( HasInputRegisters ) ) ;
2150-
21512149 OnCommunicationEvent ( this , CommunicationEvent . CreateInfoEvent ( "All input registers cleared" ) ) ;
21522150 }
21532151 catch ( Exception ex )
@@ -2213,11 +2211,7 @@ private void RemoveRegister()
22132211 // Remove from service
22142212 _slaveService . RemoveRegister ( registerToRemove ) ;
22152213
2216- // Update all register addresses after removal
2217- UpdateHoldingRegisterAddresses ( ) ;
2218-
2219- // Notify UI
2220- OnPropertyChanged ( nameof ( HasRegisters ) ) ;
2214+ OnCommunicationEvent ( this , CommunicationEvent . CreateInfoEvent ( $ "Removed register at address { registerToRemove . Address } ") ) ;
22212215 }
22222216 catch ( Exception ex )
22232217 {
@@ -2242,9 +2236,6 @@ private void ClearRegisters()
22422236 _slaveService . RemoveRegister ( register ) ;
22432237 }
22442238
2245- // Notify UI
2246- OnPropertyChanged ( nameof ( HasRegisters ) ) ;
2247-
22482239 OnCommunicationEvent ( this , CommunicationEvent . CreateInfoEvent ( "All registers cleared" ) ) ;
22492240 }
22502241 catch ( Exception ex )
@@ -2596,19 +2587,19 @@ private void InputRegister_PropertyChanged(object? sender, PropertyChangedEventA
25962587 try
25972588 {
25982589 _slaveService . UpdateInputRegisterValue ( register ) ;
2599-
2600- // If the data type or register count changed and we're not already updating addresses, update all addresses
2601- if ( ( e . PropertyName == nameof ( RegisterDefinition . DataType ) || e . PropertyName == nameof ( RegisterDefinition . RegisterCount ) ) && ! _isUpdatingAddresses )
2602- {
2603- UpdateInputRegisterAddresses ( ) ;
2604- }
26052590 }
26062591 catch ( Exception ex )
26072592 {
26082593 // Log the error but don't crash
26092594 OnCommunicationEvent ( this , CommunicationEvent . CreateErrorEvent ( $ "Failed to update input register { register . Address } : { ex . Message } ") ) ;
26102595 }
26112596 }
2597+
2598+ // If the data type or register count changed and we're not already updating addresses, update all addresses
2599+ if ( sender is RegisterDefinition && ( e . PropertyName == nameof ( RegisterDefinition . DataType ) || e . PropertyName == nameof ( RegisterDefinition . RegisterCount ) ) && ! _isUpdatingAddresses )
2600+ {
2601+ UpdateInputRegisterAddresses ( ) ;
2602+ }
26122603 }
26132604
26142605 /// <summary>
@@ -3305,10 +3296,6 @@ private async void LoadProfilesAsync()
33053296
33063297 // Methods for coils are implemented earlier in this file.
33073298
3308- // Import and export methods for coils are implemented earlier in this file.
3309-
3310- // Methods for discrete inputs are implemented earlier in this file.
3311-
33123299 /// <summary>
33133300 /// Loads a profile by name
33143301 /// </summary>
0 commit comments