Using timer for visual basic 2005

Discussion in 'Programmer's Corner' started by weiix05, Apr 19, 2009.

  1. weiix05

    Thread Starter Member

    Apr 2, 2009
    15
    0
    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Enabled =
    True
    Timer1.Interval = 1000
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    TextBox1.Text =
    ""
    TextBox2.Text = ""
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    If SerialPort1.IsOpen Then
    SerialPort1.Close()
    End If
    SerialPort1.PortName = "COM1"
    SerialPort1.BaudRate = 9600
    SerialPort1.Parity = IO.Ports.Parity.None
    SerialPort1.DataBits = 8
    SerialPort1.StopBits = IO.Ports.StopBits.One
    SerialPort1.RtsEnable =
    True
    SerialPort1.Open()
    SerialPort1.WriteLine(
    "U2" + vbCr)
    TextBox1.Text = vbNewLine &
    "....Work in progress...."
    TextBox1.Refresh()
    System.Threading.Thread.Sleep(150)
    TextBox1.Text =
    ""
    TextBox1.Text = SerialPort1.ReadExisting

    SerialPort1.Close()

    Dim hello As Integer
    Dim my_str As String = TextBox1.Text.Substring(2, 3)- error is here.
    hello = Val(
    "&H" + my_str)
    TextBox2.Text = hello ^ 0.01

    End Sub

    End
    Class

    Error:ArgumentOutOfRangeException was unhandled.
    startIndex cannot be larger than the length of the string.
    Parameter name:startIndex
     
  2. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    5,448
    782
    The text in TextBox1 must be shorter in length than the 5 characters you expected to be there. Also remember in VB the lowest array index value is 0 - not 1.

    So if TextBox1 text = "ABCDE"

    The statement....

    Dim my_str AsString = TextBox1.Text.Substring(2, 3)

    would not throw an exception since the valid substring would be "CDE".

    But if TextBox1 ="ABCD" then an error would be thrown since from index 2 onwards there are only two elements present.

     
  3. weiix05

    Thread Starter Member

    Apr 2, 2009
    15
    0
    since you say this can only use for four alphabets den if i nid to use for 7 alphabets as i am converting from hex to dec, wad am i supposed to do?
    thanks so much.
     
  4. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    5,448
    782
    I not suggesting there is a limitation in your case.

    I was simply trying to show, by way of example, what would cause an error and what would not.

    You can have as many characters as practicable but if the substring index range exceeds the available no. of characters then an error occurs.

    Was there a string actually shown in TextBox1 at the time the error occured?

    If you are generating errors such as these you should use an error handler such as the {Try ....Catch ...End Try} structure to avoid throwing the exception error - see

    http://msdn.microsoft.com/en-us/library/fk6t46tz.aspx
     
  5. weiix05

    Thread Starter Member

    Apr 2, 2009
    15
    0
    When the error occured there is no string shown at all,
    the program just hang there.
    I did try using th try..catch..end try.
    But it cant work.
     
  6. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    5,448
    782
    Looks like there is a problem with the serial data transfer when under the timer control.

    If that part worked OK previously I suggest you go back to the code that did work and make sure the serial transfer still functions OK. While it's unlikely there is a problem it's worth checking the functionality.

    The problem may then be isolated to the timer tick event not executing the serial data transfer correctly.

    As a starting point do you see the "....Work in progress...." phrase appearing in the TextBox1 field?

    One thing I would do is have an initializing routine to set up the port when the application opens - rather than re-executing this on every Timer1 tick event. Alternatively, you can easily configure this in the Timer1 properties window without having to explicitly do it in software.

    So all that the Timer1 function call should involve is the serial write / read process.

    The {Try ....Catch ....End Try} structure won't cure any problems other than the application "crashing" due to an unhandled error. It's sole purpose is to detect an error, handle it accordingly then allow the application to move on to the next step.
     
  7. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    5,448
    782
    Also - There is a more fundamental problem with trying to update the user interface from a timer event thread - rather than the user interface thread.

    It may be an option for you to create a public variable for the received serial text and update this variable when the timer event (tick) occurs. When the timer event handler is finished use a method (test loop?) in the user interface thread to execute the TextBox manipulations required.

    You may need to do a web search on cross threading issues with visual basic timers to get a handle on how to resolve this.

    Hopefully someone else can offer some advice as well.
     
  8. weiix05

    Thread Starter Member

    Apr 2, 2009
    15
    0
    Code ( (Unknown Language)):
    1. [SIZE=2][COLOR=#0000ff]
    2. Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Form1
    3. [/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
    4. [/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
    5. [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click
    6. [/SIZE][SIZE=2][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=2] Read()
    7. [/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
    8. [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Read()
    9. [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] SerialPort1.IsOpen [/SIZE][SIZE=2][COLOR=#0000ff]Then
    10. [/COLOR][/SIZE][SIZE=2]SerialPort1.Close()
    11. [/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
    12. [/COLOR][/SIZE][SIZE=2]SerialPort1.PortName = [/SIZE][SIZE=2][COLOR=#800000]"COM1"
    13. [/COLOR][/SIZE][SIZE=2]SerialPort1.BaudRate = 9600
    14. SerialPort1.Parity = IO.Ports.Parity.None
    15. SerialPort1.DataBits = 8
    16. SerialPort1.StopBits = IO.Ports.StopBits.One
    17. SerialPort1.RtsEnable = [/SIZE][SIZE=2][COLOR=#0000ff]True
    18. [/COLOR][/SIZE][SIZE=2]SerialPort1.Open()
    19. SerialPort1.WriteLine([/SIZE][SIZE=2][COLOR=#800000]"U2"[/COLOR][/SIZE][SIZE=2] + vbCr)
    20. TextBox1.Text = vbNewLine & [/SIZE][SIZE=2][COLOR=#800000]"....Work in progress...."
    21. [/COLOR][/SIZE][SIZE=2]TextBox1.Refresh()
    22. System.Threading.Thread.Sleep(150)
    23. TextBox1.Text = [/SIZE][SIZE=2][COLOR=#800000]""
    24. [/COLOR][/SIZE][SIZE=2]TextBox1.Text = SerialPort1.ReadExisting
    25. SerialPort1.Close()
    26. [/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
    27. [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button2_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button2.Click
    28. [/SIZE][SIZE=2][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=2] Convert()
    29. [/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
    30. [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Convert()
    31. [/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] hello [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
    32. [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] my_str [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = TextBox1.Text.Substring(2, 3)
    33. hello = Val([/SIZE][SIZE=2][COLOR=#800000]"&H"[/COLOR][/SIZE][SIZE=2] + my_str)
    34. TextBox2.Text = hello ^ 0.01
    35. [/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
    36. [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button3_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button3.Click
    37. TextBox1.Text = [/SIZE][SIZE=2][COLOR=#800000]""
    38. [/COLOR][/SIZE][SIZE=2]TextBox2.Text = [/SIZE][SIZE=2][COLOR=#800000]""
    39. [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
    40. End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class
    41. [/COLOR][/SIZE]
    my this code is working fine,
    now i just wanna add in with
    a timer so that i can extract the
    values itself without pressing the button.

    thanks tnk,
    really hope that someone would help.
    thanks so much!
     
Loading...