1 module modbus.exception;
2 
3 import std.string : format;
4 
5 enum MINIMUM_MODBUS_MSG_LENGTH = 5;
6 
7 class ModbusException : Exception
8 {
9     this(string msg, string file=__FILE__, size_t line=__LINE__)
10         @nogc @safe pure nothrow
11     { super(msg, file, line); }
12 }
13 
14 class CheckCRCException : ModbusException
15 {
16     ubyte dev, fnc;
17 
18     this(ubyte dev, ubyte fnc, string file=__FILE__, size_t line=__LINE__)
19     {
20         this.dev = dev;
21         this.fnc = fnc;
22 
23         super(format("dev %d fnc %d(0x%x) recive msg CRC check fails",
24                     dev, fnc, fnc), file, line);
25     }
26 }
27 
28 class FunctionErrorException : ModbusException
29 {
30     ubyte dev, fnc, res;
31     FunctionErrorCode code;
32 
33     this(ubyte dev, ubyte fnc, ubyte res, ubyte code,
34             string file=__FILE__, size_t line=__LINE__)
35     {
36         this.dev = dev;
37         this.fnc = fnc;
38         this.res = res;
39         this.code = cast(FunctionErrorCode)code;
40 
41         super(format("dev %d fnc %d(0x%x) recive fnc %d(0x%x) with exception code %s (%d)",
42         dev, fnc, fnc, res, res, code, code), file, line);
43     }
44 }
45 
46 class ReadDataLengthException : ModbusException
47 {
48     ubyte dev, fnc;
49 
50     this(ubyte dev, ubyte fnc, size_t exp, size_t res,
51             string file=__FILE__, size_t line=__LINE__)
52     {
53         super(format("dev %d fnc to %d(0x%x) recieves wrong"~
54                     " count of bytes (%d != expected %d or more what %d)",
55                     dev, fnc, fnc, res, exp, MINIMUM_MODBUS_MSG_LENGTH), file, line);
56     }
57 }
58 
59 enum FunctionErrorCode : ubyte
60 {
61     ILLEGAL_FUNCTION     = 1,
62     ILLEGAL_DATA_ADDRESS = 2,
63     ILLEGAL_DATA_VALUE   = 3,
64     SLAVE_DEVICE_FAILURE = 4,
65     ACKNOWLEDGE          = 5,
66     SLAVE_DEVICE_BUSY    = 6,
67     MEMORY_PARITY_ERROR  = 8,
68 }