首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

关于SerialPort的一个有关问题

2012-12-14 
关于SerialPort的一个问题下是我的代码Private Sub SerialPortYKQ1_DataReceived(ByVal sender As Object,

关于SerialPort的一个问题
下是我的代码

  Private Sub SerialPortYKQ1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPortYKQ1.DataReceived
        If closingS = True Then Exit Sub
        Try
            Sleep(100)
            listeningS = True
            Dim bytesInput() As Byte          
            Dim intInputLen As Integer
            intInputLen = SerialPortYKQ1.BytesToRead

            If intInputLen >= 2 Then
                ReDim bytesInput(intInputLen - 1)
                SerialPortYKQ1.Read(bytesInput, 0, intInputLen)
                myfunction(bytesInput, intInputLen)
            End If

        Catch ex As Exception
            BLL.GetIni.WriteLogText(ex.Message)
            listeningS = False
        Finally
            listeningS = False
        End Try
    End Sub
 Delegate Sub _dlmyfunction(ByVal bytesRead() As Byte, ByVal intInputLen As Integer)
    Private Sub myfunction(ByVal bytesRead() As Byte, ByVal intInputLen As Integer)
        Dim d As New _dlmyfunction(AddressOf ShowSB)
        Me.Invoke(d, bytesRead, intInputLen)
    End Sub

    Private Sub ShowSB(ByVal bytesRead() As Byte, ByVal intInputLen As Integer)

        Dim i As Integer = 0
        Dim SaS As String = ""
        Dim flagPostion1 As Integer = 0
        Dim flagpostion2 As Integer = 0
        For i = 1 To intInputLen Step 1
            SaS = SaS & "," & Hex(bytesRead(i - 1))
        Next i

        flagPostion1 = InStr(SaS, "AA,55")
        If flagPostion1 > 0 Then
            bl_TongXun = True


        Else
            bl_TongXun = False
        End If

        flagpostion2 = InStr(SaS, "5A,5A")
        If flagpostion2 > 0 Then
            ChangeDataFromSB(bytesRead, intInputLen)
        End If

    End Sub
 Private Sub ChangeDataFromSB(ByVal bytesInput() As Byte, ByVal intInputLen As Integer)                
                   SendAllPointStatusBefore(PointStatus, Points)'刷新状态
                 If bl_TongXun = False Then
                         SendAllPointStatusBefore(PointStatus, Points)
                  End If
 End Sub
 Public sub SendAllPointStatusBefore(ByVal PointStatus() As String, ByVal Points As Integer) 
        Dim Js(15) As Byte
        SerialPortYKQ1.Write(Js, 0, 16)
        Sleep(200)
 End sub


当我接收到下位机发送过来的数据,并进入到ChangeDataFromSB()这个函时,我在ChangeDataFromSB()函数中调用SendAllPointStatusBefore()给下位机发送内容,下位机接收到内容后会反馈信息给我。为什么,在ChangeDataFromSB()函数中执行完第一条语句,就是刷新状态,我的bl_TongXun这个变量的值没有改变呢。难道不是发送之后下位机有反馈值了,要先进入dataReceive事件的吗?
[最优解释]
Sleep(200) 后面用 application.doevents
这样才能触发其他事件

[其他解释]
怎么都没人回复啊,请各位给点提示好吗?谢谢!
[其他解释]
晕,居然还没人回,我都等了一个下午了!
[其他解释]
  serialport 网上的例子 不多   我也是 在学这方面的东西   但是 还没有 很深入  刚刚起步  
[其他解释]
收数据肯定在DataReceived中
另外,DataReceived中不要Sleep
[其他解释]
引用:
收数据肯定在DataReceived中
另外,DataReceived中不要Sleep
为什么没有触发DataReceived事件呢?
[其他解释]
那要看你SerialPort设置的事件触发方式了
可以是每接受一个字节触发,也可以是接受一定数量的字节再触发的
具体属性不知道,VB的MSCOM是有这个属性的
[其他解释]
引用:
那要看你SerialPort设置的事件触发方式了
可以是每接受一个字节触发,也可以是接受一定数量的字节再触发的
具体属性不知道,VB的MSCOM是有这个属性的
我设置为收到一个字节就触发。

热点排行