Advertisement
Guest User

aabs

a guest
Nov 10th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.56 KB | None | 0 0
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.ComponentModel
  4. Imports System.Data
  5. Imports System.Drawing
  6. Imports System.Linq
  7. Imports System.Text
  8. Imports System.Threading.Tasks
  9. Imports System.Windows.Forms
  10. Imports System.IO.Ports
  11. Imports System.Threading
  12.  
  13. Namespace arduino
  14.     Public Partial Class Form1
  15.         Inherits Form
  16.  
  17.         Private X As Thread
  18.  
  19.         Public Sub New()
  20.             InitializeComponent()
  21.             ComboPorta.DataSource = SerialPort.GetPortNames()
  22.             X = New Thread(New ThreadStart(AddressOf Serial))
  23.         End Sub
  24.  
  25.         Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
  26.             If SP.IsOpen Then
  27.                 SP.Close()
  28.                 X.Abort()
  29.                 timerx.Enabled = False
  30.             Else
  31.                 SP.PortName = ComboPorta.Text
  32.                 SP.BaudRate = Convert.ToInt32(ComboBaud.Text)
  33.                 SP.Open()
  34.                 timerx.Enabled = True
  35.             End If
  36.         End Sub
  37.  
  38.         Private Sub Serial()
  39.             While True
  40.                 SP.Write("a")
  41.                 textBox1.Text += SP.ReadLine()
  42.                 textBox1.Text += vbCrLf
  43.             End While
  44.         End Sub
  45.  
  46.         Private Sub timerx_Tick(ByVal sender As Object, ByVal e As EventArgs)
  47.             SP.Write("a")
  48.         End Sub
  49.  
  50.         Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
  51.             SP.Close()
  52.             X.Abort()
  53.             timerx.Enabled = False
  54.         End Sub
  55.  
  56.         Private Sub SP_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
  57.             Me.Invoke(New EventHandler(AddressOf transfere))
  58.         End Sub
  59.  
  60.         Private Sub transfere(ByVal sender As Object, ByVal e As EventArgs)
  61.             Dim dado As String = SP.ReadLine()
  62.             Dim dados As String() = dado.Split(","c)
  63.             textBox1.Text += dados(0)
  64.             textBox1.Text += "%" & vbCrLf
  65.             textBox1.[Select](textBox1.Text.Length, 0)
  66.             textBox1.ScrollToCaret()
  67.             textBox2.Text += dados(1)
  68.             textBox2.Text += "°C" & vbCrLf
  69.             textBox2.[Select](textBox2.Text.Length, 0)
  70.             textBox2.ScrollToCaret()
  71.             chart1.Series(0).Points.Add(Convert.ToDouble(dados(0)))
  72.             chart1.Series(1).Points.Add(Convert.ToDouble(dados(1)))
  73.             Dim x As Integer = chart1.Series(0).Points.Count
  74.             If x > 100 Then chart1.ChartAreas(0).AxisX.ScaleView.Zoom(x - 100, x)
  75.         End Sub
  76.     End Class
  77. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement