我正在创建一个应用程序,它通过串行电缆从连接到我的计算机的仪表读取值。当我按下一个按钮时,我向仪表发送一个命令,几毫秒后,我得到了一个回答来自仪表的响应。 我将这些值保存到具有属性init的类中,以便我可以从任何地方访问这些值。
所以我的问题是,当我尝试获取值时,它返回一个“没有值”,它可能来自初始化我有一个'新'就像这样' Dim clsSavedValues As new clsSavedValues ',所以当我尝试从该属性类中获取值时,我会创建一个新的instanse,如果没有错的话,该instanse为空。
我发布下面的代码,但这里是代码的流程: 我有3节课。 MainClass,ProtocolClass,PropertiesClass。 从main我调用ProtocolClass中的方法,该方法向仪表发送命令。在几毫秒之后,我在ProtocolClass内部得到一个回调,这个方法被称为'私有Sub SerialPort_DataReceived(ByVal sender As Object,ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)处理SerialPort.DataReceived '和它将返回值保存到PropertiesClass。 在DataReceived方法完成后,我返回MainClass并调用另一个方法从我刚刚保存的PropertiesClass中获取值,但它们返回null。我知道它们已正确保存,因为如果我从ProtocolClass中调用它们,我可以访问它们。但是它们在MainClass中是空的。
这是我的代码: 的 MainClass
'Here i call the ProtocolClass
Private Sub btnGetLastTransaction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetLastTransaction.Click
clsProtocol.GetLastTransaction(1, Integer.Parse(tbxTransactionPosition.Text))
End Sub
'Here i try to read the valies from PropertiesClass
Public Sub RetrieveMeterSerialNumber()
Dim clsSavedValues As New clsSavedValues
lblMeterSerialNumber.Text = clsSavedValues.SaveMeterSerialNumber
End Sub
ProtocolClass
Public Sub GetLastTransaction(ByVal destinationAddress As String, ByVal transactionNum As Integer)
clsSavedValues = New clsSavedValues 'Creating Instance of the properties class
Try
Dim v_bodyOfMessage As [Byte]() = {ASCIItoHEX("G"), _
ASCIItoHEX("r")}
Dim v_bytearray As [Byte]() = ConstructCommand(v_bodyOfMessage)
SendCommand(v_bytearray)
Catch ex As Exception
Console.WriteLine("Meter serial number button click exception: {0}", ex)
End Try
End Sub
Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
If comOpen Then
Try
ReDim rx(rxPacketSize)
Console.WriteLine("RESPONSE")
For i = 0 To rxPacketSize - 1
readByte = SerialPort.ReadByte.ToString
Console.WriteLine(i.ToString & ": " & Conversion.Int(readByte).ToString)
rx(i) = Conversion.Int(readByte).ToString
If i <> 0 Then
If Convert.ToByte(rx(i)) = vDelimeterFlag(0) Then Exit For
End If
Next
DecodeResponse()
Catch ex As Exception
MsgBox("SerialPort_DataReceived Exception: " & ex.Message)
End Try
End If
End Sub
Private Sub GetMeterSerialNumber()
Dim i_startPosition As Integer = 5
Dim meterSerialNumber As String = GetRemainingPortionOfString(i_startPosition)
clsSavedValues.SaveMeterSerialNumber = meterSerialNumber
frmExplorer.RetrieveMeterSerialNumber() 'This is the call to the main class
End Sub
PropertiesClass
Public Property SaveMeterSerialNumber() As String
Get
Return _MeterSerialNumber
End Get
Set(ByVal meterSerialNumber As String)
_MeterSerialNumber = meterSerialNumber
End Set
End Property
我想从PropertiesClass获取值,因为生病得到的不仅仅是来自仪表的响应,这会引起线程问题,我无法跟踪它们。所以我将值保存在一个类中,然后我想从该类中访问它们。 对不起,很长的帖子,问我想要澄清的任何内容
答案 0 :(得分:0)
主要:
Public Sub RetrieveMeterSerialNumber(clsSavedValues As clsSavedValues )
lblMeterSerialNumber.Text = clsSavedValues.SaveMeterSerialNumber
End Sub
协议:
Private Sub GetMeterSerialNumber()
Dim i_startPosition As Integer = 5
Dim meterSerialNumber As String = GetRemainingPortionOfString(i_startPosition)
clsSavedValues.SaveMeterSerialNumber = meterSerialNumber
frmExplorer.RetrieveMeterSerialNumber(clsSavedValues) 'This is the call to the main class
End Sub
或在PropertiesClass中使用静态属性