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