Contact Info

Crumbtrail

ActiveXperts.com » Administration » VBScript » Network Monitor » Serial Device

Serial.vbs - Monitor a Serial Device like a modem or a UPS connected to a COM port

ActiveXperts Network Monitor ships with a powerful set of pre-defined checks. Each individual check has a static number of configuration items. To monitor other items, or to combine monitoring items, you can make use of custom VBScript checks.

Most of the built-in checks have a VBScript equivalent, implemented as a Function in a VBScript (.vbs) file. Out-of-the-box, each VBScript function monitors the same items as the built-in check. Feel free to modify a function. The VBScript check can be customized by editing the VBScript function.

To add a new VBScript-based Serial Device monitoring check, do the following:

To customize the above monitoring check, click on the 'Edit button' next to the 'File selection box'. Notepad will be launched. You can now make changes to the VBScript function(s).

Screenshot of a VBScript Serial Device check

Serial.vbs script source code

' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' // © ActiveXperts Software B.V.
' //
' // For more information about ActiveXperts Network Monitor and VBScript, please
' // visit the online ActiveXperts Network Monitor VBScript Guidelines at:
' //    https://www.activexperts.com/support/network-monitor/online/vbscript/
' // 
' ///////////////////////////////////////////////////////////////////////////////
'  

Option Explicit
Const  retvalUnknown = 1
Dim    SYSDATA, SYSEXPLANATION  ' Used by Network Monitor, don't change the names


' ///////////////////////////////////////////////////////////////////////////////

' // To test a function outside Network Monitor (e.g. using CSCRIPT from the
' // command line), remove the comment character (') in the following 5 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( "" )
' Sample:
'     CheckSerialDevice( "COM1" )

   Dim objComport, strResponse, strResponseAll, iResponse

   Set objComport = CreateObject( "AxSerial.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:\AxSerialPort.log"

   ' Open the device
   objComport.Open
   If( objComport.LastError <> 0 ) Then
      CheckSerialDevice =  retvalUnknown
      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 =  retvalUnknown
      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 =  retvalUnknown
      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