Dynamically change characters expected in serial request

Hi, currently you can split input on timeout, silence, certain character, or fixed number of characters. Is it possible to have it return on a variable number of characters by passing the number in with the input message?

I briefly looked at 25-serial.js and it appears that you would just need to change the size of the response buffer on input. Not fully cognizant everything that's going on in there though.

A use case of this would be for serial protocols such as Modbus RTU, where the response size is variable and you want to return ASAP when you get the right number of characters back. Interbyte timeout doesn't always work because some Modbus devices out there are really slow/cruddy and may pause before responding fully.

Related to this -- is it also possible to flush the input buffer before sending a new request? This may already be the behavior -- not sure -- in Python I have to discard what was sitting in the input buffer though if I've received a message of the wrong size before sending out another request.

P.S. I know there are a few nodes out there for Modbus RTU already, but I'd rather write my own minimalist code for Modbus. Modbus is so simple that the request/response code for both serial and TCP (with error-checking) in Python is only about a page -- there's no reason to have (some) overly complex nodes with many dependencies.


Did you consider configuring the serial node to split at a fixed length of 1 character and then coding some logic to compare the number of characters received against your target?

That may work, not sure. I'd have to think about that.

In any case, I would think that this behavior is still desired to match a lot of other serial implementations -- there's usually a read function that reads a given number of characters, either returning that number, some number less (what was received) if timed out, or nothing if timed out. The number of characters to read is an input, not fixed.

is this use case mainly about the serial-request node ?
I.E. could the number of characters required be part of the command/query ?

Yes, exactly. I was envisioning giving the input msg a "chars_requested" property or something like that to go along with the payload.

So... opinions please... two thoughts.

  1. If there was a msg.property that specified what character/byte (e.g. $ or \n or 0x02 ) to wait for before starting to handle output - what would be a sensible name. msg.waitfor ?

  2. If there was also a property that could override the number of characters then sent, (if the node is configured to send a number of characters)... what should that be called ? msg.count ?

other suggestions - thoughts for and against please.

1 Like
  1. My thought would be to follow the language in the node config, i.e. something like msg.splitinputonchar or msg.splitonchar, with or without camel case.

  2. msg.count seems fine, or msg.splitoncount

Thrilled that you're looking into this, thank you.

1 - this would be a new property... the split is still available for use to detect the end of the sequence... (You occasionally see old serial things send 0x02 ..... 0x03 (Start of text - End of text)... so needs a new name :slight_smile:

@dceejay I see you added msg.count to the serial request node -- thank you!