Questo è il codice dell'applicazione:
codice:Public Class Form1 Dim inputData As String = "" Dim s As String = "" Public Event DataReceived As IO.Ports.SerialDataReceivedEventHandler Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'Set values for some properties SerialPort1.PortName = "COM1" SerialPort1.BaudRate = 19200 SerialPort1.Parity = IO.Ports.Parity.None SerialPort1.DataBits = 8 SerialPort1.StopBits = IO.Ports.StopBits.One SerialPort1.Handshake = IO.Ports.Handshake.None SerialPort1.RtsEnable = True ' Open the Serial Port SerialPort1.Open() 'Writes data to the Serial Port output buffer If SerialPort1.IsOpen = True Then SerialPort1.Write("PC Pronto") ListBox1.Items.Add("PC Pronto") End If End Sub ' Receive data from the Serial Port Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived inputData = SerialPort1.ReadExisting 'or SerialPort1.ReadLine Me.Invoke(New EventHandler(AddressOf DoUpdate)) End Sub 'Show received data on UI controls and do something Public Sub DoUpdate() s = inputData ListBox2.Items.Add(s) TextBox1.Text = s End Sub Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed ' Close the Serial Port SerialPort1.Close() End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click ListBox2.Items.Clear() End Sub Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click ListBox1.Items.Clear() End Sub 'Invia testo alla seriale Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click If SerialPort1.IsOpen = True Then SerialPort1.Write(TextBox2.Text) ListBox1.Items.Add(TextBox2.Text) Else MsgBox("Porta seriale non aperta", vbExclamation) End If End Sub End Class

Rispondi quotando