locked
Delayed serialport write RRS feed

  • Question

  • Is it o.k. to use delayed serialport write like this, or is there some other better way to do it and why?

                    serialport.Write("1") 
                    System.Threading.Thread.Sleep(500)
                    serialport.Write("2") 
                    System.Threading.Thread.Sleep(500)
                    serialport.Write("3") 
                    System.Threading.Thread.Sleep(500)
                    serialport.Write("4") 
                    System.Threading.Thread.Sleep(500)
                    serialport.Write("5") 
    Tnx


    Tuesday, May 29, 2012 11:41 AM

Answers

  • Hello,

    that's ok but You block Your main UI.

    You can take Timer:

        Dim msg() As String = {"1", "2", "3"}
        ''' <summary>
        ''' timer
        ''' </summary>
        Private Sub Timer1_Tick(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) Handles Timer1.Tick
            Static count As Integer
            SerialPort1.Write(msg(count))
            count += 1
            If count = 3 Then
                Timer1.Enabled = False
                count = 0
            End If
        End Sub
    Timer interval 500ms and start is with Timer1.enabled = true

    Ich benutze/ I'm using VB2008 & VB2010

    Tuesday, May 29, 2012 12:18 PM