Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

serial.vbs - vbscript script by ActiveXperts Software

serial.vbs queries a serial device.

Use serial.vbs directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select serial.vbs. Configure the required parameter, or press 'Load a working sample'.

In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.


serial.vbs script code

' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' // For more information about ActiveXperts Network Monitor and VBScript, visit
' // http://www.activexperts.com/support/network-monitor/online/vbscript/
' ///////////////////////////////////////////////////////////////////////////////

Option Explicit

' Declaration of global variables
Dim   SYSDATA, SYSEXPLANATION   ' SYSDATA is displayed in the 'Data' column in the Manager; SYSEXPLANATION in the 'LastResponse' column

' Constants - return values
Const retvalUnknown = 1         ' ActiveXperts Network Monitor functions should always return True (-1, Success), False (0, Error) or retvalUnknown (1, Uncertain)

' // To test a function outside Network Monitor (e.g. using CSCRIPT from the
' // command line), remove the comment character (') in the following lines:
' Dim bResult
' bResult =  CheckSerialDevice( "COM1" )
' WScript.Echo "Return value: [" & bResult & "]"
' WScript.Echo "SYSDATA: [" & SYSDATA & "]"
' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]"


Function CheckSerialDevice( strDevice ) 
' Description: 
'     Queries a serial device. Depending on the results, the result is set to True, 
'     False or Unknown
' Parameters:
'     1) strDevice As String  - COM port (like: COM1) or TAPI Device (like: Standard 9600 bps Modem)
' Usage:
'     CheckSerialDevice( "<COMx | TAPIDevice>" )
' Sample:
'     CheckSerialDevice( "COM1" )

  Dim objComport, strResponse, strResponseAll, iResponse

  Set objComport = CreateObject( "ActiveXperts.Comport" )

  ' Set Device property
  objComport.Device = strDevice

  ' Optionally, override device defaults
  ' objComport.BaudRate             = 57600
  ' objComport.HardwareFlowControl  = True
  ' objComport.SoftwareFlowControl  = False

  ' Optionally, set Logging - for troubleshooting purposes
  ' objComport.LogFile = "C:\ActiveComport.log"

  ' Open the device
  objComport.Open
  If( objComport.LastError <> 0 ) Then
    CheckSerialDevice =  False
    SYSEXPLANATION = "Open port failed, Error #" & objComport.LastError & ": " & objComport.GetErrorDescription( objComport.LastError )
    Exit Function
  End If

  ' Write command: "ATZ"
  objComport.WriteString "ATZ"
  If( objComport.LastError <> 0 ) Then
    CheckSerialDevice =  False
    SYSEXPLANATION    = "Write port failed, Error #" & objComport.LastError & ": " & objComport.GetErrorDescription( objComport.LastError )
    objComport.Close
    Exit Function
  End If 

  ' Wait for the response (expected response: "OK")
  strResponseAll = "[" & objComport.ReadString & "]"
  If( objComport.LastError <> 0 ) Then
    CheckSerialDevice =  False
    SYSEXPLANATION    = "Read port failed, Error #" & objComport.LastError & ": " & objComport.GetErrorDescription( objComport.LastError )
    objComport.Close
    Exit Function
  End If

  iResponse = 1
  While( objComport.LastError = 0 And iResponse < 10 )
    strResponse = "[" & objComport.ReadString & "]"
    If( objComport.LastError = 0 ) Then
      strResponseAll = strResponseAll & " " & strResponse
    End If
    iResponse = iResponse + 1
  WEnd

  If( InStr( UCase( strResponseAll ), "OK" ) ) Then
    CheckSerialDevice =  True
    SYSEXPLANATION    = "Response matched, response=" & strResponseAll
  Else
    CheckSerialDevice =  False
    SYSEXPLANATION    = "Response not matched, response=" & strResponseAll
  End If

  ' Close the port
  objComport.Close
  Set objComport = Nothing

End Function