You are on page 1of 2

'1 form with

'1 textbox : name=text1, multiline=true, scrollbars=3(both)


'1 textbox : name=text2
'1 command button
'1 mscomm control (Microsoft Comm Control 6.0)
Option Explicit

Private Sub Command1_Click()


With MSComm1
'make sure the serial port is open
If .PortOpen = False Then .PortOpen = True
'send the data (including a tailing carriage return as often needed)
.Output = Text2.Text & vbCr
End With 'MSComm1
With Text2
'place the focus back to the textbox
.SetFocus
'select the current text to be overwritten
.SelStart = 0
.SelLength = Len(.Text)
End With 'Text1
End Sub

Private Sub Form_Load()


With MSComm1
'make sure the serial port is not open (by this program)
If .PortOpen Then .PortOpen = False
'set the active serial port
.CommPort = 2
'set the badurate,parity,databits,stopbits for the connection
.Settings = "9600,N,8,1"
'set the DRT and RTS flags
.DTREnable = True
.RTSEnable = True
'enable the oncomm event for every reveived character
.RThreshold = 1
'disable the oncomm event for send characters
.SThreshold = 0
'open the serial port
.PortOpen = True
End With 'MSComm1
With Text1
'set the properties for the displaying textbox
.BackColor = vbCyan
.Locked = True
.Text = ""
End With 'Text1
With Text2
'set the properties for the 'send' textbox
.TabIndex = 0
.Text = ""
End With 'Text2
With Command1
'set the properties for the 'send' command button
.Caption = "&Send"
.Default = True
.TabIndex = 1
End With 'Command1
End Sub

Private Sub Form_Resize()


Dim sngWidth As Single, sngHeight As Single
Dim sngDisplayHeight As Single
Dim sngTxtWidth As Single
Dim sngCmdWidth As Single, sngCmdHeight As Single
'calculate the inner size of the form
sngWidth = ScaleWidth
sngHeight = ScaleHeight
With Command1
'resize and reposition the command button
sngCmdHeight = .Height
sngCmdWidth = .Width
sngDisplayHeight = sngHeight - sngCmdHeight
sngTxtWidth = sngWidth - sngCmdWidth
.Move sngTxtWidth, sngDisplayHeight, sngCmdWidth, sngCmdHeight
End With 'Command1
'resize and reposition the label
Text1.Move 0, 0, sngWidth, sngDisplayHeight
'resize and reposition the textbox
Text2.Move 0, sngDisplayHeight, sngTxtWidth, sngCmdHeight
End Sub

Private Sub MSComm1_OnComm()


Dim strInput As String
With MSComm1
'test for incoming event
Select Case .CommEvent
Case comEvReceive
'display incoming event data to displaying textbox
strInput = .Input
Text1.SelText = strInput
End Select
End With 'MSComm1
End Sub

You might also like