Question

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.

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.

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
Was it helpful?

Solution


I had few experiences in RS232 comm with USB, as nowadays laptops/pc they dont come with serial port no more. Serial ports usually emulated by USB, using [TTL-to-RS232 transistor, MAX like] some common supplier would use prolific as driver to emulate USB-to-RS232. First you need to know the data type, a simple string or binary.

SerialPorts is event driven, data coming thru the ports can trigger events. I assume to get UPS to send you status, first, you need to send command, as such [some pseudo];

objUPSPort.WriteLine("Command will be here" & vbCrLf)

There two ways to get the data:

  • Using data receive event driven :
Private Sub objUPSPort_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles objUPSPort.DataReceived
    'call ReceiveData()
End Sub
  • Create a pooling thread to read data periodically
Private Sub threadUPSReceive() 
      Do 
          data = objUPSPort.ReadLine() 'for string
          'process the data here or call ReceiveData()
      Loop 
End Sub

If data stream to be read is binary (similar like yours):

Private Function ReceiveData()

        Dim bRead As Integer
        Dim returnStr As String = vbEmpty
        bRead = objUPSPort.BytesToRead 'Number of Bytes to read
        Dim cData(bRead - 1) As Byte

        For Each b As Byte In cData
            returnStr += Chr(b) 'put data stream in readable ascii     
        Next

        Return returnStr
End Sub

One more thing, make sure the baudrate/stopbit/databit is set correctly. Hope this help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top