Offline
Newbie
Karma: 0
Posts: 39
|
 |
« on: November 07, 2012, 04:11:39 am » |
hi everyone, need ur help to solve the problem that i face.im working on developing the flood monitoring system, using serial data received, then manipulte the data by arduino atMega328 deumilanove. im trying to do the visual basic command but i got an error stated that there are command that is not initialized.my coding are as follows.
visual basic 2010 coding.
Imports System.Text Imports System.IO.Ports Imports System.Threading Imports System.TimeZone Imports System.Math
Public Class Form1 Dim voltage As Integer Delegate Sub ProcessDataDelegate(ByVal Longitude As String, ByVal Latitude As String) Private Sub SerialPort1_DataReceived(ByVal sender As Object, _ ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Try Dim Longitude As Integer = SerialPort1.ReadLine() Dim Latitude As Integer = SerialPort1.ReadLine() Me.Invoke(New ProcessDataDelegate(AddressOf ProcessData), Longitude, Latitude) Catch ex As Exception MessageBox.Show(ex.ToString, " Unable to get the location") SerialPort1.Close() End Try End Sub Sub ProcessData(ByVal Longitude As String, ByVal Latitude As String) TextBox1.Text = Longitude TextBox2.Text = Latitude End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal _e As EventArgs) Handles Button1.Click If Button1.Text = "Close" Then SerialPort1.Close() Button1.Text = "Open" Else If Not SerialPort1.IsOpen Then SerialPort1.Open()
Button1.Text = " Close"
End If End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal _e As FormClosingEventArgs ) Handles Me.FormClosing SerialPort1.Close()
End Sub Private Sub WebBrowser2_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted WebBrowser2.Navigate("maps.google.com/maps?q=") End Sub
Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) If (voltage <= 0.5) Then MsgBox("Checked") Else MsgBox("Unchecked")
End If End Sub
Private Sub CheckedListBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged If CheckedListBox1.GetItemCheckState(voltage <= 0.5) Then CheckedListBox1.Text = True CheckedListBox1.CheckOnClick = True Else If Not CheckedListBox1.CheckOnClick Then CheckedListBox1.Text = False
End If
End If
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged If (voltage <= 0.5) Then CheckedListBox1.Text = True MsgBox(" safe") TextBox3.Show() End If If (voltage >= 0.5 And voltage <= 0.6) Then CheckedListBox2.Text = True MsgBox(" WARNING") TextBox3.Show() End If If (voltage <= 0.6 And voltage <= 0.7) Then CheckedListBox3.Text = True MsgBox("DANGER") TextBox3.Show() End If
End Sub
Private Sub SplitContainer2_Panel2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer2.Panel2.Paint
End Sub
Private Sub SplitContainer1_Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer1.Panel1.Paint End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub CheckedListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox2.SelectedIndexChanged If CheckedListBox2.GetItemCheckState(voltage >= 0.5 And voltage <= 0.6) Then CheckedListBox2.Text = True CheckedListBox2.CheckOnClick = True Else If Not CheckedListBox2.CheckOnClick Then CheckedListBox1.Text = False End If End If
End Sub
Private Sub CheckedListBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox3.SelectedIndexChanged If CheckedListBox3.GetItemCheckState(voltage >= 0.6 And voltage <= 0.7) Then CheckedListBox2.Text = True CheckedListBox2.CheckOnClick = True Else If Not CheckedListBox2.CheckOnClick Then CheckedListBox1.Text = False End If End If End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Timer1.Enabled = True Timer1.Interval = 1 End Sub
Private Sub PictureBox1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.TextChanged
Label6.Text = DateString
End Sub
Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.TextChanged
Label7.Text = TimeOfDay End Sub End Class
hope anyone can help me solve this problem.thanks.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #1 on: November 07, 2012, 07:48:01 am » |
but i got an error stated that there are command that is not initialized. I doubt that the syntax of the message was as mangled as that. Post the EXACT error message. Good thing you found this problem early before you wrote a ton of code.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #2 on: November 07, 2012, 12:26:44 pm » |
i cannot paste the print screen of the error..
after compiling the coding...another window appeared stated that " an invalid operation excerption was unhandled" The error is: Failed to initialize because CategoryName is missing
tq.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #3 on: November 07, 2012, 04:52:52 pm » |
Where is SerialPort1 defined/initialized? How is it defined and initialized?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #4 on: November 08, 2012, 03:52:27 am » |
should it be initialized?how? because previously i have complied the code, but no error happens. how ever , the error appeared as i put the timer function to the code.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #5 on: November 11, 2012, 10:09:25 am » |
paul...how to intialized the serial port1?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #6 on: November 11, 2012, 10:27:19 am » |
how to intialized the serial port1? Initialization is the second step. You have not explained the first step - declaration.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #7 on: November 11, 2012, 10:46:23 am » |
Private Sub Button1_Click(ByVal sender As Object, ByVal _e As EventArgs) Handles Button1.Click If Button1.Text = "Close" Then ' will fail it button's text is " Close" (notice the extra space) SerialPort1.Close() Button1.Text = "Open" Else If Not SerialPort1.IsOpen Then SerialPort1.Open() ' where is endif ?
Button1.Text = " Close" ' one extra space!
End If End Sub
Where do you set the port options (baud rate, etc. ?).
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #8 on: November 15, 2012, 10:17:22 am » |
PaulS, here is the declaration for the serialport1.
Public Class Form1 Dim voltage As Integer Dim WithEvents longitude As SerialPort = New _ System.IO.Ports.SerialPort("COM23", 9600, Parity.None, 8, StopBits.One)
however, i still get the same error " system.InvalidOperationException ".
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #9 on: November 15, 2012, 10:20:23 am » |
tuxduino...
actually i got the coding from other pages.
Private Sub Button1_Click(ByVal sender As Object, ByVal _e As EventArgs) Handles Button1.Click If Button1.Text = "Close" Then ' will fail it button's text is " Close" (notice the extra space) SerialPort1.Close() Button1.Text = "Open" Else If Not SerialPort1.IsOpen Then SerialPort1.Open() ' where is endif ?
Button1.Text = " Close" ' one extra space!( meaning of the code)?
End If// here is the end if . End Sub
hope u can help me in understanding me the button.text= "close" code..i can get ur comment on that code.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #10 on: November 15, 2012, 11:51:45 am » |
(Please use code tags (# button in the editor toolbar above) when posting code.) Please have a look at this (corrected) version of the code: Private Sub Button1_Click(ByVal sender As Object, ByVal _e As EventArgs) Handles Button1.Click If Button1.Text = "Close" Then SerialPort1.Close() Button1.Text = "Open" Else If Not SerialPort1.IsOpen Then SerialPort1.Open() Button1.Text = " Close" End If End if End Sub
Let's try to explain it in pseudocode: if button1 label is "Close" then (A) close the serial port change button1 label to "Open" else if serial port is not already open open it change button1 label to "Close" (B) endif endif (B) If the string assigned here is not _exactly_ equal to that tested in (A) the code won't work. In the code fragment posted earlier, in (B) you have " Close", with an extra space before the first letter. That's not going to be equal to "Close" in (A), so the IF will always fail. I hope it's clearer now.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #11 on: November 16, 2012, 05:56:28 am » |
tuxdino,i have try compile will your code.however..the compiling error still exist.the error stated that "A first chance exception of type 'System.InvalidOperationException' occurred in System.dll"
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #12 on: November 16, 2012, 06:01:03 am » |
My advice was about the errors in the piece of code you posted. Presumably that exception comes from another part of the code. Without seeing the full code, it's hard to tell.
Also, does it happen just by clicking that Button1 or when some serial data comes in ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #13 on: November 17, 2012, 12:35:06 am » |
tuxdino..PaulS..here is the full code Imports System.Text Imports System.IO.Ports Imports System.Threading Imports System.TimeZone Imports System.Math
Public Class Form1 Dim voltage As Integer Dim WithEvents longitude As SerialPort = New _ System.IO.Ports.SerialPort("COM23", 9600, Parity.None, 8, StopBits.One)
Delegate Sub ProcessDataDelegate(ByVal Longitude As String, ByVal Latitude As String) Private Sub SerialPort1_DataReceived(ByVal sender As Object, _ ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Try Dim Longitude As Integer = SerialPort1.ReadLine() Dim Latitude As Integer = SerialPort1.ReadLine() Me.Invoke(New ProcessDataDelegate(AddressOf ProcessData), Longitude, Latitude) Catch ex As Exception MessageBox.Show(ex.ToString, " Unable to get the location") SerialPort1.Close() End Try End Sub Sub ProcessData(ByVal Longitude As String, ByVal Latitude As String) TextBox1.Text = Longitude TextBox2.Text = Latitude End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal _e As EventArgs) Handles Button1.Click If Button1.Text = "Close" Then SerialPort1.Close() Button1.Text = "Open" Else If Not SerialPort1.IsOpen Then SerialPort1.Open() Button1.Text = " Close"
End If End If
End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal _e As FormClosingEventArgs ) Handles Me.FormClosing SerialPort1.Close()
End Sub Private Sub WebBrowser2_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted WebBrowser2.Navigate("maps.google.com/maps?q=") End Sub
Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) If (voltage <= 0.5) Then MsgBox("Checked") Else MsgBox("Unchecked")
End If End Sub
Private Sub CheckedListBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged If CheckedListBox1.GetItemCheckState(voltage <= 0.5) Then CheckedListBox1.Text = True CheckedListBox1.CheckOnClick = True Else If Not CheckedListBox1.CheckOnClick Then CheckedListBox1.Text = False
End If
End If
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged If (voltage <= 0.5) Then CheckedListBox1.Text = True MsgBox(" safe") TextBox3.Show() End If If (voltage >= 0.5 And voltage <= 0.6) Then CheckedListBox2.Text = True MsgBox(" WARNING") TextBox3.Show() End If If (voltage <= 0.6 And voltage <= 0.7) Then CheckedListBox3.Text = True MsgBox("DANGER") TextBox3.Show() End If
End Sub
Private Sub SplitContainer2_Panel2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer2.Panel2.Paint
End Sub
Private Sub SplitContainer1_Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SplitContainer1.Panel1.Paint End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub CheckedListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox2.SelectedIndexChanged If CheckedListBox2.GetItemCheckState(voltage >= 0.5 And voltage <= 0.6) Then CheckedListBox2.Text = True CheckedListBox2.CheckOnClick = True Else If Not CheckedListBox2.CheckOnClick Then CheckedListBox1.Text = False End If End If
End Sub
Private Sub CheckedListBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox3.SelectedIndexChanged If CheckedListBox3.GetItemCheckState(voltage >= 0.6 And voltage <= 0.7) Then CheckedListBox2.Text = True CheckedListBox2.CheckOnClick = True Else If Not CheckedListBox2.CheckOnClick Then CheckedListBox1.Text = False End If End If End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Timer1.Enabled = True Timer1.Interval = 1 End Sub
Private Sub PictureBox1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
PictureBox1.GetContainerControl() If (voltage = 0.5) Then
End If
End Sub
Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.TextChanged
Label6.Text = DateString
End Sub
Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.TextChanged
Label7.Text = TimeOfDay End Sub
End Class
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
|
 |
« Reply #14 on: November 17, 2012, 04:51:17 am » |
Does that "Unable to get the location" error message box show up ?
|
|
|
|
|
Logged
|
|
|
|
|
|