Question treat USB as RS232

liey87

New member
Joined
Nov 12, 2013
Messages
3
Programming Experience
3-5
I have a hardware with USB for communicate between computer to hardware. The vendor not giving any APIs to connect to the device. They give me a protocol. But the protocol is serve for RS232 mode. I ask the vendor whether this protocol can be apply to the USB, they said 'YES'.. So, I'm thirst of idea how to use this protocol. Does anyone know? My old friend said yes I can use the USB and treat is as COM which I need to create an object. Create instance of the object which declare as a serialport as below. But it still can't get the status.

VB.NET:
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
        Catch ex As Exception
            MsgBox("Error In Init UPSComm")
        End Try
End Sub

Can someone help me identified this? This hardware is UPS. A simple command write to the port. But I get the error when get status. Below is the code to write to the UPS.

VB.NET:
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
 
Back
Top