- the return value is usable for determining whether a potentially
lengthy packet processing went on.
{
bufptr_t packet_len;
uint16_t crc;
{
bufptr_t packet_len;
uint16_t crc;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
if (transmitting)
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
if (transmitting)
if (buf_len == 0) // nothing received yet
if (buf_len == 0) // nothing received yet
if (get_clock() - last_rx < REQ_TIMEOUT) // still receiving
if (get_clock() - last_rx < REQ_TIMEOUT) // still receiving
if (buf_len < 4) { // too short
buf_len = 0;
if (buf_len < 4) { // too short
buf_len = 0;
}
if (buffer[0] != mb_unit_id) { // not for myself
buf_len = 0;
}
if (buffer[0] != mb_unit_id) { // not for myself
buf_len = 0;
}
transmitting = 1; // disable further reads
}
transmitting = 1; // disable further reads
transmitting = 0;
buf_len = 0;
}
transmitting = 0;
buf_len = 0;
}
}
packet_len -= 2; // strip the CRC
}
packet_len -= 2; // strip the CRC
tx_ptr = 0;
ctl_pin_on();
UCSR0B |= _BV(UDRIE0);
tx_ptr = 0;
ctl_pin_on();
UCSR0B |= _BV(UDRIE0);
* All bugs by Jan "Yenya" Kasprzak <kas@fi.muni.cz> :-)
*/
* 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_init(uint8_t unit_id);
+
+/*
+ * 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,
typedef enum {
MB_OK = 0,