• Regolamento Macrocategoria DEV
    Prima di aprire un topic nella Macrocategoria DEV, è bene leggerne il suo regolamento. Sei un'azienda o un hosting/provider? Qui sono anche contenute informazioni per collaborare con Sciax2 ed ottenere l'accredito nella nostra community!

Problema Comunicazione seriale

Askodatw

Nuovo utente
Autore del topic
16 Agosto 2017
1
9
Miglior risposta
0
Ciao a tutti, ho bisogno di ricevere dei dati tramite seriale usando visual basic 2010, i dati vengono trasmessi via USB da arduino e a seconda del dato ricevuto visual basic deve aprire un Form da me impostati.
Premetto che ci ho già provato...ma purtroppo non riesco a capire cosa sbaglio e perché non funziona :C

codice utilizzato:

Imports System
Imports System.IO.Ports

Public Class Form1
Dim comPORT As String
Dim receivedData As String = ""
Dim inizio As String = "0"

Private Sub Form1_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 comPort_ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comPort_ComboBox.SelectedIndexChanged
If (comPort_ComboBox.SelectedItem <> "") Then
comPORT = comPort_ComboBox.SelectedItem
End If
End Sub


Private Sub connect_BTN_Click(sender As Object, e As EventArgs) Handles connect_BTN.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


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
receivedData = ReceiveSerialData()
RichTextBox1.Text &= receivedData
If receivedData = inizio Then
Form2.Show()
End If
End Sub


Function ReceiveSerialData() As String
Dim Incoming As String
Try
Incoming = SerialPort1.ReadExisting()
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 clear_BTN_Click(sender As Object, e As EventArgs) Handles clear_BTN.Click
RichTextBox1.Text = ""
End Sub
End Class
 
Ultima modifica:
Ciao a tutti, ho bisogno di ricevere dei dati tramite seriale usando visual basic 2010, i dati vengono trasmessi via USB da arduino e a seconda del dato ricevuto visual basic deve aprire un Form da me impostati.
Premetto che ci ho già provato...ma purtroppo non riesco a capire cosa sbaglio e perché non funziona :C

codice utilizzato:

Imports System
Imports System.IO.Ports

Public Class Form1
Dim comPORT As String
Dim receivedData As String = ""
Dim inizio As String = "0"

Private Sub Form1_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 comPort_ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comPort_ComboBox.SelectedIndexChanged
If (comPort_ComboBox.SelectedItem <> "") Then
comPORT = comPort_ComboBox.SelectedItem
End If
End Sub


Private Sub connect_BTN_Click(sender As Object, e As EventArgs) Handles connect_BTN.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


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
receivedData = ReceiveSerialData()
RichTextBox1.Text &= receivedData
If receivedData = inizio Then
Form2.Show()
End If
End Sub


Function ReceiveSerialData() As String
Dim Incoming As String
Try
Incoming = SerialPort1.ReadExisting()
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 clear_BTN_Click(sender As Object, e As EventArgs) Handles clear_BTN.Click
RichTextBox1.Text = ""
End Sub
End Class

esiste l'evento DataReceived , che viene lanciato quando ricevi dati , usa quello invece di un timer