我有一个Delphi程序,它与我创建的vb.net dll进行通信。 vb.net dll通过RS232连接与Fluke示波表通信。我创建了一个通过RS232正确通信的程序,给了我预期的价值。但是,当我将它打包为dll并从我的Delphi程序访问dll时,我总是收到值“1”,而不是我期望的值(大多数情况下是240左右的值)。
我知道我的delphi程序肯定与dll正确通信,我的dll肯定是与示波器通信。这只是返回的值,这是问题!希望我在这里做了一些非常愚蠢的事情,但是我看不到它,它开始让我发疯!
这是我的vb.net代码:
Public Interface IFlukeComm
Function GetReading(ByVal Command As String, ByVal PortNum As Integer) As String
Function Test() As String
End Interface
Public Class FComm : Implements IFlukeComm
Private WithEvents moRS232 As Rs232
Dim InvokeRequired As Boolean
Dim TxResult As String
Private Sub OpenCom(ByVal PortNum As Integer)
Dim sTx As String
moRS232 = New Rs232()
Try
With moRS232
.Port = PortNum
.BaudRate = 1200
.DataBit = 8
.StopBit = Rs232.DataStopBit.StopBit_1
.Parity = Rs232.DataParity.Parity_None
.Timeout = 1500
End With
moRS232.Open()
moRS232.Dtr = True
moRS232.Rts = True
moRS232.EnableEvents()
moRS232.PurgeBuffer(Rs232.PurgeBuffers.TxClear Or Rs232.PurgeBuffers.RXClear)
sTx = "PC 9600"
sTx += ControlChars.Cr
moRS232.Write(sTx)
moRS232.Close()
Catch ex As Exception
End Try
End Sub
Private Sub moRS232_CommEvent(ByVal source As Rs232, ByVal Mask As Rs232.EventMasks) Handles moRS232.CommEvent
If (Mask And Rs232.EventMasks.RxChar) > 0 Then
TxResult = source.InputStreamString
End If
End Sub
Public Function GetReading(ByVal Command As String, ByVal PortNum As Integer) As String Implements IFlukeComm.GetReading
OpenCom(PortNum)
Threading.Thread.Sleep(500)
With moRS232
.Port = PortNum
.BaudRate = 9600
.DataBit = 8
.StopBit = Rs232.DataStopBit.StopBit_1
.Parity = Rs232.DataParity.Parity_None
.Timeout = 1500
End With
moRS232.Open()
moRS232.Dtr = True
moRS232.Rts = True
moRS232.EnableEvents()
Command += ControlChars.Cr
moRS232.Write(Command)
Threading.Thread.Sleep(500)
moRS232.Close()
If TxResult <> "" Then
'moRS232.Read(10)
Dim sRead As String = moRS232.InputStreamString
Return sRead
Else
Return "Invalid"
End If
End Function
Public Function Test() As String Implements IFlukeComm.Test
Return "Success"
End Function
End Class
这是我的Delphi代码:
procedure TfrmLauncher.btnM1UNClick(Sender: TObject);
begin
ShowMessage(GetMeasurements('QM 11',cmbM1PC.ItemIndex + 1));
end;
function TfrmLauncher.GetMeasurements(strType : string; iPort : Integer) : string;
var
IFC : IFlukeComm;
begin
IFC := CreateComObject(CLASS_FComm) as IFlukeComm;
Result := IFC.GetReading(strType,iPort);
end;
答案 0 :(得分:0)
TxResult = source.InputStreamString
Dim sRead As String = moRS232.InputStreamString
我不使用VB.net,但看起来好像你可能试图两次读取接收缓冲区。 InputStreamString是允许这样做的,还是在读取时为空?