OleDbException was unhandled [Newbie here] - vb.net

i got error in this code please
i need this in school its a group project but im only doing this because they dont know how to code, so i participate to do this program, im a begginer in vb, i hope you understand, Thank you :)
Imports System.Data.OleDb
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form9
Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'FoodDataSet.Table1' table. You can move, or remove it, as needed.
Table1BindingSource.AddNew()
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
TextBox1.Text = ("Burger P 12")
ElseIf RadioButton1.Checked = False Then
TextBox1.Text = ("Choose Your Order!")
End If
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = True Then
TextBox1.Text = ("Cheese Burger = P 15")
ElseIf RadioButton2.Checked = False Then
TextBox1.Text = ("Choose Your Order!")
End If
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
If RadioButton3.Checked = True Then
TextBox1.Text = ("Ham Burger = P 15")
ElseIf RadioButton3.Checked = False Then
TextBox1.Text = ("Choose Your Order!")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
If RadioButton4.Checked = True Then
TextBox2.Text = ("Potato = P 10")
ElseIf RadioButton4.Checked = False Then
TextBox2.Text = ("Choose Your Order!")
End If
End Sub
Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
If RadioButton5.Checked = True Then
TextBox2.Text = ("Piatos = P 12")
ElseIf RadioButton5.Checked = False Then
TextBox2.Text = ("Choose Your Order!")
End If
End Sub
Private Sub RadioButton6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton6.CheckedChanged
If RadioButton6.Checked = True Then
TextBox2.Text = ("Potato Fries = P 15")
ElseIf RadioButton6.Checked = False Then
TextBox2.Text = ("Choose Your Order!")
End If
End Sub
Private Sub RadioButton11_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton11.CheckedChanged
If RadioButton11.Checked = True Then
TextBox3.Text = ("Coke = P 10")
ElseIf RadioButton11.Checked = False Then
TextBox3.Text = ("Choose Your Order!")
End If
End Sub
Private Sub RadioButton8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton8.CheckedChanged
If RadioButton8.Checked = True Then
TextBox3.Text = ("Sprite = P 10")
ElseIf RadioButton8.Checked = False Then
TextBox3.Text = ("Choose Your Order!")
End If
End Sub
Private Sub RadioButton9_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton9.CheckedChanged
If RadioButton9.Checked = True Then
TextBox3.Text = ("Royal = P 10")
ElseIf RadioButton9.Checked = False Then
TextBox3.Text = ("Choose Your Order!")
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim con As OleDbConnection
Table1BindingSource.AddNew()
MessageBox.Show("Your Orders in ongoing,Kindly Prepare your money and wait your order, Thank you!")
Me.Validate()
Me.Table1BindingSource.EndEdit()
con = New OleDbConnection("Provider=Microsoft.Ace.Oledb.12.0;Data Source=C:\Documents and Settings\WinXP\Desktop\LoadTest HP\WindowsApplication1\Food.mdb")
Me.Table1TableAdapter.Update(Me.FoodDataSet.Table1)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Table1BindingSource.RemoveCurrent()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Close()
End Sub
Private Sub RadioButton7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton7.CheckedChanged
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Form10.Show()
Me.Close()
End Sub
End Class

It means you have a possible exception that wasn't handled you should handle the exception which will even show you a error which can further help you figure out what's wrong with your code :)
How do you handle exceptions? wrap code which uses OleDB with try/catch anywhere you use OleDb you should try/catch it.. remove the try/catch later when you know your code works 100%.
like so..
Try
con = New OleDbConnection("Provider=Microsoft.Ace.Oledb.12.0;Data Source=C:\Documents and Settings\WinXP\Desktop\LoadTest HP\WindowsApplication1\Food.mdb")
Catch ex as OleDbException
Msgbox ex.ToString()
End Try
I would wrap where you declare the OleDbConnection as well which is not necessary but it gets all the OleDb stuff in a try/catch
Try
Dim con As OleDbConnection
Table1BindingSource.AddNew()
MessageBox.Show("Your Orders in ongoing,Kindly Prepare your money and wait your order, Thank you!")
Me.Validate()
Me.Table1BindingSource.EndEdit()
con = New OleDbConnection("Provider=Microsoft.Ace.Oledb.12.0;Data Source=C:\Documents and Settings\WinXP\Desktop\LoadTest HP\WindowsApplication1\Food.mdb")
Catch ex as OleDbException
Msgbox ex.ToString()
End Try

Related

Displaying decrementing value of a primary key

Private Sub TblCustomerBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.TblCustomerBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CustomerDataDataSet)
End Sub
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'CustomerDataDataSet.tblCustomer' table. You can move, or remove it, as needed.
Me.TblCustomerTableAdapter.Fill(Me.CustomerDataDataSet.tblCustomer)
End Sub
Private Sub btnRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.Click
Try
TblCustomerBindingSource.EndEdit()
TblCustomerTableAdapter.Update(CustomerDataDataSet.tblCustomer)
MessageBox.Show("Registered successfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Form5.Show()
Me.Hide()
Form5.TblReservationBindingSource.AddNew()
Form5.Label2.Text = Customer_IDTextBox.Text
Form5.Label3.Text = FirstNameTextBox.Text & " " & LastNameTextBox.Text
Catch ex As Exception
MessageBox.Show("Error")
End Try
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
FirstNameTextBox.Text = ""
LastNameTextBox.Text = ""
Contact_NoTextBox.Text = ""
Email_AddressTextBox.Text = ""
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Form1.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Date_Of_RegistrationTextBox.Text = Format(Now, "MM/dd/yyyy, hh:mm:ss tt")
End Sub
When I try to add a new record vb.net automatically display the primary key in a decrementing value but when I click register and check my ms access file it displays the correct value for my primary key.
Here's my program

Get highest value from scales and remove unnecessary words

I created a little program to get weight from scales via COM port. But then I turn on program. It's start to loop (creating long list) SV 0 KG in Label. And if weight increase it's also loop, for e.g. SV 0 KG, SV 50 KG, SV 48 KG, SV 0 KG and etc..
So my question is how to stop loop and always show one value?
And second question can I use Replace("SV", " ") or there is other better method?
Dim myPort As Array
Delegate Sub SetTextCallback(ByVal [text] As String)
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
cmbBaud.Items.Add(9600)
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)
For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0)
cmbBaud.Text = cmbBaud.Items.Item(0)
btnDisconnect.Enabled = False
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbPort.Text
SerialPort1.BaudRate = cmbBaud.Text
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
SerialPort1.Open()
btnConnect.Enabled = False
btnDisconnect.Enabled = True
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close()
btnConnect.Enabled = True
btnDisconnect.Enabled = False
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
SerialPort1.Write(txtTransmit.Text & vbCr)
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text]
End If
End Sub
Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbPort.Text
Else
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaud.Text
Else
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub rtbReceived_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbReceived.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
rtbReceived.Text = " "
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
SerialPort1.Write(txtTransmit.Text & vbCr)
End Sub

String cannot be converted to System.Uri

Codeļ¼š
Imports System
Imports System.IO
Imports System.Text
Public Class Browser
Dim Tab1Url As String = ""
Dim Tab2Url As String = ""
Dim TabNumber As Integer = 1
Dim DefaultHomePageSavedLocation As String = "C:\ToolBoxData\TinyBrowser\Home.txt"
Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
Label2.Text = "[BROWSER] Loading in Progress......"
ProgressBar1.Value = 50
End Sub
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
WebBrowser1.GoBack()
Label2.Text = "[BROWSER] Going to Last History......"
ProgressBar1.Value = 0
End Sub
Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
'Next
WebBrowser1.GoForward()
Label2.Text = "[BROWSER] Going to Next History......"
ProgressBar1.Value = 0
End Sub
Private Sub HomeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HomeToolStripMenuItem.Click
'Home
Dim HomeLink As String = My.Computer.FileSystem.ReadAllText(DefaultHomePageSavedLocation)
WebBrowser1.Navigate(HomeLink)
Label2.Text = "[BROWSER] Going Home......"
ProgressBar1.Value = 0
End Sub
Private Sub ToolStripTextBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripTextBox1.Click
Label2.Text = "[USER] Inserting / Copying URL......"
End Sub
Private Sub GoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoToolStripMenuItem.Click
If TabNumber = 1 Then
Tab1Url = ToolStripTextBox1.Text
WebBrowser1.Navigate(Tab1Url)
Button1.Text = ToolStripTextBox1.Text
ElseIf TabNumber = 2 Then
Tab2Url = ToolStripTextBox1.Text
WebBrowser1.Navigate(Tab2Url)
Button2.Text = ToolStripTextBox1.Text
End If
ProgressBar1.Value = 0
End Sub
Private Sub ReloadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReloadToolStripMenuItem.Click
WebBrowser1.Refresh()
Label2.Text = "[BROWSER] Refreshing......"
ProgressBar1.Value = 0
End Sub
Private Sub TinyBrowserV10ByToolBoxWeeblyComToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TinyBrowserV10ByToolBoxWeeblyComToolStripMenuItem.Click
WebBrowser1.Navigate("http://tool-box.weebly.com/")
Label2.Text = "[BROWSER] Redirecting to Our Website......"
ProgressBar1.Value = 0
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Label2.Text = "[BROWSER] Loading Completed."
ToolStripTextBox1.Text = WebBrowser1.Url.ToString
ProgressBar1.Value = 100
End Sub
Private Sub ToolStripTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ToolStripTextBox1.KeyPress
If e.KeyChar = Chr(13) Then
If TabNumber = 1 Then
Tab1Url = ToolStripTextBox1.Text
WebBrowser1.Navigate(Tab1Url)
Button1.Text = ToolStripTextBox1.Text
ElseIf TabNumber = 2 Then
Tab2Url = ToolStripTextBox1.Text
WebBrowser1.Navigate(Tab2Url)
Button2.Text = ToolStripTextBox1.Text
End If
ProgressBar1.Value = 0
End If
End Sub
Private Sub Browser_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = Convert.ToChar(Keys.Back) Then
WebBrowser1.GoBack()
ProgressBar1.Value = 0
End If
End Sub
Private Sub OptionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptionToolStripMenuItem.Click
Setting.Show()
End Sub
Private Sub Browser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Computer.FileSystem.FileExists(DefaultHomePageSavedLocation) = False Then
My.Computer.FileSystem.CreateDirectory("C:\ToolBoxData\TinyBrowser\")
My.Computer.FileSystem.WriteAllText(DefaultHomePageSavedLocation, "http://hk.yahoo.com", True)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not TabNumber = 1 Then
TabNumber = 1
ChangeTab()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Not TabNumber = 2 Then
TabNumber = 2
ChangeTab()
End If
End Sub
Private Sub ChangeTab()
If TabNumber = 1 Then
WebBrowser1.Url = Tab1Url
ElseIf TabNumber = 2 Then
WebBrowser1.Url = Tab2Url
End If
End Sub
End Class
And the error is:
Value of type "String" cannot be converted to "System.Uri" (Line 121, 123)
What's the problem?
I've found the solution at another website.
Solution:
Line 121:
WebBrowser1.Url = New Uri(Tab1Url)
Line 123:
WebBrowser1.Url = New Uri(Tab2Url)
Thanks for all your help!

vb For loop repeat a certain number of times

So I am making a port scanner and have a min and max port, but can't get the port scanner to stop scanning when it reaches the maximum port?
I have tried doing an Exit For when port reaches portmax.
Here is the code:
Public Class Form1
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim counter As Integer
Button2.Enabled = False
'set counter explained before to 0
counter = 0
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
Dim host As String
Dim counter As Integer
Dim portmin As Integer = TextBox3.Text
Dim portmax As Integer = TextBox2.Text
'Set the host and port and counter
counter = counter + 1 'counter is for the timer
host = TextBox1.Text
For port As Integer = portmin To portmax
' Next part creates a socket to try and connect
' on with the given user information.
Dim hostadd As System.Net.IPAddress = _
System.Net.Dns.GetHostEntry(host).AddressList(0)
Dim EPhost As New System.Net.IPEndPoint(hostadd, port)
Dim s As New System.Net.Sockets.Socket( _
System.Net.Sockets.AddressFamily.InterNetwork, _
System.Net.Sockets.SocketType.Stream, _
System.Net.Sockets.ProtocolType.Tcp)
Try
s.Connect(EPhost)
Catch
End Try
If Not s.Connected Then
ListBox1.Items.Add("Port " + port.ToString + " is not open")
Else
ListBox1.Items.Add("Port " + port.ToString + " is open")
ListBox2.Items.Add(port.ToString)
End If
Label3.Text = "Open Ports: " + ListBox2.Items.Count.ToString
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
'stop button
Timer1.Stop()
Timer1.Enabled = False
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add("Scanning: " + TextBox1.Text)
ListBox1.Items.Add("-------------------")
Button2.Enabled = True
Button1.Enabled = False
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
End Sub
End Class
I would really appreciate any help
thanks,
Try to stop the Timer while in the scanning.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
'Your code
Timer1.Enabled = True
End Sub
If thats the problem, and looks like it, you should consider using a Try/Catch block:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
Timer1.Enabled = False
'Your code
Catch ex As Exception
'Manage the error
Finally
Timer1.Enabled = True
End Try
End Sub
Try this
For port As Integer = portmin To portmax - 1

Very basic VB.Net & Serial IO issue

After searching around I am still having issues with reading data from a serial port in VB.Net/VS2010. I know the Serial Port works, I can write to the port fine but when reading from it, nothing happens. I have only been programming for the last 3 weeks so am still trying to get my head around it all.
The program must run to capture data from a door logger, I will then be outputting the data to a database (not yet implemented - I want to get this part sorted first).
I have tried using several terminal programs as well as another device which outputs data onto the serial line, with nothing displaying in the textbox tbxIn.
Any help would be greatly appreciated.
Code is below:
Imports System.IO.Ports
Imports System.IO.Ports.SerialPort
Public Class Form1
Dim comPort As IO.Ports.SerialPort = Nothing
Dim sComPort As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GetSerialPortNames()
End Sub
Sub GetSerialPortNames()
' Show all available COM ports.
For Each sp As String In My.Computer.Ports.SerialPortNames
lstPorts.Items.Add(sp)
Next
End Sub
Private Sub lstPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPorts.SelectedIndexChanged
sComPort = lstPorts.SelectedItem
Button1.Enabled = True
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Open the serial port using the OpenSerialPort method
Button1.Enabled = False
Button2.Enabled = True
Try
comPort = My.Computer.Ports.OpenSerialPort(sComPort, 9600, IO.Ports.Parity.None, 8, 1)
' comPort.DtrEnable = True
comPort.ReadTimeout = 500
Do
comPort.WriteLine("Go")
Dim sIncomming As String = comPort.ReadLine()
tbxIn.Text = sIncomming & vbCrLf
Loop
Catch ex As TimeoutException
tbxIn.Text &= "Error: Serial Port Read Timeout" & vbCrLf
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
comPort.Close()
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
tbxIn.Text = e.ToString
End Sub
End Class
PRetty sure this will get you what you need. You DON't need the Serial1 component on the designer. Remove that and use this code:
Private comPort As IO.Ports.SerialPort = Nothing
Private sComPort As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
GetSerialPortNames()
End Sub
Sub GetSerialPortNames()
' Show all available COM ports.
For Each sp As String In My.Computer.Ports.SerialPortNames
lstPorts.Items.Add(sp)
Next
End Sub
Private Sub lstPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPorts.SelectedIndexChanged
sComPort = lstPorts.SelectedItem
Button1.Enabled = True
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Open the serial port using the OpenSerialPort method
Button1.Enabled = False
Button2.Enabled = True
Try
comPort = My.Computer.Ports.OpenSerialPort(sComPort, 9600, IO.Ports.Parity.None, 8, 1)
' comPort.DtrEnable = True
'must add handler
AddHandler comPort.DataReceived, AddressOf SerialPort1_DataReceived
comPort.ReadTimeout = 500
Do
comPort.WriteLine("Go")
Dim sIncomming As String = comPort.ReadLine()
tbxIn.Text = sIncomming & vbCrLf
Loop
Catch ex As TimeoutException
tbxIn.Text &= "Error: Serial Port Read Timeout" & vbCrLf
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
comPort.Close()
'remove handler
RemoveHandler comPort.DataReceived, AddressOf SerialPort1_DataReceived
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
tbxIn.Text = e.ToString
End Sub