Public Sub New(ByVal intComNumber As Integer, ByVal lngBaudRate As Long, ByVal intDataLng As Integer, ByVal intStopBit As Integer, ByVal intParity As Integer) Try objUPSPort = New SerialPort With objUPSPort .PortName = ("COM" & intComNumber) .BaudRate = lngBaudRate .DataBits = intDataLng .StopBits = intStopBit .Parity = intParity .Handshake = Handshake.None End With objUPSPort.Open() Catch ex As Exception MsgBox("Error In Init UPSComm") End Try End Sub
Public Function GetStatus() As String Dim strRet As String Dim strRecv As String Dim byteRead() As Byte Try If Not IsNothing(objUPSPort) Then objUPSPort.Open() objUPSPort.WriteLine("Command will be here" & vbCrLf) For i = 0 To 100000 If objUPSPort.BytesToRead >= 45 Then Exit For End If Next ReDim byteRead(objUPSPort.BytesToRead) objUPSPort.Read(byteRead, 0, objUPSPort.BytesToRead) strRecv = String.Empty For i = 0 To byteRead.Length - 1 strRecv = strRecv & Chr(byteRead(i)) Next If byteRead(38) = 48 Then MsgBox("Power OK") ElseIf byteRead(38) = 49 Then MsgBox("Power Off") Else MsgBox("Unknown") End If strRet = strRecv Return strRecv Else MsgBox("Error In ComPort Object") Return String.Empty End If Catch ex As Exception MsgBox("Exception In ComPort Object - " & ex.Message) Return String.Empty Finally objUPSPort.Close() End Try End Function
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)