tokio-modbus icon indicating copy to clipboard operation
tokio-modbus copied to clipboard

Any example for ExceptionResponse?

Open ArcherGu opened this issue 3 years ago • 3 comments

I'm a rust newcomer and am building a modbus server, and when the client sends the wrong address I need to give feedback on the error message, whether there are any examples of ExpressinResponse

ArcherGu avatar Aug 30 '21 02:08 ArcherGu

Any response to this question? it appears to me that the exception definitions are in frame which is private. How could a server implementation produce an appropriate exception response in this case?

For what is worth, below is my workaround: the client side appears to be taken care of so if you are only interested in building a client the current implementation would work just fine. the server side however does not get support from this crate at the moment, the way I get it working is to overload the custom response type to send back the exception response. though it is not clean and could be confusing, but it works for now.

justinyhuang avatar Nov 29 '21 20:11 justinyhuang

I wanted to match on

Custom { kind: Other, error: ExceptionResponse { function: 3, exception: GatewayTargetDevice } } => {

}

but you are right frame is private where I find myself needing a few things from it. In the end I converted the error to a string and did a match (yuck)

glennpierce avatar Jan 17 '22 13:01 glennpierce

I would think that you could have the modbus server return an error response, by returning from call() like so:

 . . . 
    let fncode = tokio_modbus::coderc::req_to_fn_code(&req);
    return std::future::ready(Err(tokio_modbus::frame::ExceptionResponse {
        function: fncode,
        exception: tokio_modbus::frame::Exception::IllegalFunction,   // Or any other kind of Modbus exception
    }));
 . . .

Like mentioned before, for this to work, the following needs to be published:

  • frame::ExceptionResponse
  • frame::Exception
  • codec::req_to_fn_code()

The first 2 could be included in the prelude, like Request and Response are. The last 1 I am not sure about.

If you return with another kind of error than an ExceptionResponse, like std::io::Error, the application compiles just fine, but the connection is forcibly closed by the server when the error is returned.

The workaround from @justinyhuang (clever!) works fine:

        return std::future::ready(error_response(&req, Exception::IllegalFunction));

Copy the following functions and enum to make this work:

fn error_response(req: &Request, exc: Exception) -> std::io::Result<Response> {
    Ok(Response::Custom(req2functioncode(&req) | 0x80, vec![exc as u8]))
}

fn req2functioncode(req: &Request) -> FunctionCode {
    match *req {
        Request::ReadCoils(_, _) => 0x01,
        Request::ReadDiscreteInputs(_, _) => 0x02,
        Request::WriteSingleCoil(_, _) => 0x05,
        Request::WriteMultipleCoils(_, _) => 0x0F,
        Request::ReadInputRegisters(_, _) => 0x04,
        Request::ReadHoldingRegisters(_, _) => 0x03,
        Request::WriteSingleRegister(_, _) => 0x06,
        Request::WriteMultipleRegisters(_, _) => 0x10,
        Request::ReadWriteMultipleRegisters(_, _, _, _) => 0x17,
        Request::Custom(code, _) => code,
        _ => 0x00,
    }
}

#[repr(u8)]
enum Exception {
    IllegalFunction = 0x01,
    IllegalDataAddress = 0x02,
    IllegalDataValue = 0x03,
    ServerDeviceFailure = 0x04,
    Acknowledge = 0x05,
    ServerDeviceBusy = 0x06,
    MemoryParityError = 0x08,
    GatewayPathUnavailable = 0x0A,
    GatewayTargetDevice = 0x0B,
}

LenRemmerswaal avatar Mar 18 '22 11:03 LenRemmerswaal