X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fmodbus.c;h=28f976df7b8e43f2b619043421c44aa5b5f8d3e7;hb=5286b9d20dd896a010e0d1db9f0813f5c489951e;hp=cd1d34eed28a5abd29f3c24f97f412159c4d4c70;hpb=20b74671177fb3a7c2dfd13aecbff4470f00f916;p=home-hw.git diff --git a/lib/modbus.c b/lib/modbus.c index cd1d34e..28f976d 100644 --- a/lib/modbus.c +++ b/lib/modbus.c @@ -96,6 +96,7 @@ static byte *rx_frame_end; static byte tx_buf[MODBUS_TX_BUFSIZE]; static u16 tx_size; static u16 tx_pos; +static byte pending_error; static bool check_frame(void); static void process_frame(void); @@ -370,6 +371,7 @@ enum mb_error { ERR_ILLEGAL_FUNCTION = 0x01, ERR_ILLEGAL_DATA_ADDRESS = 0x02, ERR_ILLEGAL_DATA_VALUE = 0x03, + ERR_SLAVE_DEVICE_FAILURE = 0x04, }; static uint read_remains(void) @@ -685,6 +687,7 @@ static void process_frame(void) tx_buf[0] = MODBUS_OUR_ADDRESS; tx_buf[1] = rx_buf[1]; tx_size = 2; + pending_error = 0; switch (func) { case FUNC_READ_COILS: @@ -724,6 +727,15 @@ static void process_frame(void) report_error(ERR_ILLEGAL_FUNCTION); } + // Is there a deferred error pending? + if (pending_error) + report_error(pending_error); + // Finish reply frame write_u16(crc16(tx_buf, tx_size)); } + +void modbus_slave_error(void) +{ + pending_error = ERR_SLAVE_DEVICE_FAILURE; +}