Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Scivo le varie COM attive nella combobox
ComboBox1.Visible = True
SerialPort1.Close()
ComboBox1.Items.Clear()
ComboBox1.Items.AddRange(IO.Ports.SerialPort.GetPortNames())
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Seleziono la seriale e attivo il timer
ComboBox1.Visible = False
SerialPort1.Close()
If ComboBox1.Text <> "" Then
Try
SerialPort1.Encoding = System.Text.Encoding.Default
SerialPort1.PortName = ComboBox1.Text
SerialPort1.Open()
Button2.Text = "Connesso"
Label29.Visible = True
Label29.Text = "Collegato alla porta" + ComboBox1.Text
Timer1.Enabled = True
Catch ex As Exception
MsgBox(" Non Connesso")
End Try
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'Invia i dati ad arduino
SerialPort1.WriteLine(TextBox12.Text) ' invia il valore Set.Menisco
SerialPort1.WriteLine(TextBox14.Text) ' invia il valore velocità pompa mandata
Timer1.Enabled = True ' riattivo il timer
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Ogni 300ms leggo la seriale
Dim pressioneIn As Short
Dim pressioneOut As Short
Dim arduino As String = SerialPort1.ReadLine() 'scrivo in arduino quello che ricevo dalla seriqale
arduino = arduino.Trim() 'elimino eventuale porcheria
Dim lunghezza As Short = Len(arduino) 'ricavo la lunghezza del dato
Dim indice As String = arduino.Substring(0, 1) 'ricavo l'indice dai dati ricevuti per sapere dove mettere i valori
Dim valore As Short = arduino.Substring(1, lunghezza - 1) 'ricavo i valori
If indice = 0 Then
TextBox13.Text = valore 'valore menisco
End If
If indice = 1 Then
TextBox11.Text = valore 'velocità pompa ritorno
End If
If indice = 2 Then
TextBox10.Text = valore 'valore pressione mandata
pressioneIn = valore
End If
If indice = 3 Then
TextBox9.Text = valore 'valore pressione ritorno
pressioneOut = valore
End If
TextBox8.Text = pressioneIn - pressioneOut
End Sub
Private Sub TextBox14_TextChanged(sender As Object, e As EventArgs) Handles TextBox14.Click
'Fermo il timer per scrivere i dati
Timer1.Enabled = False
End Sub
Private Sub TextBox12_TextChanged(sender As Object, e As EventArgs) Handles TextBox12.Click
'Fermo il timer per scrivere i dati
Timer1.Enabled = False
End Sub
End Class