程序要不断向串口取数,因距离问题,每次取数都延迟3秒,延迟时,所有程序不动了,这样一来,使用户感觉系统很差,他想在此同时点击其他都不可以。不知大家在这种情况下是如何处理,是多线程吗?谢谢!

解决方案 »

  1.   

    “每次取数都延迟3秒”这个延迟也太长了,不能用php做了!
      

  2.   

    To  实心木头人:
    好像不行,收不到数据
    MSComm1.InBufferCount = 0    '清空接收缓冲区
       MSComm1.Output = data_out  '发送数据,这里加上 sleep 500,可以收到数据
       
       Do
           DoEvents
       Loop Until MSComm1.OutBufferCount = 0          '等待,直到数据发送完毕
      

  3.   

    To  实心木头人:
    好像不行,收不到数据
    MSComm1.InBufferCount = 0    '清空接收缓冲区
       MSComm1.Output = data_out  '发送数据,这里加上 sleep 500,可以收到数据
       
       Do
           DoEvents
       Loop Until MSComm1.OutBufferCount = 0          '等待,直到数据发送完毕————————————————————
    你sleep 500幹嗎?
      

  4.   

    以下是我在项目中的一个函数,应可以解决你的问题:
    Private Sub MoveBox(ByVal sSection As String, ByVal sAddress As String, ByVal sWay As String, ByVal sMode As String, ByVal COM As MSComm)
        On Error Resume Next
        Dim sBurlLayer As String
        Dim sDisplayBLAddress As String
        Dim i As Long
        Dim dStart As Date
        Dim dEnd As Date
        
        Dim sCurMode As String '当前模式
        Dim sCurWay As String  '当前状态
        Dim sCurAddress As String '当前地址
        
        bIsStop = False
        sReturn = COM.Input
        sReturn = ""
        
        If Trim(FilePosition.sBurl) = "" Then
            FilePosition.sBurl = "1"
        End If
        If Trim(FilePosition.sLayer) = "" Then
            FilePosition.sLayer = "1"
        End If
        If Trim(FilePosition.sAddress) = "" Then
            sDisplayBLAddress = GetAddress(sAddress)
        Else
            sDisplayBLAddress = FilePosition.sAddress
        End If
        sBurlLayer = Format(Right(FilePosition.sBurl, 1), "0") & Format(Right(FilePosition.sLayer, 1), "0")
        
    '    Sleep (100)
    '    Call SendData(sSection, sAddress, "n", ":s", COM) '允许
        Sleep (100)
        Call SendData(sSection, sAddress, sWay, sMode, COM)
        dStart = Now
        Do Until bExit = True
            DoEvents:
            dEnd = Now
            i = (dEnd - dStart) * (10 ^ 5)
            If i > 140 Then
    '            Call SendData(sSection, sAddress, "y", ":s", COM) '允许
    '            Sleep (100)
    '            Call SendData(sSection, sAddress, sWay, ":c", COM) '禁止
                Unload frmMessage
                Screen.MousePointer = 0
                bExit = True
            Else
                If COM.InBufferCount > 23 Then
                    sReturn = COM.Input
                    
                    sCurMode = Mid(sReturn, 2, 1)
                    sCurWay = Mid(sReturn, 19, 1)
                    sCurAddress = Mid(sReturn, 5, 4)
                    
                    If sCurMode = "c" Then
                        Screen.MousePointer = 0
                        bIsStop = True
                        bExit = True
                    End If
                    If sCurMode = "g" Then
                        Select Case sCurWay
                            Case "m" '门禁-有人进入
                                bExit = True
                                MsgBox "列 " & sCurAddress & " 门禁-有人进入"
                                bExit = True
                            Case "t" '时间到
                                bExit = True
                                MsgBox "列 " & sCurAddress & " 时间到"
                            Case "u" '超速
                                bExit = True
                                MsgBox "列 " & sCurAddress & " 超速"
                            Case "d" '电眼坏
                                bExit = True
                                MsgBox "列 " & sCurAddress & " 电眼坏"
                            Case "x"
                                bExit = True
                                Sleep (100)
                                Call SendData(sSection, sAddress, sWay, ":c", COM) '停止
                                MsgBox "列 " & sCurAddress & " 走廊有人"
                            Case "r" '复位
                                dStart = Now
                                j = 140 * 5
                                Sleep (100)
                                Call SendData(sSection, sAddress, sWay, sMode, COM)
                            Case Else
                        End Select
                    End If
                End If
            End If
        Loop
        Select Case Mid(sReturn, 2, 1)
            Case "s"
                MsgBox "已禁止"
            Case "c"
                If bDisplayBurlLayer = True Then
                    Sleep (100)
                    Call SendData(sBurlLayer, sDisplayBLAddress, sWay, ":j", COM) '显示打开的节与层
                End If
                MsgBox "已停止或打开到位"
            Case Else
                Call SendData(sSection, sAddress, "y", ":s", COM) '允许
                Sleep (100)
                Call SendData(sSection, sAddress, sWay, ":c", COM) '停止
    '            MsgBox "打开失败"
        End Select
        If bExit = True Then
            Unload Me
        End If
    End Sub
      

  5.   

    看我接收怎么错了?   '开始接收
        N = MSComm1.InBufferCount
        If N > 0 Then
           MSComm1.InputLen = N
           ReDim data_in(N) As Byte
           data_in = MSComm1.Input
           Do While True        '数据接收
             
               DoEvents
               Sleep 100
               If MSComm1.InBufferCount >= N Then Exit Do    //感觉这里不对
           
           Loop
            
        End If