基于RS-232的VB通讯程序

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值