Explain code

toto11

New member
Joined
Jun 9, 2007
Messages
3
Programming Experience
Beginner
Hello,
Could you explain this code .

2003532015642029303_rs.jpg

VB.NET:
Public Class Form1
    Dim curChr As UShort
    Dim MotorON As Boolean
    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False  ' allow worker thread to update main text field
        MotorON = False             ' Initialize
        Outputer.RunWorkerAsync()   ' Start up the serial output thread    

        '==================================================
        Dim pwm As UShort
        Dim val As UShort
        
        pwm = 126
        Stats.Text = "PWM Val = " + Str(pwm) + " (0x" + Hex(pwm) + ")"
        curChr = pwm    ' Modify the global values that outputer will pick up.
        MotorON = True
        '===================================================

    End Sub

    ' HScrollBar1_Scroll
    ' One of two "meaty" functions in this little program.  This is called
    ' whenever the value of the scrollbar is changed.
    ' We read the current setting of the scroll bar, compute a PWM value
    ' with some number of consecutive zero bits, and turn the motor on or
    ' off if the value has changed to or from zero.
    '
    Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
       
        Dim pwm As UShort
        Dim val As UShort
        val = HScrollBar1.Value  'Get current value of the scroll bar
        'TextBox1.Text = Str(val)
        ScrollVal.Text = "ScrollVal = " + Str(val)  'report the value to text field
        If val = 0 Then ' Has the motor been turned OFF?
            Stats.Text = "Motor OFF"
            MotorON = False
        Else ' The motor is ON.  Compute a character value with N consecutive 0 bits
            ' In theory, we could do up to 8 bits by changing the 127 below to 255,
            ' but something goes wrong somewhere (sign issues?) and we don't get
            ' distinct speeds.
            pwm = (255 << (val - 1)) And 127
            Stats.Text = "PWM Val = " + Str(pwm) + " (0x" + Hex(pwm) + ")"
            curChr = pwm    ' Modify the global values that outputer will pick up.
            MotorON = True
        End If
    End Sub

    Private Sub Stats_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stats.Click

    End Sub

    ' Outputer_DoWork is the other meaty function.  This is a separate thread, all it
    ' does is continuously output the current character value to the serial port
    ' (assuming the motor is ON.)
    '
    Private Sub Outputer_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Outputer.DoWork

        SerialPort1.Open() ' open the serial port at the beginning of time.

        While (True)
            If MotorON Then
                If Len(OutString.Text) > 25 Then
                    ' make sure our informational display doesn't get too big
                    OutString.Text = ""
                End If
                OutString.Text = OutString.Text + Chr(curChr) ' update out informational display
                SerialPort1.Write(StrDup(100, Chr(curChr))) ' output to the serial port
            End If
        End While
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim pwm As UShort
        Dim val As UShort

        ' val = HScrollBar1.Value  'Get current value of the scroll bar
        ' ScrollVal.Text = "ScrollVal = " + Str(val)  'report the value to text field
        val = 2
        If val = 0 Then ' Has the motor been turned OFF?
            Stats.Text = "Motor OFF"
            MotorON = False
        Else ' The motor is ON.  Compute a character value with N consecutive 0 bits
            ' In theory, we could do up to 8 bits by changing the 127 below to 255,
            ' but something goes wrong somewhere (sign issues?) and we don't get
            ' distinct speeds.
            pwm = 126
            If Button1.Text = "Stop" Then pwm = 126 : Button1.Text = "Start" : GoTo 500
            If Button1.Text = "Start" Then pwm = 0 : Button1.Text = "Stop"
500:        REM
            Stats.Text = "PWM Val = " + Str(pwm) + " (0x" + Hex(pwm) + ")"
            curChr = pwm    ' Modify the global values that outputer will pick up.
            MotorON = True
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim pwm As UShort
        Dim val As UShort

        ' val = HScrollBar1.Value  'Get current value of the scroll bar
        ' ScrollVal.Text = "ScrollVal = " + Str(val)  'report the value to text field
        val = 2
        If val = 0 Then ' Has the motor been turned OFF?
            Stats.Text = "Motor OFF"
            MotorON = False
        Else ' The motor is ON.  Compute a character value with N consecutive 0 bits
            ' In theory, we could do up to 8 bits by changing the 127 below to 255,
            ' but something goes wrong somewhere (sign issues?) and we don't get
            ' distinct speeds.
            '  pwm = 126
            If Button3.Text = "Clockwise" Then pwm = 0 : Button3.Text = "Unti-Clockwise" : GoTo 500
            If Button3.Text = "Unti-Clockwise" Then
                Stats.Text = "Motor OFF" : MotorON = False : Button3.Text = "Clockwise"
                Exit Sub
            End If
500:        REM
            Stats.Text = "PWM Val = " + Str(pwm) + " (0x" + Hex(pwm) + ")"
            curChr = pwm    ' Modify the global values that outputer will pick up.
            MotorON = True
        End If
    End Sub
End Class
 
Back
Top