@@ -218,5 +218,163 @@ void TxtSlaveAddressInputTextChanged(object sender, EventArgs e)
218218 {
219219 modbusClient . UnitIdentifier = byte . Parse ( txtSlaveAddressInput . Text ) ;
220220 }
221- }
221+
222+ bool listBoxPrepareCoils = false ;
223+ private void btnPrepareCoils_Click ( object sender , EventArgs e )
224+ {
225+ if ( ! listBoxPrepareCoils )
226+ {
227+ lsbAnswerFromServer . Items . Clear ( ) ;
228+ }
229+ listBoxPrepareCoils = true ;
230+ listBoxPrepareRegisters = false ;
231+ lsbAnswerFromServer . Items . Add ( txtCoilValue . Text ) ;
232+
233+ }
234+ bool listBoxPrepareRegisters = false ;
235+ private void button1_Click ( object sender , EventArgs e )
236+ {
237+ if ( ! listBoxPrepareRegisters )
238+ {
239+ lsbAnswerFromServer . Items . Clear ( ) ;
240+ }
241+ listBoxPrepareRegisters = true ;
242+ listBoxPrepareCoils = false ;
243+ lsbAnswerFromServer . Items . Add ( int . Parse ( txtRegisterValue . Text ) ) ;
244+ }
245+
246+ private void btnWriteSingleCoil_Click ( object sender , EventArgs e )
247+ {
248+ try
249+ {
250+ if ( ! modbusClient . Connected )
251+ {
252+ modbusClient . IPAddress = txtIpAddressInput . Text ;
253+ modbusClient . Port = int . Parse ( txtPortInput . Text ) ;
254+ modbusClient . Connect ( ) ;
255+ }
256+
257+ bool coilsToSend = false ;
258+
259+ coilsToSend = bool . Parse ( lsbAnswerFromServer . Items [ 0 ] . ToString ( ) ) ;
260+
261+
262+ modbusClient . WriteSingleCoil ( int . Parse ( txtStartingAddressInput . Text ) - 1 , coilsToSend ) ;
263+ }
264+ catch ( Exception exc )
265+ {
266+ MessageBox . Show ( exc . Message , "Exception writing values to Server" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
267+ }
268+ }
269+
270+ private void btnWriteSingleRegister_Click ( object sender , EventArgs e )
271+ {
272+ try
273+ {
274+ if ( ! modbusClient . Connected )
275+ {
276+ modbusClient . IPAddress = txtIpAddressInput . Text ;
277+ modbusClient . Port = int . Parse ( txtPortInput . Text ) ;
278+ modbusClient . Connect ( ) ;
279+ }
280+
281+ int registerToSend = 0 ;
282+
283+ registerToSend = int . Parse ( lsbAnswerFromServer . Items [ 0 ] . ToString ( ) ) ;
284+
285+
286+ modbusClient . WriteSingleRegister ( int . Parse ( txtStartingAddressInput . Text ) - 1 , registerToSend ) ;
287+ }
288+ catch ( Exception exc )
289+ {
290+ MessageBox . Show ( exc . Message , "Exception writing values to Server" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
291+ }
292+ }
293+
294+ private void btnWriteMultipleCoils_Click ( object sender , EventArgs e )
295+ {
296+ try
297+ {
298+ if ( ! modbusClient . Connected )
299+ {
300+ modbusClient . IPAddress = txtIpAddressInput . Text ;
301+ modbusClient . Port = int . Parse ( txtPortInput . Text ) ;
302+ modbusClient . Connect ( ) ;
303+ }
304+
305+ bool [ ] coilsToSend = new bool [ lsbAnswerFromServer . Items . Count ] ;
306+
307+ for ( int i = 0 ; i < lsbAnswerFromServer . Items . Count ; i ++ )
308+ {
309+
310+ coilsToSend [ i ] = bool . Parse ( lsbAnswerFromServer . Items [ i ] . ToString ( ) ) ;
311+ }
312+
313+
314+ modbusClient . WriteMultipleCoils ( int . Parse ( txtStartingAddressInput . Text ) - 1 , coilsToSend ) ;
315+ }
316+ catch ( Exception exc )
317+ {
318+ MessageBox . Show ( exc . Message , "Exception writing values to Server" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
319+ }
320+ }
321+
322+ private void btnWriteMultipleRegisters_Click ( object sender , EventArgs e )
323+ {
324+ try
325+ {
326+ if ( ! modbusClient . Connected )
327+ {
328+ modbusClient . IPAddress = txtIpAddressInput . Text ;
329+ modbusClient . Port = int . Parse ( txtPortInput . Text ) ;
330+ modbusClient . Connect ( ) ;
331+ }
332+
333+ int [ ] registersToSend = new int [ lsbAnswerFromServer . Items . Count ] ;
334+
335+ for ( int i = 0 ; i < lsbAnswerFromServer . Items . Count ; i ++ )
336+ {
337+
338+ registersToSend [ i ] = int . Parse ( lsbAnswerFromServer . Items [ i ] . ToString ( ) ) ;
339+ }
340+
341+
342+ modbusClient . WriteMultipleRegisters ( int . Parse ( txtStartingAddressInput . Text ) - 1 , registersToSend ) ;
343+ }
344+ catch ( Exception exc )
345+ {
346+ MessageBox . Show ( exc . Message , "Exception writing values to Server" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
347+ }
348+ }
349+
350+ private void lsbAnswerFromServer_DoubleClick ( object sender , EventArgs e )
351+ {
352+ int rowindex = lsbAnswerFromServer . SelectedIndex ;
353+
354+
355+
356+
357+
358+ }
359+
360+ private void txtCoilValue_DoubleClick ( object sender , EventArgs e )
361+ {
362+ if ( txtCoilValue . Text . Equals ( "FALSE" ) )
363+ txtCoilValue . Text = "TRUE" ;
364+ else
365+ txtCoilValue . Text = "FALSE" ;
366+ }
367+
368+ private void btnClear_Click ( object sender , EventArgs e )
369+ {
370+ lsbAnswerFromServer . Items . Clear ( ) ;
371+ }
372+
373+ private void button2_Click ( object sender , EventArgs e )
374+ {
375+ int rowindex = lsbAnswerFromServer . SelectedIndex ;
376+ if ( rowindex >= 0 )
377+ lsbAnswerFromServer . Items . RemoveAt ( rowindex ) ;
378+ }
379+ }
222380}
0 commit comments