开发者

Issue communicating via RS232 - VB.net dll / Delphi win32 exe

开发者 https://www.devze.com 2023-04-07 03:12 出处:网络
I have a Delphi program that communicates with a vb.net dll that I created.The vb.net dll communicates with a Fluke scope meter via an RS232 connection.I have created a program that communicates corre

I have a Delphi program that communicates with a vb.net dll that I created. The vb.net dll communicates with a Fluke scope meter via an RS232 connection. I have created a program that communicates correctly via RS232, giving me the anticipated value. Though, when I package this up as a dll and access the dll from my Delphi program I always receive a value of "1", rather than the value I was expecting (most cases a value in and around 240).

I know that my delphi program is definitely communicating correctly with the dll and that my dll is definitely communicating with the scope meter. It is just the value that is returned which is the issue! Hopefully I have simply done something very silly here, but I can't see it and it is starting to drive me mad!

Here is my vb.net code:

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)

    Wi开发者_开发知识库th 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

Here is my Delphi code:

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;


TxResult = source.InputStreamString

Dim sRead As String = moRS232.InputStreamString

I don't use VB.net, but it looks as if you may be attempting to read the receive buffer twice. Does InputStreamString permit this, or does it empty on read?

0

精彩评论

暂无评论...
验证码 换一张
取 消