N
ns20
Guest
Hi everyone. I'm a beginner in this visual basic language. Currently i'm having a problem with my project which involved 3 components, a dispenser instrument, VB and Arduino. I need to take the data from the instrument using serial communication and transfer them to vb and from vb to be displayed in a LCD via Arduino. Therefore, i have made two forms for each serial port in VB and successfully transferred the data. The problem occurred when I connect to Arduino and transferred data to LCD, my form will freeze and I cannot click the disconnect button for both my forms. The data will then appeared in the LCD after I stop execute. I hope anyone can help to give me some idea to solve this problem. Thank you!! I think there is some problem with my looping but I dont know where =(
Attached are both coding for form1 & 2.
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class Form1
Dim myPort As Array
Delegate Sub SetTextCallBack(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames
ComboBoxPort.Items.AddRange(myPort)
End Sub
Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
Try
SerialPort1.PortName = ComboBoxPort.Text
SerialPort1.BaudRate = ComboBoxBaud.Text
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
SerialPort1.Encoding = System.Text.Encoding.Default
SerialPort1.Open()
AddHandler SerialPort1.DataReceived, AddressOf SerialPort1_DataReceived
If SerialPort1.IsOpen Then
btnConnect.Enabled = False
btnDisconnect.Enabled = True
End If
Catch
SerialPort1.Close()
End Try
End Sub
Private Sub BtnDisonnect_Click(sender As Object, e As EventArgs) Handles btnDisconnect.Click
Try
SerialPort1.Close()
btnConnect.Enabled = True
btnDisconnect.Enabled = False
Exit Sub
Catch
MessageBox.Show("Error!")
End Try
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.RichTextBoxReceive.InvokeRequired Then
Dim x As New SetTextCallBack(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBoxReceive.Text = [text]
Dim Data As String
Data = RichTextBoxReceive.Text
Data = text.Replace("Pressure", vbCrLf + "Pressure").Replace("Vacuum", vbCrLf + "Vacuum").Replace("Dispensing Count", vbCrLf + "Dispensing Count")
Dim SplitLine() As String
SplitLine = Split(Data, vbLf)
For Index As Integer = 0 To Data.Split(vbCrLf).Length - 2
If SplitLine(Index).Contains("Pressure") Then
Me.RichTextBoxData1.Text = SplitLine(Index)
End If
If SplitLine(Index).Contains("Vacuum") Then
Me.RichTextBoxData2.Text = SplitLine(Index)
End If
If SplitLine(Index).Contains("Dispensing Count") Then
Me.RichTextBoxData3.Text = SplitLine(Index)
End If
Next
End If
End Sub
Private Sub ComboBoxPort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxPort.SelectedIndexChanged
End Sub
Private Sub RichTextBoxData1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData1.TextChanged
RichTextBoxData1.SelectionStart = RichTextBoxData1.Text.Length
End Sub
Private Sub RichTextBoxData2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData2.TextChanged
RichTextBoxData2.SelectionStart = RichTextBoxData2.Text.Length
End Sub
Private Sub RichTextBoxData3_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData3.TextChanged
RichTextBoxData3.SelectionStart = RichTextBoxData3.Text.Length
End Sub
Private Sub RichTextBoxReceive_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxReceive.TextChanged
RichTextBoxReceive.SelectionStart = RichTextBoxReceive.Text.Length
End Sub
Private Sub BtnForm2_Click(sender As Object, e As EventArgs) Handles BtnForm2.Click
Dim myForm As New Form2
If myForm Is Nothing Then
myForm = New Form2
End If
myForm.Show()
End Sub
End Class
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Text.RegularExpressions
Public Class Form2
Inherits System.Windows.Forms.Form
Dim myPort As Array
Delegate Sub SetTextCallBack(ByVal [text] As String)
Dim stopclick As Boolean = True
Public myCaller As Form1
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames
ComboBoxPort.Items.AddRange(myPort)
End Sub
Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles BtnConnect.Click
Try
SerialPort2.PortName = ComboBoxPort.Text
SerialPort2.BaudRate = ComboBoxBaud.Text
SerialPort2.Parity = IO.Ports.Parity.None
SerialPort2.StopBits = IO.Ports.StopBits.One
SerialPort2.DataBits = 8
SerialPort2.Encoding = System.Text.Encoding.Default
SerialPort2.Open()
AddHandler SerialPort2.DataReceived, AddressOf SerialPort2_DataReceived
If SerialPort2.IsOpen Then
BtnConnect.Enabled = False
BtnDisconnect.Enabled = True
Timer1.Start()
TestContinousLoop()
End If
Catch
SerialPort2.Close()
End Try
End Sub
Private Sub BtnDisconnect_Click(sender As Object, e As EventArgs) Handles BtnDisconnect.Click
Try
SerialPort2.Close()
BtnConnect.Enabled = True
BtnDisconnect.Enabled = False
Timer1.Stop()
stopclick = True
Catch
MessageBox.Show("Error!")
End Try
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.RichTextBoxReceive.InvokeRequired Then
Dim x As New SetTextCallBack(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBoxReceive.Text &= [text]
End If
End Sub
Private Sub RichTextBoxReceive_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxReceive.TextChanged
RichTextBoxReceive.SelectionStart = RichTextBoxReceive.Text.Length
End Sub
Private Sub RichTextBoxData1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData1.TextChanged
RichTextBoxData1.SelectionStart = RichTextBoxData1.Text.Length
End Sub
Private Sub RichTextBoxData2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData2.TextChanged
RichTextBoxData2.SelectionStart = RichTextBoxData2.Text.Length
End Sub
Private Sub RichTextBoxData3_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData3.TextChanged
RichTextBoxData3.SelectionStart = RichTextBoxData3.Text.Length
End Sub
Private Sub SerialPort2_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived
ReceivedText(SerialPort2.ReadExisting())
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
RichTextBoxData1.Text = Form1.RichTextBoxData1.Text
RichTextBoxData2.Text = Form1.RichTextBoxData2.Text
RichTextBoxData3.Text = Form1.RichTextBoxData3.Text
End Sub
Private Sub TestContinousLoop()
Do
Timer1.Interval = 5000
Timer1.Start()
Dim newData1 As String
newData1 = Regex.Replace(RichTextBoxData1.Text, "[^\d]", String.Empty)
Dim p As Integer
p = Integer.Parse(newData1)
If p < 50 Then
SerialPort2.WriteLine("Pressure:" & p & " LOW")
Else
SerialPort2.WriteLine("Pressure:" & p & " NORMAL")
End If
Dim newData2 As String
newData2 = Regex.Replace(RichTextBoxData2.Text, "[^\d]", String.Empty)
Dim v As Integer
'v = Integer.Parse(newData2)
If v < 100 Then
SerialPort2.WriteLine("Vacuum :" & v & " LOW")
Else
SerialPort2.WriteLine("Vacuum :" & v & " NORMAL")
SerialPort2.WriteLine("")
End If
If stopclick = False Then
Exit Do
End If
Loop
End Sub
End Class
Continue reading...
Attached are both coding for form1 & 2.
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class Form1
Dim myPort As Array
Delegate Sub SetTextCallBack(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames
ComboBoxPort.Items.AddRange(myPort)
End Sub
Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
Try
SerialPort1.PortName = ComboBoxPort.Text
SerialPort1.BaudRate = ComboBoxBaud.Text
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
SerialPort1.Encoding = System.Text.Encoding.Default
SerialPort1.Open()
AddHandler SerialPort1.DataReceived, AddressOf SerialPort1_DataReceived
If SerialPort1.IsOpen Then
btnConnect.Enabled = False
btnDisconnect.Enabled = True
End If
Catch
SerialPort1.Close()
End Try
End Sub
Private Sub BtnDisonnect_Click(sender As Object, e As EventArgs) Handles btnDisconnect.Click
Try
SerialPort1.Close()
btnConnect.Enabled = True
btnDisconnect.Enabled = False
Exit Sub
Catch
MessageBox.Show("Error!")
End Try
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.RichTextBoxReceive.InvokeRequired Then
Dim x As New SetTextCallBack(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBoxReceive.Text = [text]
Dim Data As String
Data = RichTextBoxReceive.Text
Data = text.Replace("Pressure", vbCrLf + "Pressure").Replace("Vacuum", vbCrLf + "Vacuum").Replace("Dispensing Count", vbCrLf + "Dispensing Count")
Dim SplitLine() As String
SplitLine = Split(Data, vbLf)
For Index As Integer = 0 To Data.Split(vbCrLf).Length - 2
If SplitLine(Index).Contains("Pressure") Then
Me.RichTextBoxData1.Text = SplitLine(Index)
End If
If SplitLine(Index).Contains("Vacuum") Then
Me.RichTextBoxData2.Text = SplitLine(Index)
End If
If SplitLine(Index).Contains("Dispensing Count") Then
Me.RichTextBoxData3.Text = SplitLine(Index)
End If
Next
End If
End Sub
Private Sub ComboBoxPort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxPort.SelectedIndexChanged
End Sub
Private Sub RichTextBoxData1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData1.TextChanged
RichTextBoxData1.SelectionStart = RichTextBoxData1.Text.Length
End Sub
Private Sub RichTextBoxData2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData2.TextChanged
RichTextBoxData2.SelectionStart = RichTextBoxData2.Text.Length
End Sub
Private Sub RichTextBoxData3_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData3.TextChanged
RichTextBoxData3.SelectionStart = RichTextBoxData3.Text.Length
End Sub
Private Sub RichTextBoxReceive_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxReceive.TextChanged
RichTextBoxReceive.SelectionStart = RichTextBoxReceive.Text.Length
End Sub
Private Sub BtnForm2_Click(sender As Object, e As EventArgs) Handles BtnForm2.Click
Dim myForm As New Form2
If myForm Is Nothing Then
myForm = New Form2
End If
myForm.Show()
End Sub
End Class
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Text.RegularExpressions
Public Class Form2
Inherits System.Windows.Forms.Form
Dim myPort As Array
Delegate Sub SetTextCallBack(ByVal [text] As String)
Dim stopclick As Boolean = True
Public myCaller As Form1
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames
ComboBoxPort.Items.AddRange(myPort)
End Sub
Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles BtnConnect.Click
Try
SerialPort2.PortName = ComboBoxPort.Text
SerialPort2.BaudRate = ComboBoxBaud.Text
SerialPort2.Parity = IO.Ports.Parity.None
SerialPort2.StopBits = IO.Ports.StopBits.One
SerialPort2.DataBits = 8
SerialPort2.Encoding = System.Text.Encoding.Default
SerialPort2.Open()
AddHandler SerialPort2.DataReceived, AddressOf SerialPort2_DataReceived
If SerialPort2.IsOpen Then
BtnConnect.Enabled = False
BtnDisconnect.Enabled = True
Timer1.Start()
TestContinousLoop()
End If
Catch
SerialPort2.Close()
End Try
End Sub
Private Sub BtnDisconnect_Click(sender As Object, e As EventArgs) Handles BtnDisconnect.Click
Try
SerialPort2.Close()
BtnConnect.Enabled = True
BtnDisconnect.Enabled = False
Timer1.Stop()
stopclick = True
Catch
MessageBox.Show("Error!")
End Try
End Sub
Private Sub ReceivedText(ByVal [text] As String)
If Me.RichTextBoxReceive.InvokeRequired Then
Dim x As New SetTextCallBack(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBoxReceive.Text &= [text]
End If
End Sub
Private Sub RichTextBoxReceive_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxReceive.TextChanged
RichTextBoxReceive.SelectionStart = RichTextBoxReceive.Text.Length
End Sub
Private Sub RichTextBoxData1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData1.TextChanged
RichTextBoxData1.SelectionStart = RichTextBoxData1.Text.Length
End Sub
Private Sub RichTextBoxData2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData2.TextChanged
RichTextBoxData2.SelectionStart = RichTextBoxData2.Text.Length
End Sub
Private Sub RichTextBoxData3_TextChanged(sender As Object, e As EventArgs) Handles RichTextBoxData3.TextChanged
RichTextBoxData3.SelectionStart = RichTextBoxData3.Text.Length
End Sub
Private Sub SerialPort2_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived
ReceivedText(SerialPort2.ReadExisting())
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
RichTextBoxData1.Text = Form1.RichTextBoxData1.Text
RichTextBoxData2.Text = Form1.RichTextBoxData2.Text
RichTextBoxData3.Text = Form1.RichTextBoxData3.Text
End Sub
Private Sub TestContinousLoop()
Do
Timer1.Interval = 5000
Timer1.Start()
Dim newData1 As String
newData1 = Regex.Replace(RichTextBoxData1.Text, "[^\d]", String.Empty)
Dim p As Integer
p = Integer.Parse(newData1)
If p < 50 Then
SerialPort2.WriteLine("Pressure:" & p & " LOW")
Else
SerialPort2.WriteLine("Pressure:" & p & " NORMAL")
End If
Dim newData2 As String
newData2 = Regex.Replace(RichTextBoxData2.Text, "[^\d]", String.Empty)
Dim v As Integer
'v = Integer.Parse(newData2)
If v < 100 Then
SerialPort2.WriteLine("Vacuum :" & v & " LOW")
Else
SerialPort2.WriteLine("Vacuum :" & v & " NORMAL")
SerialPort2.WriteLine("")
End If
If stopclick = False Then
Exit Do
End If
Loop
End Sub
End Class
Continue reading...