Automotive and Embedded Networks

cancel
Showing results for 
Search instead for 
Did you mean: 

Best way to scan for devnet devices? Undefined NC_STATE_ENUM values?

Hi, I am trying to scan a devicenet bus to determine what devices are connected.  The strategy I'm using is to open an explicit message object, and ask for the DN serial number, if I recieve a reply, I assume a device is connected.  Is there a better way to enumerate the devices connected to a bus? The problem I am having with this is that it is unreliable. Sometimes ncWaitForState returns the value "9" for the NC_STATE_ENUM which is *NOT* a valid enumerated value?  Is NC_STATE_ENUM really a bit mapped value and not an enumeration?  Which would indicate both a read is avaliable and a connection has been established? Any thoughts would be MUCH appreciated as I've been tearing my hair out over this one for the better part of a week.


Public Function Scan(LoBound As Integer, UpBound As Integer, ByRef List As Collection)

    Dim MacId As Integer, Status As Long, Handle As Long
    Dim CState As NC_STATE_ENUM
   
    For MacId = LoBound To UpBound

        Status = ncOpenDnetExplMsg(PortName, CInt(MacId), Handle)
        If ProcessError(Status) = True Then
       
            Sleep (5)
            Status = ncWaitForState(Handle, NC_ST_ESTABLISHED, 2200, CState)
            If ProcessError(Status) = True And CState = NC_ST_ESTABLISHED Then

            'Ask for the Devicenet serial number.  Every Device should have this.
            Status = ncWriteDnetExplMsg(Handle, _
                                CByte(14), _
                                CInt(1), _
                                CInt(1), _
                                CInt(1), _
                                CByte(6))
   
            If ProcessError(Status) = True Then
           
                Sleep (5)
                Status = ncWaitForState(Handle, NC_ST_READ_AVAIL, 500, CState)

                If ProcessError(Status) = True And CState = NC_ST_READ_AVAIL Then
                    List.Add (MacId)
                End If
            End If
       
            End If
       
        ncCloseObject Handle
        End If
       
    Next MacId

End Function

Private Function ProcessError(s As Long) As Boolean

    If (s <> NC_SUCCESS) Then

        myErr1.Number = s
        ncStatToStr Status, myErr1.Description
       
        ProcessError = False
        Exit Function

    End If
   
    ProcessError = True

End Function
0 Kudos
Message 1 of 2
(3,323 Views)

Hi Celeritas,

The value 9 for the state returning from the wait function is a combination of 1 and 8, which means  hex1 (read available) and hex 8 (established). For more info have a look to the programmers reference manual comming with the driver.

have you tried the example comming with the 1.5 driver? See the attached example.

DirkW

0 Kudos
Message 2 of 2
(3,308 Views)