1 /// 2 module modbus.protocol.slave.types; 3 4 import modbus.types; 5 import modbus.backend; 6 7 /// 8 struct Response 9 { 10 /// 11 void[] data; 12 /// 13 bool error; 14 15 static auto fail(T)(T val) 16 { return Response(cast(void[])[val], true); } 17 18 /// Response with error code (1) 19 enum illegalFunction = fail(FunctionErrorCode.illegalFunction); 20 /// Response with error code (2) 21 enum illegalDataAddress = fail(FunctionErrorCode.illegalDataAddress); 22 /// Response with error code (3) 23 enum illegalDataValue = fail(FunctionErrorCode.illegalDataValue); 24 /// Response with error code (4) 25 enum slaveDeviceFailure = fail(FunctionErrorCode.slaveDeviceFailure); 26 /// Response with error code (5) 27 enum acknowledge = fail(FunctionErrorCode.acknowledge); 28 /// Response with error code (6) 29 enum slaveDeviceBusy = fail(FunctionErrorCode.slaveDeviceBusy); 30 /// Response with error code (8) 31 enum memoryParityError = fail(FunctionErrorCode.memoryParityError); 32 } 33 34 /// 35 interface ResponseWriter 36 { 37 /// 38 Backend backend() @property; 39 /// 40 protected void[] buffer() @property; 41 42 /// 43 Response pack(Args...)(Args args) 44 { 45 size_t idx; 46 backend.recursiveAppend(buffer, idx, args); 47 return Response(buffer[0..idx]); 48 } 49 50 /// improve usability: wrap pack method with sending length of array 51 final Response packArray(ushort[] arr) 52 { 53 auto bc = (cast(void[])arr).length; 54 if (bc >= ubyte.max) 55 throwModbusException("so big array for pack to message"); 56 return pack(cast(ubyte)bc, arr); 57 } 58 }