Anda di halaman 1dari 4

Imports Winsoft.

ComPort

Public Class FormTerminal

Private Sub ButtonConfig_Click(sender As System.Object, e As


System.EventArgs) Handles ButtonConfig.Click
ComPort.ConfigDialog()
End Sub

Private Sub ButtonSetRTS_Click(sender As System.Object, e As


System.EventArgs) Handles ButtonSetRTS.Click
ComPort.SetRts()
End Sub

Private Sub ButtonClearRTS_Click(sender As System.Object, e As


System.EventArgs) Handles ButtonClearRTS.Click
ComPort.ClearRts()
End Sub

Private Sub ButtonSetDTR_Click(sender As System.Object, e As


System.EventArgs) Handles ButtonSetDTR.Click
ComPort.SetDtr()
End Sub

Private Sub ButtonClearDTR_Click(sender As System.Object, e As


System.EventArgs) Handles ButtonClearDTR.Click
ComPort.ClearDtr()
End Sub

Private Sub FormTerminal_Load(sender As System.Object, e As System.EventArgs)


Handles MyBase.Load
ComboBoxDeviceName.Items.AddRange(ComPort.EnumComDevicesFromRegistry())
If ComboBoxDeviceName.Items.Count > 0 Then
ComboBoxDeviceName.SelectedIndex = 0
Else
ButtonOpen.Enabled = False
ButtonConfig.Enabled = False
End If
ComPort.DeviceName = "\\.\" + ComboBoxDeviceName.Text

AddReadBytes(0)
AddWriteBytes(0)
SetModemStatus()
End Sub

Private Sub ButtonOpen_Click(sender As System.Object, e As System.EventArgs)


Handles ButtonOpen.Click
ComPort.Active = Not ComPort.Active
End Sub

Private Sub ComboBoxDeviceName_SelectedIndexChanged(sender As System.Object,


e As System.EventArgs) Handles ComboBoxDeviceName.SelectedIndexChanged
ComPort.DeviceName = "\\.\" + ComboBoxDeviceName.Text
End Sub

Private Sub ComPort_Opened(sender As System.Object, e As System.EventArgs)


Handles ComPort.Opened
UpdateInfo()
End Sub
Private Sub ComPort_Closed(sender As System.Object, e As System.EventArgs)
Handles ComPort.Closed
UpdateInfo()
End Sub

Private Sub UpdateInfo()


Dim active As Boolean
active = ComPort.Active
If active Then ButtonOpen.Text = "Close" Else ButtonOpen.Text = "Open"
TextBox.Enabled = active
If active Then TextBox.Focus()
ComboBoxDeviceName.Enabled = Not active
ButtonSetRTS.Enabled = active
ButtonClearRTS.Enabled = active
ButtonSetDTR.Enabled = active
ButtonClearDTR.Enabled = active
End Sub

Private readCount As Integer

Private Sub AddReadBytes(ByVal value As Integer)


readCount = readCount + value
ToolStripStatusRead.Text = "Read bytes: " + readCount.ToString()
End Sub

Private writeCount As Integer

Private Sub AddWriteBytes(ByVal value As Integer)


writeCount = writeCount + value
ToolStripStatusWrite.Text = "Write bytes: " + writeCount.ToString()
End Sub

Private Sub TextBox_KeyPress(sender As System.Object, e As


System.Windows.Forms.KeyPressEventArgs) Handles TextBox.KeyPress
Try
ComPort.WriteChar(e.KeyChar)
Catch exception As ComException
MessageBox.Show(exception.Message, Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub ComPort_Written(sender As System.Object, e As


Winsoft.ComPort.ReadWriteEventArgs) Handles ComPort.Written
AddWriteBytes(e.Value.Length)
End Sub

Private Sub ComPort_RxChar(sender As System.Object, e As System.EventArgs)


Handles ComPort.RxChar
Dim text As String
text = ComPort.ReadString()
TextBox.SelectedText = text
AddReadBytes(text.Length)
End Sub

Private Sub SetModemStatus()


Dim modemStatus As ModemStatus
If ComPort.Active Then
modemStatus = ComPort.ModemStatus
Else
modemStatus = 0
End If

If (modemStatus And modemStatus.Cts) <> 0 Then


ToolStripStatusCts.Text = "CTS: On"
Else
ToolStripStatusCts.Text = "CTS: Off"
End If

If (modemStatus And modemStatus.Dsr) <> 0 Then


ToolStripStatusDsr.Text = "DSR: On"
Else
ToolStripStatusDsr.Text = "DSR: Off"
End If

If (modemStatus And modemStatus.Ring) <> 0 Then


ToolStripStatusRing.Text = "Ring: On"
Else
ToolStripStatusRing.Text = "Ring: Off"
End If

If (modemStatus And modemStatus.Rlsd) <> 0 Then


ToolStripStatusRlsd.Text = "RLSD: On"
Else
ToolStripStatusRlsd.Text = "RLSD: Off"
End If
End Sub

Private Sub ComPort_CtsChange(sender As System.Object, e As System.EventArgs)


Handles ComPort.CtsChange
SetModemStatus()
End Sub

Private Sub ComPort_DsrChange(sender As System.Object, e As System.EventArgs)


Handles ComPort.DsrChange
SetModemStatus()
End Sub

Private Sub ComPort_Ring(sender As System.Object, e As System.EventArgs)


Handles ComPort.Ring
SetModemStatus()
End Sub

Private Sub ComPort_RlsdChange(sender As System.Object, e As


System.EventArgs) Handles ComPort.RlsdChange
SetModemStatus()
End Sub

Private Sub ShowLineError(ByVal message As String)


MessageBox.Show(message, "Line Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Sub

Private Sub ComPort_LineError(sender As System.Object, e As


Winsoft.ComPort.LineErrorEventArgs) Handles ComPort.LineError
If (e.LineErrors And LineErrors.Break) <> 0 Then ShowLineError("Break
detected")
If (e.LineErrors And LineErrors.LptDeviceNotSelected) <> 0 Then
ShowLineError("LPT device not selected")
If (e.LineErrors And LineErrors.RxFrame) <> 0 Then ShowLineError("Frame
error")
If (e.LineErrors And LineErrors.LptIOError) <> 0 Then ShowLineError("IO
error")
If (e.LineErrors And LineErrors.UnsupportedMode) <> 0 Then
ShowLineError("Unsupported mode")
If (e.LineErrors And LineErrors.LptOutOfPaper) <> 0 Then
ShowLineError("LPT device out of paper")
If (e.LineErrors And LineErrors.RxOverrun) <> 0 Then
ShowLineError("Overrun detected")
If (e.LineErrors And LineErrors.LptTimeOut) <> 0 Then
ShowLineError("LPT device timeout")
If (e.LineErrors And LineErrors.RxOverflow) <> 0 Then
ShowLineError("Receiver overflow")
If (e.LineErrors And LineErrors.RxParity) <> 0 Then
ShowLineError("Parity error")
If (e.LineErrors And LineErrors.TxFull) <> 0 Then
ShowLineError("Transmitter full")
End Sub
End Class
-------------------------------------------------

Anda mungkin juga menyukai