Option Explicit
Dim result, prt
Private Sub Form_Load()
Dim n As Integer
cmbport.ListIndex = 0
cmbspeed.Text = "1200"
cmdparity.Text = "even"
cmdDataBits.Text = 7
cmdStopBit.Text = 1
End Sub
Private Sub txtbin_Change()
MsComm.InBufferSize = txtbin.Text
Exit Sub
End Sub
Private Sub txtbout_Change()
MsComm.OutBufferSize = txtbout.Text
Exit Sub
End Sub
Private Sub cmdDataBits_LostFocus()
applySettings
Exit Sub
End Sub
Private Sub txtSend_Change()
If txtSend.Text = "" Then
cmdsend.Enabled = False
Else
cmdsend.Enabled = True
End If
End Sub
Private Sub cmdparity_Change()
applySettings
Exit Sub
End Sub
Private Sub cmbport_Change()
MsComm.CommPort = cmbport.Text
Exit Sub
End Sub
Public Sub applySettings()
Dim temp
chkParity (cmdparity.Text)
temp = cmbspeed.Text & "," & prt & "," & cmdDataBits & "," &
cmdStopBit.Text
MsComm.Settings = temp
Exit Sub
End Sub
Function chkParity(X As String) As String
Select Case X
Case "Even"
prt = "e"
Case "None"
prt = "n"
Case "Odd"
prt = "o"
Case "Mark"
prt = "m"
Case "Space"
prt = "s"
End Select
End Function
Private Sub cmbspeed_LostFocus()
applySettings
Exit Sub
End Sub
Private Sub cmdStopBit_LostFocus()
applySettings
Exit Sub
End Sub
Private Sub cmdconnect_Click()
If cmdconnect.Caption = "&Connect" Then
cmbport.Enabled = False
MsComm.PortOpen = True
cmdconnect.Caption = "&Disconnect"
connection.Caption = "Connection Established"
Else
cmbport.Enabled = True
cmdconnect.Caption = "&Connect"
MsComm.PortOpen = False
connection.Caption = "Establish A Connection"
End If
Exit Sub
End Sub
Private Sub cmdsend_Click()
If cmdconnect.Caption = "&Connect" Then resutl = MsgBox("Please establish a
connection first!", vbOKOnly + vbInformation, "Error"): Exit Sub
If MsComm.PortOpen Then
MsComm.Output = txtParam.Text & " says: " & txtSend.Text & vbCrLf
txtSend.Text = ""
txtReceive.Refresh
txtSend.SetFocus
End If
End Sub
Private Sub MsComm_OnComm()
Dim buffer As Variant
Dim msg As Integer
If MsComm.CommEvent = comEvReceive Then
buffer = MsComm.Input
txtReceive.Text = txtReceive.Text & buffer
txtReceive.Refresh
End If
End Sub