Hi Robert,
Thank you for your reply. Considering maximum number of registers to be written simultaneously are 50 (Modbus pdu length for writing this data 109 bytes), at 9600 baud, comes out to be 102 ms. I put 150 ms in the pdMS_TO_TICKS() as shown below and tried again. But I could not write even 25 holding registers at a time.
The modified function
Code: Select all
STATIC mp_obj_t machine_uart_waittxdone(mp_obj_t self_in) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
uart_wait_tx_done(self->uart_num, pdMS_TO_TICKS(150));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_waittxdone_obj, machine_uart_waittxdone);
My modbus micropython relevant function is as below (from uModbus library):
Code: Select all
def _send(self, modbus_pdu, slave_addr):
serial_pdu = bytearray()
serial_pdu.append(slave_addr)
serial_pdu.extend(modbus_pdu)
crc = self._calculate_crc16(serial_pdu)
serial_pdu.extend(crc)
print('serial pdu: ',serial_pdu)
if self._ctrlPin:
self._ctrlPin(1)
self._uart.write(serial_pdu)
time.sleep(0.1)
if self._ctrlPin:
self._uart.waittxdone()
# while not self._uart.wait_tx_done():
# machine.idle()
time.sleep_us(self._t35chars)
self._ctrlPin(0)
When I connect the modbus slave to esp (esp as master). I get this communication on the slave side: (writing on 25 holding registers)
Code: Select all
Rx:000000-01 10 00 00 00 19 32 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00
Rx:000001-14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 D5 31
Rx:000002-01 10 00 00 00 19 32 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00
Rx:000003-14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 D5 31
Rx:000004-01 10 00 00 00 19 32 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00
Rx:000005-14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 00 14 D5 31
I get success in writing multiple registers up to 14 registers. When I increase the
time.sleep(0.1) sleep time, then I am able to write more registers. However, I need to make the wait_tx_done function work, so that I don't need to worry about this sleep time.
What am I missing to get this function work properly, so that it only returns when all the bytes are written as desired before ctrlPin is pulled low?
Thank you.