Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
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


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
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
Posted
Comments
F. Xaver 27-Mar-14 7:01am    
So, whats your error, and where do you get it?

but basicaly .. why dont you use the SerialPort.DataReceived Event? its suitable for most cases!

when you want to wait for a response of your hardware.. that 0 to 100k for loop may be a to short time for your Hardware to respond to your command.
I had Projects where i needed to use Serialport in a simmilar way.. but i wouldn't recommend doing it that way
Luiey Ichigo 2-Apr-14 0:24am    
It error said out of bound of array.

May I have your advise Xaver? How to do it?
F. Xaver 2-Apr-14 8:58am    
puh.. i deleted that file.. but it was something like this ... if you are going to use that 'GetStatus' often.. it will lag your UI....

Dim timeout As Integer = 500
Dim endTime As Date = Now.AddMilliseconds(timeout)
Do
If yourserialport.BytesToRead >= 45 Then Exit Do
If Now > endTime Then Throw New TimeoutException("Hardware didn't respond intime")
Threading.Thread.Sleep(10)
Loop While True
'do stuff..

you don't check if you really received >= 45 Bytes.. if your For goes the 100k.. without getting any Data.. you still try to access byteRead(38) and maybee there is only 0 or 10 or whatever
I guess that courses your error
Bernhard Hiller 27-Mar-14 9:36am    
It is just "normal" that for many devices which previously were connected to a serial port, a USB to Serial Port adapter is used. When you install the drivers for the device, such a redirection gets installed. Your most important job is then to find out which port number was assigned to your device!
ledtech3 28-Mar-14 6:04am    
Would not this line be saying you want 38 bytes ?
If byteRead(38) = 48 Then

you might want to step thru it in the debugger to see what you are getting or andd more feed back.

1 solution

From your applications standpoint you don't even think of USB at all. Your code should just be concerned with serial communication, that's all. USB has NOTHING AT ALL to do with what your application is doing.

Since we know nothing of any error messages you're getting, what exactly you expect to the code to do, what you expect the code to do, what is actually happening in the code, ..., there's really nothing anyone can tell you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900