Asked by:
Creating visual graph with vb net

Question
-
Dear all.
I am planning to impliment below project using visual basic.Please let me know How can use Image and process image based on serial data.
I am also plot the graph Time Vs Temprature, Can someone suugest plotting graph with current time and being recorded into Excel Sheet.
Visual studioC# program to display the temprature and humidity
I am using visual basic 2010 for coding. Please share link for implimmenting above code.
AMPS12
All replies
-
I didn't understand what you want. Do you want to create images inside a Windows Forms application or create charts inside an Excel worksheet?
If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".
Se achou este post útil, por favor clique em "Votar como útil". Se por acaso respondeu sua dúvida, lembre de "Marcar como Resposta".
-
You can convert the c# code here:
You can draw with a chart control like here:
The serial device may be like here:
- Proposed as answer by Ed Price - MSFTMicrosoft employee, Owner Saturday, November 5, 2016 9:30 PM
-
I Have written code as below.
Here I am facing 3 issue.
Issue1:
I could not able to connect the Comport one first step it disconnect and again press connect button Then only connect.
Issue 2:
I am getting below error when I change value from Status_text.Text = Status(1) . When its 0 showing right value and when i put 1 get Error as below.
Index was outside the bounds of the array.
Issue 3:
Now I would like to Plot running Curve for Time Vs Humidity and Time vs Temperature. It should show Higher Level temp/ humidity reached in time frame . It should show 3 status
High, Low , Normal Temparure and Humidity.
When Normal Green Light must be Indicated , High means Red Light Must be indicated , Low means Blue Light must be Indicated.
In order to achieve This what need to be done. Is there any code avialble.
Option Explicit On Imports System Imports System.IO.Ports Imports System.Text Public Class DHT11 Dim comPORT As String Dim receivedData As String = "" Dim strArray() As String Dim Data As String Dim Time_date() As String Dim Status() As String Dim Humidity() As String Dim Temp_Var() As String Sub Parse_receivedata(ByVal msg As String) Data = msg Display_Serialout.Text = msg strArray = Split(Data, ",") Time_date = Split(strArray(0), ":") '' TextBox_trial.Text = Time_date(0) '' MsgBox(strArray(0)) Hour_Text.Text = Time_date(1) Min_Text.Text = Time_date(2) Sec_text.Text = Time_date(3) Status = Split(strArray(1), ":") Status_text.Text = Status(0) Humidity = Split(strArray(2), ":") Hum_textbox.Text = Humidity(0) Temp_Var = Split(strArray(3), ":") Temp_text.Text = Temp_Var(0) End Sub Private Sub DHT11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Timer1.Enabled = False comPORT = "" For Each sp As String In My.Computer.Ports.SerialPortNames comPort_ComboBox.Items.Add(sp) Next End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick receivedData = ReceiveSerialData() ''RichTextBox1.Text &= receivedData Parse_receivedata(receivedData) ''Display_Serialout.Text = receivedData receivedData = "" '' data_parse() End Sub Function ReceiveSerialData() As String Dim Incoming As String Try Incoming = SerialPort1.ReadLine() If Incoming Is Nothing Then Return "nothing" & vbCrLf Else Return Incoming End If Catch ex As TimeoutException Return "Error: Serial Port read timed out." End Try End Function Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comPort_ComboBox.SelectedIndexChanged If (comPort_ComboBox.SelectedItem <> "") Then comPORT = comPort_ComboBox.SelectedItem End If End Sub Private Sub BtConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtConnect.Click If (connect_BTN.Text = "Connect") Then If (comPORT <> "") Then SerialPort1.Close() SerialPort1.PortName = comPORT SerialPort1.BaudRate = 9600 SerialPort1.DataBits = 8 SerialPort1.Parity = Parity.None SerialPort1.StopBits = StopBits.One SerialPort1.Handshake = Handshake.None SerialPort1.Encoding = System.Text.Encoding.Default SerialPort1.ReadTimeout = 10000 SerialPort1.Open() connect_BTN.Text = "Dis-connect" Timer1.Enabled = True Timer_LBL.Text = "Timer: ON" Else MsgBox("Select a COM port first") End If Else SerialPort1.Close() connect_BTN.Text = "Connect" Timer1.Enabled = False Timer_LBL.Text = "Timer: OFF" End If End Sub End Class
AMPS12
-
I am getting below error when I change value from Status_text.Text = Status(1) . When its 0 showing right value and when i put 1 get Error as below.
strArray(1) is "DHT11". There is no colon, so there cannot be a Status(1).
For your other error you should use breakpoints and single stepping. Put a breakpoint at the start of the routine, and then single step through the code a line at a time, and check what is happening at each step. You have not indicated how you know it disconnects that first time. It is possible that it connects OK but your first attempt at receivning something fails because it is only part of a message and cannot be parsed. I think you need a lot more checking of that message before you try to parse it.
-
I have made changes code as you said. But i get problem this section .
How could i plot graph Time vs Humidity and time vs Temperature. And update max temprature and humidity recorded on display.
Hour_Text.Text = Time_date(1)
Min_Text.Text = Time_date(2)
Sec_text.Text = Time_date(3)Option Explicit On Imports System Imports System.IO.Ports Imports System.Text Public Class DHT11 Dim comPORT As String Dim receivedData As String = "" Dim strArray() As String Dim Data As String Dim Time_date() As String Dim Status() As String Dim Humidity() As String Dim Temp_Var() As String Dim Temp_Int As Double Dim Hum_Int As Double Sub Parse_receivedata(ByVal msg As String) Data = msg ''Display_Serialout.Text = msg strArray = Split(Data, ",") Time_date = Split(strArray(0), ":") '' TextBox_trial.Text = Time_date(0) '' MsgBox(strArray(0)) Hour_Text.Text = Time_date(1) Min_Text.Text = Time_date(2) Sec_text.Text = Time_date(3) Status = Split(strArray(2), ":") Status_text.Text = Status(1) If Status(1) = "OK" Then Status_text.ReadOnly = True Status_text.BackColor = Color.LightGreen End If If Status(1) = "Checksum error" Then Status_text.ReadOnly = True Status_text.BackColor = Color.PaleVioletRed End If If Status(1) = "Connect error" Then Status_text.ReadOnly = True Status_text.BackColor = Color.MediumVioletRed End If If Status(1) = "Ack Low error" Then Status_text.ReadOnly = True Status_text.BackColor = Color.IndianRed End If If Status(1) = "Ack High error" Then Status_text.ReadOnly = True Status_text.BackColor = Color.DarkRed End If Humidity = Split(strArray(3), ":") Hum_textbox.Text = Humidity(1) Hum_Int = CDbl(Humidity(1)) If Temp_Int >= 25.0 And Temp_Int <= 35.0 Then Hum_textbox.ReadOnly = True Hum_textbox.BackColor = Color.LightGreen End If If Temp_Int < 25.0 Then Hum_textbox.ReadOnly = True Hum_textbox.BackColor = Color.AliceBlue End If If Temp_Int > 35.0 Then Hum_textbox.ReadOnly = True Hum_textbox.BackColor = Color.DarkRed End If Temp_Var = Split(strArray(4), ":") Temp_text.Text = Temp_Var(1) Temp_Int = CDbl(Temp_Var(1)) If Temp_Int >= 15.0 And Temp_Int <= 30 Then Temp_text.ReadOnly = True Temp_text.BackColor = Color.LightGreen End If If Temp_Int < 15.0 Then Temp_text.ReadOnly = True Temp_text.BackColor = Color.AliceBlue End If If Temp_Int > 30.0 Then Temp_text.ReadOnly = True Temp_text.BackColor = Color.DarkRed End If End Sub Private Sub DHT11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Timer1.Enabled = False comPORT = "" For Each sp As String In My.Computer.Ports.SerialPortNames comPort_ComboBox.Items.Add(sp) Next End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick receivedData = ReceiveSerialData() ''RichTextBox1.Text &= receivedData Parse_receivedata(receivedData) ''Display_Serialout.Text = receivedData receivedData = "" '' data_parse() End Sub Function ReceiveSerialData() As String Dim Incoming As String Try Incoming = SerialPort1.ReadLine() If Incoming Is Nothing Then Return "nothing" & vbCrLf Else Return Incoming End If Catch ex As TimeoutException Return "Error: Serial Port read timed out." End Try End Function Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comPort_ComboBox.SelectedIndexChanged If (comPort_ComboBox.SelectedItem <> "") Then comPORT = comPort_ComboBox.SelectedItem End If End Sub Private Sub BtConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtConnect.Click If (connect_BTN.Text = "Connect") Then If (comPORT <> "") Then SerialPort1.Close() SerialPort1.PortName = comPORT SerialPort1.BaudRate = 9600 SerialPort1.DataBits = 8 SerialPort1.Parity = Parity.None SerialPort1.StopBits = StopBits.One SerialPort1.Handshake = Handshake.None SerialPort1.Encoding = System.Text.Encoding.Default SerialPort1.ReadTimeout = 1000 SerialPort1.Open() connect_BTN.Text = "Dis-connect" connect_BTN.BackColor = Color.DarkRed Timer1.Enabled = True Timer_LBL.Text = "Timer: OFF" Else MsgBox("Select a COM port first") End If Else SerialPort1.Close() connect_BTN.Text = "Connect" connect_BTN.BackColor = Color.LightGreen Timer1.Enabled = False Timer_LBL.Text = "Timer: ON" End If End Sub End Class
AMPS12
-
How could i plot graph Time vs Humidity and time vs Temperature. And update max temprature and humidity recorded on display.
Use the reference provided to you already:
To update max temperature and humidity create a new variables to record those values. Each time new data is received compare it to the existing values, and if the new value is higher, update them.
-
How could i populate the temperature values In below code. I have Temp_Int perameter where data being converter ed and moved .
Time is should be in x axis and populated Temrature reading should be X axis.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick receivedData = ReceiveSerialData() ''RichTextBox1.Text &= receivedData Parse_receivedata(receivedData) ''Display_Serialout.Text = receivedData receivedData = "" '' data_parse() Chart1.Series(0).Points.AddXY(Temp_Int, CDbl(Rand.Next(0, 20))) '' Chart1.ChartAreas(0).AxisX.CustomLabels.Add(Temp_Int, Now.ToString("HH:mm:ss fff")) Counter += 1 End Sub Sub Parse_receivedata(ByVal msg As String) Data = msg ''Display_Serialout.Text = msg strArray = Split(Data, ",") Time_date = Split(strArray(0), ":") '' TextBox_trial.Text = Time_date(0) '' MsgBox(strArray(0)) Hour_Text.Text = Time_date(1) Min_Text.Text = Time_date(2) Sec_text.Text = Time_date(3) Status = Split(strArray(2), ":") Status_text.Text = Status(1) If Status(1) = "OK" Then Status_text.ReadOnly = True Status_text.BackColor = Color.LightGreen End If If Status(1) = "Checksum error" Then Status_text.ReadOnly = True Status_text.BackColor = Color.PaleVioletRed End If If Status(1) = "Connect error" Then Status_text.ReadOnly = True Status_text.BackColor = Color.MediumVioletRed End If If Status(1) = "Ack Low error" Then Status_text.ReadOnly = True Status_text.BackColor = Color.IndianRed End If If Status(1) = "Ack High error" Then Status_text.ReadOnly = True Status_text.BackColor = Color.DarkRed End If Humidity = Split(strArray(3), ":") Hum_textbox.Text = Humidity(1) Hum_Int = CDbl(Humidity(1)) If Temp_Int >= 25.0 And Temp_Int <= 35.0 Then Hum_textbox.ReadOnly = True Hum_textbox.BackColor = Color.LightGreen End If If Temp_Int < 25.0 Then Hum_textbox.ReadOnly = True Hum_textbox.BackColor = Color.AliceBlue End If If Temp_Int > 35.0 Then Hum_textbox.ReadOnly = True Hum_textbox.BackColor = Color.DarkRed End If Temp_Var = Split(strArray(4), ":") Temp_text.Text = Temp_Var(1) Temp_Int = CDbl(Temp_Var(1)) If Temp_Int >= 15.0 And Temp_Int <= 30 Then Temp_text.ReadOnly = True Temp_text.BackColor = Color.LightGreen End If If Temp_Int < 15.0 Then Temp_text.ReadOnly = True Temp_text.BackColor = Color.AliceBlue End If If Temp_Int > 30.0 Then Temp_text.ReadOnly = True Temp_text.BackColor = Color.DarkRed End If End Sub
I still get an error Index was outside the bounds of the array ,
My serial output look like this
Time:17:45:35,DHT11:1,Staus:OK,Humidity:24.0,Tempratuere:23.0,END
Time:17:45:36,DHT11:1,Staus:OK,Humidity:22.0,Tempratuere:23.0,END
Time:17:45:37,DHT11:1,Staus:OK,Humidity:22.0,Tempratuere:23.0,END
Time:17:45:38,DHT11:1,Staus:OK,Humidity:22.0,Tempratuere:23.0,END
Time:17:45:39,DHT11:1,Staus:OK,Humidity:22.0,Tempratuere:23.0,END
Time:17:45:40,DHT11:1,Staus:OK,Humidity:32.0,Tempratuere:25.0,END
Time:17:45:41,DHT11:1,Staus:OK,Humidity:22.0,Tempratuere:23.0,END
Time:17:45:42,DHT11:1,Staus:OK,Humidity:32.0,Tempratuere:25.0,END
Time:17:45:43,DHT11:1,Staus:OK,Humidity:24.0,Tempratuere:23.0,END
Time:17:45:44,DHT11:1,Staus:OK,Humidity:32.0,Tempratuere:25.0,END
Time:17:45:45,DHT11:1,Staus:OK,Humidity:32.0,Tempratuere:25.0,END
AMPS12
- Edited by AMPS12 Sunday, November 13, 2016 12:38 PM
-
Here is another simple chart example. You just need to sort out your values to plot. On this line in the example you use you temp values instead of the random number etc.
SpeedData.Add(New Point(CInt(Count), Rand.Next(0, 40)))
Option Strict On 'running serialized chart for continous display Imports System.Windows.Forms.DataVisualization.Charting Public Class ChartSerialAniation Private WithEvents timer1 As New System.Windows.Forms.Timer Private SpeedData As New List(Of Point) Private DirectionData As New List(Of Point) Private Rand As New Random(Now.Millisecond) Private Count As Single Private Sub timer1_Tick(sender As Object, e As EventArgs) Handles timer1.Tick 'increment time count - this could be a date/time Count += 1 If Count > 30 AndAlso SpeedData IsNot Nothing AndAlso SpeedData.Count > 0 Then SpeedData.RemoveAt(0) DirectionData.RemoveAt(0) End If 'add random values to the data SpeedData.Add(New Point(CInt(Count), Rand.Next(0, 40))) DirectionData.Add(New Point(CInt(Count), Rand.Next(0, 10))) DrawChart() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Button1.Text = "Stop" Then timer1.Stop() Button1.Text = "Start" Else DirectionData.Clear() SpeedData.Clear() Button1.Text = "Stop" Count = 0 timer1.Start() End If End Sub Sub DrawChart() If SpeedData Is Nothing Or SpeedData.Count = 0 Then Exit Sub 'setup the chart Chart1.ChartAreas.Clear() Chart1.ChartAreas.Add("Default") With Chart1.ChartAreas("Default") .AxisX.Title = "Increment" .AxisX.Interval = 5 .AxisX.IsMarginVisible = True .AxisX.MajorGrid.LineColor = Color.SkyBlue .AxisY.MajorGrid.LineColor = Color.SkyBlue .AxisY.Title = "Speed/Direction" .AxisY.Interval = 10 End With Chart1.Legends.Clear() Chart1.Legends.Add("Default") Chart1.Legends("Default").Docking = Docking.Top Chart1.Series.Clear() Chart1.Series.Add("Speed") Chart1.Series.Add("Direction") With Chart1.Series(0) .BorderWidth = 3 .Color = Color.Red .ChartType = DataVisualization.Charting.SeriesChartType.Line End With With Chart1.Series(1) .BorderWidth = 3 .Color = Color.Blue .ChartType = DataVisualization.Charting.SeriesChartType.Line End With For i = 0 To SpeedData.Count - 1 Chart1.Series(0).Points.AddXY(SpeedData(i).X, SpeedData(i).Y) Chart1.Series(1).Points.AddXY(DirectionData(i).X, DirectionData(i).Y) Next End Sub End Class