VB.NET将USB视为RS232 [英] VB.NET Treat USB as RS232
问题描述
我有一个带USB的硬件,用于计算机与硬件之间的通信。供应商未提供任何API以连接到设备。他们给了我一个协议。但该协议适用于RS232模式。我问供应商这个协议是否适用于USB,他们说'是'。所以,我很想知道如何使用这个协议。有人知道吗?我的老朋友说是的,我可以使用USB并将其视为创建对象所需的COM。创建声明为serialport的对象的实例,如下所示。但它仍然无法获得状态。
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
objUPSPort.Open()
Catch ex As Exception
MsgBox("Error In Init UPSComm")
End Try
End Sub
有人可以帮我识别出来吗?这个硬件是UPS。一个简单的命令写入端口。但是我获得状态时会收到错误。以下是写入UPS的代码。
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
推荐答案
从您的应用程序角度来看,您根本不会想到USB。您的代码应该只关注串行通信,就是这样。 USB几乎没有关于你的应用程序正在做什么。
因为我们对你得到的任何错误消息一无所知,你对代码的期望是什么要做的,你期望代码做什么,代码中实际发生了什么,......,真的没有人能告诉你。
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.
这篇关于VB.NET将USB视为RS232的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!