0

right now I am facing a weird problem as the code worked already and I don't know what I changed. I'm using the .NET API for vector CANoe to create test cases in c#. This just as a side fact.

In my testcase I'm performing a simple reset:

    [TestCase()]
    public void hardReset() {
        Response _response = UDS.resetDevice(ResetTypes.HARD);
        if (_response.IsPositive) {
            //Do something really great
        }
    }

So the the _response of the Type Response is created by the class 'UDS' with the reset function. This looks like this:

    public static Response resetDevice(byte resetType) {
        using (Ecu _ECU = Application.GetEcu(Qualifier.Name)) {
            byte[] _raw = new byte[] { 0x11, resetType };
            return sendRequestMessage(_raw, _ECU);
        }
    }

It opens the connection to the device, builds the request and sends the message, which returns the Response. Afterwards the connection is shut down by using(){}. The send function looks like this.

    private static Response sendRequestMessage(byte[] rawData, Ecu ECU) {
        Request _request = ECU.CreateRequest(rawData);
        SendResult _result = _request.Send();
        if (_result.Status == SendStatus.Ok) {
            return _result.Response;
        }
        return null;
    }

This is taken from the Vector GmbH manual and works as expected. If the request was send it returns _result.Response as it should. It's never null.

If I simply send the request, everything is fine, but as I take the '_response' (not null) and want to do something, it seems to be already disposed. Can someone show me the blind spot?

I already tried to declare the _response outside like this:

    public static Response resetDevice(byte resetType) {
        Response response = null;
        using (Ecu _ECU = Application.GetEcu(Qualifier.Name)) {
            byte[] _raw = new byte[] { 0x11, resetType };
            response =  sendRequestMessage(_raw, _ECU);
        }
        return response;
    }

The behavior is the same. The response is never null but gets disposed right away.

I found this hint in the meantime, but I'm not getting any better with this:

Response Comment

MSpiller
  • 3,500
  • 2
  • 12
  • 24
Piepe
  • 98
  • 4
  • 2
    Does Ecu has some sort of connection with the Response it has created? Maybe it disposes those stuff when itself gets disposed. I don't know what these classes are. If they are your own development then look up their source. – Ralf Mar 10 '23 at 11:29
  • 3
    I can easily imagine that Response refers to data that was buffered by Ecu. It is a common programming pattern. So if you dispose Ecu then the response is wiped out as well. Complete guess, but the shoe fits and you'll surely have to keep Ecu alive. – Hans Passant Mar 10 '23 at 13:21
  • I know I did it this way a few month ago and it worked, but I'll give it a shot when the hardware is available by next week again. – Piepe Mar 10 '23 at 13:50
  • Ok, so it seems to fix the problem at first if I leave the connection open but as I am now running into another problem (hardware based), the first code above might also work. It seems my hardware is not responding in the expected time and so the connection closes before the answer arrived. – Piepe Mar 14 '23 at 08:14

0 Answers0