How to Write Ladder Logic Using Visual Basic
- 1). Open your Visual Basic file with an editor such as Microsoft Visual Studio.
- 2). Simulate the ladder logic function "s = x AND (y or z)" by adding the following code in your function:
Dim contact_x As Boolean
Dim contact_y As Boolean
Dim contact_z As Boolean
Dim coil_s As Boolean
contact_x = True
contact_y = False
contact_z = True
coil_s= (contact_x AndAlso (contact_y Or contact_z))
The value of the coil "coil_s" will be "True" and its rung will be open. - 3). Simulate the latch configuration "run = (start OR run) AND (NOT STOP)" by adding the following code in your function:
Dim contact_start As Boolean
Dim contact_stop As Boolean
Dim contact_run As Boolean
Dim coil_run As Boolean
Dim coil_m As Boolean
contact_start = True
contact_run = False
contact_stop = False
coil_run = False
coil_run = (b_start Or b_run) AndAlso (Not b_stop)
coil_m = coil_run
If coil_run Then contact_run = True
The code simulates a system with a "Start" and "Stop" button. If the system is already running then it keeps going. If the system is stopped, pressing the "Start" button will start it up. The "Stop" button will stop the system. - 4). Simulate counter functionality by adding the following code in your function:
Dim counter_a As Integer
Dim counter_b As Integer
Dim counter_c As Integer
Dim contact_r As Boolean
Dim contact_i As Boolean
counter_a = 0
counter_b = 0
counter_c = 0
contact_r = True
contact_i = True
If contact_r Then
counter_a += 1
counter_c += 1
End If
If contact_i Then
counter_b += 1
counter_c += 1
End If
Whenever one of the contacts is energized the system increments its corresponding counter. The "c" counter stores the sum of the "a" and "b" counters. This is useful when you need to know how many times an event has happened. - 5). Save the Visual Basic file, compile and run your program to simulate the ladder logic functions.
Source...