Using timer for visual basic 2005

Thread Starter

weiix05

Joined Apr 2, 2009
15
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
 

t_n_k

Joined Mar 6, 2009
5,455
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.

 

Thread Starter

weiix05

Joined Apr 2, 2009
15
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.
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.
 

t_n_k

Joined Mar 6, 2009
5,455
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.
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.aspxhttp://msdn.microsoft.com/en-us/library/fk6t46tz.aspx
 

Thread Starter

weiix05

Joined Apr 2, 2009
15
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
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.
 

t_n_k

Joined Mar 6, 2009
5,455
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.
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.
 

t_n_k

Joined Mar 6, 2009
5,455
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.
 

Thread Starter

weiix05

Joined Apr 2, 2009
15
Rich (BB code):

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call Read()
End Sub
Private Sub Read()
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()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call Convert()
End Sub
Private Sub Convert()
Dim hello As Integer
Dim my_str As String = TextBox1.Text.Substring(2, 3)
hello = Val("&H" + my_str)
TextBox2.Text = hello ^ 0.01
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
End Class
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!
 
Top