]> mj.ucw.cz Git - home-hw.git/blobdiff - lib/modbus.c
BSB: An attempt at sending
[home-hw.git] / lib / modbus.c
index cd1d34eed28a5abd29f3c24f97f412159c4d4c70..28f976df7b8e43f2b619043421c44aa5b5f8d3e7 100644 (file)
@@ -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;
+}