Closed skylin008 closed 2 years ago
I'm really not in a position to do a full analysis of this substantial chunk of code. I would use one of three approaches.
As a general comment your approach looks good. One thing that did stand out is
while self._delay.running():
await asyncio.sleep_ms(self._timeout) # Wait for 5ms after last msg received
A delay of 3ms (according to my reading of the code) is unlikely actually to happen in uasyncio
because of competition from other tasks. In any event it's an inefficient way to wait on a timer. Try:
await self._delay.wait()
Internally this waits on an Event. During the waiting period there is no load on the scheduler.
By the same token main() can be simplified:
async def main():
await write()
I don't think either of these tweaks will fix the issue, but I would review any instances of short sleeps in the light of possible competition from other tasks.
Good luck.
Thank you peterhinch to shared this impressive project. I want to modified auart_hd to fit the modbus bus driver. I found used the async mode, the function response so slow.As the code follow. How to solve the slow issue. Thanks! `
The Serial's send_command() method sends a command and waits for a number of
lines from the device. The end of the process is signified by a timeout, when
a list of lines is returned. This allows line-by-line processing.
A special test mode demonstrates the behaviour with a non-responding device. If
None is passed, no commend is sent. The master waits for a response which never
arrives and returns an empty list.
import logging from machine import UART from machine import Pin import uasyncio as asyncio import aswitch
import functions as functions import const as Const import struct
logging.level_name(level = logging.INFO) log = logging.Logger('modbus_driver')
class Serial(): def init(self, uart_no, ctrl_pin = None, stime = 3, debug = False): self.uart = UART(uart_no, 19200) if ctrl_pin is not None: self._ctrlPin = Pin(ctrl_pin, mode = Pin.OUT) # Send data , ctrl_pin must be High else: self._ctrlPin = None self._timeout = stime self._swriter = asyncio.StreamWriter(self.uart, {}) self._sreader = asyncio.StreamReader(self.uart) self._delay = aswitch.Delay_ms() self._response = [] self._debug = debug
if name == 'main':
test()
`