为什么我的for循环不起作用?

时间:2017-12-07 13:28:59

标签: vb.net visual-studio can-bus

我正在尝试运行一个小测试,以查看发送的值是否与收到的值匹配。但是,在大多数情况下,收到的价值似乎落后了一步!

For i As Decimal = 0 To 127
        j = Hex(i)
        stsResult = PCANBasic.Read(m_PcanHandle, myCANMsg, CANTimeStamp)

        CANMsg = New TPCANMsg()
        CANMsg.DATA = CType(Array.CreateInstance(GetType(Byte), 8), Byte())
        CANMsg.ID = Convert.ToInt32(240, 16)
        CANMsg.LEN = Convert.ToByte(8)
        CANMsg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD
        CANMsg.DATA(3) = Convert.ToByte(10, 16)
        CANMsg.DATA(4) = Convert.ToByte(j, 16)
        CANMsg.DATA(5) = Convert.ToByte(99, 16)
        PCANBasic.Write(m_PcanHandle, CANMsg)

        ReadAnalogVal() ' Sents a request message so that the it updates the message about to be read

        stsResult = PCANBasic.Read(m_PcanHandle, myCANMsg, CANTimeStamp) 'reads bus and populates var myCANMsg with the data 

        hiBit = Hex(myCANMsg.DATA(0))

        If (stsResult <> TPCANStatus.PCAN_ERROR_QRCVEMPTY) Then

            If myCANMsg.ID = 960 And hiBit = j Then
                MessageBox.Show("you sent: " & j & " and Recieved: " & hiBit)
            Else
                MessageBox.Show("Response did NOT match output.   Output:" & j & "   Recieved: " & hiBit)
            End If
        End If

    Next


    CANMsg = New TPCANMsg()
    CANMsg.DATA = CType(Array.CreateInstance(GetType(Byte), 8), Byte())
    CANMsg.ID = Convert.ToInt32(240, 16)
    CANMsg.LEN = Convert.ToByte(8)
    CANMsg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD
    CANMsg.DATA(3) = Convert.ToByte(0, 16)
    CANMsg.DATA(4) = Convert.ToByte(0, 16)
    PCANBasic.Write(m_PcanHandle, CANMsg)

    iBuffer = PCANBasic.PCAN_FILTER_OPEN
    result = PCANBasic.SetValue(PCANBasic.PCAN_USBBUS1, TPCANParameter.PCAN_MESSAGE_FILTER, iBuffer, Convert.ToUInt32(Len(iBuffer)))
    If result <> TPCANStatus.PCAN_ERROR_OK Then
        ' An error occurred, get a text describing the error and show it
        '
        PCANBasic.GetErrorText(result, 0, strMsg)
        MessageBox.Show(strMsg.ToString)
    Else
        MessageBox.Show("Filter is open again")

    End If
End Function

这似乎适用于某些情况,然后大多数情况下它不匹配,任何建议将不胜感激。这是我的第一个VB.NET应用程序,所以我可能会犯一个业余错误。我已经研究过多线程,但我并不完全确定如何在这种情况下使其工作。

以下是更多应用程序:

Imports System.Text

' Inclusion of PEAK PCAN-Basic namespace
'
Imports System.Text

Inclusion of PEAK PCAN-Basic namespace

Imports Peak.Can.Basic
Imports TPCANHandle = System.UInt16
Imports TPCANBitrateFD = System.String
Imports TPCANTimestampFD = System.UInt64

Public Class Form1

    Public Sub New()
        ' Initializes Form's component
        '
        InitializeComponent()
        ' Initializes specific components
        '
        InitializeBasicComponents()
    End Sub

    ''' <summary>
    ''' Message Status structure used to show CAN Messages
    ''' in a ListView
    ''' </summary>
    Private Class MessageStatus
    Private m_Msg As TPCANMsgFD
    Private m_TimeStamp As TPCANTimestampFD
    Private m_oldTimeStamp As TPCANTimestampFD
    Private m_iIndex As Integer
    Private m_Count As Integer
    Private m_bShowPeriod As Boolean
    Private m_bWasChanged As Boolean

    Public Sub New(ByVal canMsg As TPCANMsgFD, ByVal canTimestamp As TPCANTimestampFD, ByVal listIndex As Integer)
        m_Msg = canMsg
        m_TimeStamp = canTimestamp
        m_oldTimeStamp = canTimestamp
        m_iIndex = listIndex
        m_Count = 1
        m_bShowPeriod = True
        m_bWasChanged = False
    End Sub

    Public Sub Update(ByVal canMsg As TPCANMsgFD, ByVal canTimestamp As TPCANTimestampFD)
        m_Msg = canMsg
        m_oldTimeStamp = m_TimeStamp
        m_TimeStamp = canTimestamp
        m_bWasChanged = True
        m_Count += 1
    End Sub

    Private Function GetMsgTypeString() As String
        Dim strTemp As String

        If (m_Msg.MSGTYPE And TPCANMessageType.PCAN_MESSAGE_STATUS) = TPCANMessageType.PCAN_MESSAGE_STATUS Then
            Return "STATUS"
        End If

        If (m_Msg.MSGTYPE And TPCANMessageType.PCAN_MESSAGE_ERRFRAME) = TPCANMessageType.PCAN_MESSAGE_ERRFRAME Then
            Return "ERROR"
        End If

        If (m_Msg.MSGTYPE And TPCANMessageType.PCAN_MESSAGE_EXTENDED) = TPCANMessageType.PCAN_MESSAGE_EXTENDED Then
            strTemp = "EXT"
        Else
            strTemp = "STD"
        End If

        If (m_Msg.MSGTYPE And TPCANMessageType.PCAN_MESSAGE_RTR) = TPCANMessageType.PCAN_MESSAGE_RTR Then
            strTemp += "/RTR"
        Else
            If (m_Msg.MSGTYPE > TPCANMessageType.PCAN_MESSAGE_EXTENDED) Then
                strTemp += " [ "
                If ((m_Msg.MSGTYPE And TPCANMessageType.PCAN_MESSAGE_FD) = TPCANMessageType.PCAN_MESSAGE_FD) Then
                    strTemp += " FD"
                End If
                If ((m_Msg.MSGTYPE And TPCANMessageType.PCAN_MESSAGE_BRS) = TPCANMessageType.PCAN_MESSAGE_BRS) Then
                    strTemp += " BRS"
                End If
                If ((m_Msg.MSGTYPE And TPCANMessageType.PCAN_MESSAGE_ESI) = TPCANMessageType.PCAN_MESSAGE_ESI) Then
                    strTemp += " ESI"
                End If
                strTemp += " ]"
            End If
        End If

        Return strTemp
    End Function

    Private Function GetIdString() As String
        If (m_Msg.MSGTYPE And TPCANMessageType.PCAN_MESSAGE_EXTENDED) = TPCANMessageType.PCAN_MESSAGE_EXTENDED Then
            Return String.Format("{0:X8}h", m_Msg.ID)
        Else
            Return String.Format("{0:X3}h", m_Msg.ID)
        End If
    End Function

    Private Function GetDataString() As String
        Dim strTemp As String

        strTemp = ""

        If (m_Msg.MSGTYPE And TPCANMessageType.PCAN_MESSAGE_RTR) = TPCANMessageType.PCAN_MESSAGE_RTR Then
            strTemp = "Remote Request"
        Else
            For i As Integer = 0 To Form1.GetLengthFromDLC(m_Msg.DLC, (m_Msg.MSGTYPE And TPCANMessageType.PCAN_MESSAGE_FD) = 0) - 1
                strTemp += String.Format("{0:X2} ", m_Msg.DATA(i))
            Next
        End If

        Return strTemp
    End Function

    Private Function GetTimeString() As String
        Dim fTime As Double

        fTime = (m_TimeStamp / 1000.0R)
        If m_bShowPeriod Then
            fTime -= (m_oldTimeStamp / 1000.0R)
        End If

        Return fTime.ToString("F1")
    End Function

    Public ReadOnly Property CANMsg() As TPCANMsgFD
        Get
            Return m_Msg
        End Get
    End Property

    Public ReadOnly Property Timestamp() As TPCANTimestampFD
        Get
            Return m_TimeStamp
        End Get
    End Property

    Public ReadOnly Property Position() As Integer
        Get
            Return m_iIndex
        End Get
    End Property

    Public ReadOnly Property TypeString() As String
        Get
            Return GetMsgTypeString()
        End Get
    End Property

    Public ReadOnly Property IdString() As String
        Get
            Return GetIdString()
        End Get
    End Property

    Public ReadOnly Property DataString() As String
        Get
            Return GetDataString()
        End Get
    End Property

    Public ReadOnly Property Count() As Integer
        Get
            Return m_Count
        End Get
    End Property

    Public Property ShowingPeriod() As Boolean
        Get
            Return m_bShowPeriod
        End Get
        Set(ByVal value As Boolean)
            If m_bShowPeriod Xor value Then
                m_bShowPeriod = value
                m_bWasChanged = True
            End If
        End Set
    End Property

    Public Property MarkedAsUpdated() As Boolean
        Get
            Return m_bWasChanged
        End Get
        Set(ByVal value As Boolean)
            m_bWasChanged = value
        End Set
    End Property

    Public ReadOnly Property TimeString() As String
        Get
            Return GetTimeString()
        End Get
    End Property
End Class



''' <summary>
''' Read-Delegate Handler
''' </summary>
Private Delegate Sub ReadDelegateHandler()

''' <summary>
''' Saves the desired connection mode
''' </summary>
Private m_IsFD As Boolean
''' <summary>
''' Saves the handle of a PCAN hardware
''' </summary>
Private m_PcanHandle As TPCANHandle
''' <summary>
''' Saves the baudrate register for a conenction
''' </summary>
Private m_Baudrate As TPCANBaudrate
''' <summary>
''' Saves the type of a non-plug-and-play hardware
''' </summary>
Private m_HwType As TPCANType
''' <summary>
''' Stores the status of received messages for its display
''' </summary>
Private m_LastMsgsList As System.Collections.ArrayList
''' <summary>
''' Read Delegate for calling the function "ReadMessages"
''' </summary>
Private m_ReadDelegate As ReadDelegateHandler
''' <summary>
''' Receive-Event
''' </summary>
Private m_ReceiveEvent As System.Threading.AutoResetEvent
''' <summary>
''' Thread for message reading (using events)
''' </summary>
Private m_ReadThread As System.Threading.Thread
''' <summary>
''' Handles of the current available PCAN-Hardware
''' </summary>
Private m_HandlesArray As TPCANHandle()

0 个答案:

没有答案