Public bytreceivebyte() As Byte
Public intreceivelen As Integer
Public intput As Integer
Public strset As String
Public inttime As Integer
Public blnautosendflag As Boolean
Public blnreceiveflag As Boolean
Public intoutmode As Integer
Public strsend As String
Public bytsendbyte() As Byte
Public bytinput() As Byte
Public recopy As Integer
'发准备送模块
Private Sub Cmdsend_Click()
If Txtsend.Text = "" Then
List1.AddItem "发送缓冲区无数据"
Exit Sub
End If
frmmain.ctrMScomm.Output = Txtsend.Text '写入字节
End Sub
'关闭模块
Private Sub Cmdquit_Click()
Unload Me
End Sub
'串口初始化
Private Sub Form_Load()
ctrMScomm.CommPort = 1
ctrMScomm.Settings = "9600,N,8,1"
ctrMScomm.InputLen = 0
'端口打开
ctrMScomm.PortOpen = True
ctrMScomm.RThreshold = 1
ctrMScomm.SThreshold = 1
End Sub
'激发MSComm控件的OnComm事件,进行发送
Private Sub ctrMSComm_OnComm()
Select Case ctrMScomm.CommEvent
Case comEvReceive
'接受缓冲区里有字符的处理,即收到外来字符便显示
Dim intinputlen As Integer
If blnreceiveflag Then
If Not frmmain.ctrMScomm.PortOpen Then
frmmain.ctrMScomm.CommPort = intport
frmmain.ctrMScomm.Settings = strset
frmmain.ctrMScomm.PortOpen = True
End If
End If
frmmain.ctrMScomm.InputMode = comInputModeBinary
intinputlen = frmmain.ctrMScomm.InBufferCount
ReDim bytinput(intinputlen)
bytinput = frmmain.ctrMScomm.Input
Call inputmanage(bytinput, intinputlen)
Case comEvSend
'发送缓冲区里字符,显示有字符
List1.AddItem "发送缓冲区有数据"
End Select
End Sub
'文本写入,保存的处理
Public Sub inputmanage(bytinput() As Byte, intinputlenth As Integer)
Dim n As Integer
ReDim Preserve bytreceivebyte(intreceivelen + intinputlenth)
For n = 1 To intinputlenth
bytreceivebyte(intreceivelen + n - 1) = bytinput(n - 1)
Next n
intreceivelen = intreceivelen + intinputlenth
recopy = intinputlenth - 1
Call display
End Sub
'文本显示的处理
Public Sub display()
Dim i As Integer
For i = 0 To recopy
Txtreceive.Text = Txtreceive.Text & Chr(bytinput(i))
Next i
End Sub