我有一些C#和Visual Basic代码,包括此功能:
Public Function InitL(ByVal portNrL As Integer) As Output
Try
If ComPortL.IsOpen = True Then
ComPortL.Close()
End If
ComPortL.PortName = "Com" & portNrL
If ComPortL.IsOpen = False Then
ComPortL.Open()
End If
Return New Output(Output.ResultEnum.Successful, "")
Catch ex As Exception
Return New Output(Output.ResultEnum.Unsuccessful, ex.Message)
End Try
End Function
我在另一个这样的类中使用它:
Output result = x.InitL(Int32.Parse(portnumber));
if (result.Result == Output.ResultEnum.Unsuccessful)
{
MessageBox.Show(result.ErrorMessage);
enableAllButtons();
return;
}
当我选择一个繁忙的COM端口号时,我的程序挂起。当我插入一个断点并进行检查时,它会显示消息框,此后每次都可以正常工作,直到我再次选择一个繁忙的COM端口号为止,此时程序会间歇性挂起。
这是Output类:
Public Class Output
Public Result As ResultEnum
Public ErrorMessage As String
Public Enum ResultEnum
Successful = 1
Unsuccessful = 2
End Enum
Public Sub New(_result As ResultEnum, _errorMessage As String)
Result = _result
ErrorMessage = _errorMessage
End Sub
End Class