# 참고동영상 |
https://www.youtube.com/watch?v=wsMMHaSvvRU |
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class Form1
Dim myPort As Array
Delegate Sub SetTextCallBack(ByVal [text] As String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
Port_ComboBox.Items.AddRange(myPort)
Write_Button.Enabled = False
End Sub
Private Sub init_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles init_Button.Click
SerialPort1.PortName = Port_ComboBox.Text
SerialPort1.BaudRate = Board_ComboBox.Text
SerialPort1.Open()
init_Button.Enabled = False
Write_Button.Enabled = True
Close_Button.Enabled = True
End Sub
Private Sub Write_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Write_Button.Click
SerialPort1.Write(Input_TextBox.Text & vbCr)
End Sub
Private Sub Close_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Close_Button.Click
SerialPort1.Close()
init_Button.Enabled = True
Write_Button.Enabled = False
Close_Button.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal text As String)
If Me.Output_TextBox.InvokeRequired Then
Dim x As New SetTextCallBack(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.Output_TextBox.Text &= text
Me.Label3.Text = text
End If
End Sub
End Class