- the return value is usable for determining whether a potentially
lengthy packet processing went on.
return MB_OK;
}
-void modbus_poll()
+uint8_t modbus_poll()
{
bufptr_t packet_len;
uint16_t crc;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
if (transmitting)
- return;
+ return 0;
if (buf_len == 0) // nothing received yet
- return;
+ return 0;
if (get_clock() - last_rx < REQ_TIMEOUT) // still receiving
- return;
+ return 0;
if (buf_len < 4) { // too short
buf_len = 0;
- return;
+ return 0;
}
if (buffer[0] != mb_unit_id) { // not for myself
buf_len = 0;
- return;
+ return 0;
}
transmitting = 1; // disable further reads
transmitting = 0;
buf_len = 0;
}
- return;
+ return 1;
}
packet_len -= 2; // strip the CRC
tx_ptr = 0;
ctl_pin_on();
UCSR0B |= _BV(UDRIE0);
+
+ return 1;
}
ISR(USART_RX_vect)
* All bugs by Jan "Yenya" Kasprzak <kas@fi.muni.cz> :-)
*/
+/*
+ * Call this at the beginning of the program.
+ */
void modbus_init(uint8_t unit_id);
-void modbus_poll();
+
+/*
+ * Call this frequently. Returns 0 when no data was received
+ * and the processing went on quickly. Returns 1 after potentially lengthy
+ * packet processing.
+ */
+uint8_t modbus_poll();
typedef enum {
MB_OK = 0,