Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System
- Imports System.Text
- Imports System.Drawing
- Imports System.IO.Ports
- Imports System.Windows.Forms
- Imports System.Threading
- Public Class Form1
- Dim testo As String
- Public trd As Thread
- Delegate Sub SetTextCallback(ByVal [text] As String)
- Private Sub ConfigurazioneSerialeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfigurazioneSerialeToolStripMenuItem.Click
- SerialPort1.Close()
- 'trd.Suspend()
- Form2.ShowDialog()
- ' trd.Resume()
- End Sub
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- Form2.ShowDialog()
- 'trd = New Thread(AddressOf ThreadProcUnsafe)
- 'trd.IsBackground = True
- 'trd.Start()
- End Sub
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- If SerialPort1.IsOpen = False Then
- SerialPort1.Open()
- End If
- SerialPort1.Write(TextBox2.Text)
- TextBox1.ForeColor = Color.Blue
- TextBox1.Text &= "Msg Inviato:" & TextBox2.Text & vbCrLf
- TextBox2.Text = ""
- End Sub
- Private Sub ThreadProcUnsafe()
- Do
- testo = ""
- Try
- SerialPort1.ReadTimeout = 500
- Do
- Dim Incoming As String = SerialPort1.ReadLine()
- If Incoming Is Nothing Then
- Exit Do
- Else
- testo &= Incoming & vbCrLf
- End If
- Loop
- Catch ex As TimeoutException
- 'testo = "Nessuno dato ricevuto"
- End Try
- Thread.Sleep(1000)
- If testo <> "" Then
- Me.SetText(testo)
- End If
- Loop
- End Sub
- Private Sub Form1_Unload(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosed
- 'trd.Suspend()
- SerialPort1.Close()
- End Sub
- Private Sub SetText(ByVal [text] As String)
- If Me.TextBox1.InvokeRequired Then
- Dim d As New SetTextCallback(AddressOf SetText)
- Me.Invoke(d, New Object() {[text]})
- Else
- Me.TextBox1.Text = [text]
- End If
- End Sub
- Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
- testo = ""
- System.Threading.Thread.Sleep(300)
- testo = (SerialPort1.ReadExisting)
- If testo <> "" Then
- TextBox1.ForeColor = Color.Red
- TextBox1.Text &= "Msg Ricevuto:" & testo & vbCrLf
- End If
- End Sub
- End Class
Add Comment
Please, Sign In to add comment