How to flush serial request node

I have a function that sends a file over the serial port. I parse the file into messages sent via the "serial request" node. Is there a way to flush the buffered messages so I don't have to wait for each one to timeout in case of an issue?

Not sure what you mean. Anything you send is sent as soon as possible so there should be nothing to flush.

I am confused, are you sending the file from node red via serial, or reading it into node red via serial?

I am transmitting from Node-Red. I believe the "serial request" node will transmit a message and wait for either a response or a timeout until it sends the next message. The file I am sending creates about 5K messages. If the remote stops responding, then I have to wait for all the remaining messages to timeout or restart Node-Red. Restarting node-red takes a long time to get back to the right state I need.

Show us how you have configured the serial port config node. That is where you setup the baud rate etc. In particular have you considered what you want the timeout setting to be?

Screenshot 2022-07-25 100544

Send the request message in msg.payload as you would do with a serial out node. The message will be forwarded to the serial port following a strict FIFO (First In, First Out) queue, waiting for a single response before transmitting the next request. Once a response is received (with the same logic of a serial in node), or after a timeout occurs, a message is sent to the output (see Outputs below), with msg.payload containing the received response (or missing in case if timeout) and all other properties are preserved.

As you can read above, messages are queued up and sent only after a response or timeout. I made the timeout as short as possible.

OK, now I understand the question. You want to flush messages that are waiting in the queue to be sent.

The simple flow below could be used to allow only one message into the request node at a time. Put the request node where the 5 second process is in the flow and when a good reply is received then trigger the single flush to send the next message, as in the attached flow. Catch the timeout error and if that is seen then trigger a flush with a very large number so that effectively all remaining messages will be flushed.

[{"id":"b6630ded2db7d680","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":2620,"wires":[["ed63ee4225312b40"]]},{"id":"3a9faf0a95b4a9bb","type":"function","z":"bdd7be38.d3b55","name":"Flush","func":"return {flush: 1}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":190,"y":2700,"wires":[["ed63ee4225312b40"]]},{"id":"ed63ee4225312b40","type":"delay","z":"bdd7be38.d3b55","name":"Queue","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":310,"y":2620,"wires":[["a82c03c3d34f683c","d4d479e614e82a49"]]},{"id":"d4d479e614e82a49","type":"debug","z":"bdd7be38.d3b55","name":"IN","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":470,"y":2540,"wires":[]},{"id":"a82c03c3d34f683c","type":"delay","z":"bdd7be38.d3b55","name":"Process taking 5 seconds","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":530,"y":2620,"wires":[["7c6253e5d34769ac","b23cea1074943d4d"]]},{"id":"b23cea1074943d4d","type":"debug","z":"bdd7be38.d3b55","name":"OUT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":2540,"wires":[]},{"id":"7c6253e5d34769ac","type":"link out","z":"bdd7be38.d3b55","name":"link out 1","mode":"link","links":["2128a855234c1016"],"x":665,"y":2700,"wires":[]}]
1 Like

I think I end up in the same place. It's the serial send node that is queueing. If I put a queue before it, then flush that queue then the messages will just end up queue in the serial node. Flush may not be the right term. What I really want is to delete the queued messages.

Thank you for your support.

I used node-red-contrib-queue-gate and the below example flow as a starting point to help with queuing messages:

[{"id":"cbf5e9f1.23a9e","type":"tab","label":"Gates","disabled":false,"info":""},{"id":"a46ee04e.b1a9b8","type":"inject","z":"cbf5e9f1.23a9e","name":"open","props":[{"p":"payload","v":"OPEN","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"OPEN","payloadType":"str","x":630,"y":180,"wires":[["c54e919c.18d7f"]]},{"id":"c54e919c.18d7f","type":"q-gate","z":"cbf5e9f1.23a9e","name":"q-gate demo","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"peek","dropCmd":"drop","statusCmd":"status","maxQueueLength":"0","keepNewest":false,"qToggle":false,"persist":false,"x":810,"y":400,"wires":[["d2740316.7035c","eb0a12e9.c810a8"]]},{"id":"c04db5ad.da76b8","type":"inject","z":"cbf5e9f1.23a9e","name":"input","props":[{"p":"payload","v":"","vt":"date"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":550,"y":400,"wires":[["c54e919c.18d7f"]]},{"id":"287fae2e.cb245a","type":"inject","z":"cbf5e9f1.23a9e","name":"toggle","props":[{"p":"payload","v":"toggle","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"toggle","payloadType":"str","x":630,"y":260,"wires":[["c54e919c.18d7f"]]},{"id":"8fd8943b.e60c28","type":"inject","z":"cbf5e9f1.23a9e","name":"close","props":[{"p":"payload","v":"close","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"close","payloadType":"str","x":630,"y":220,"wires":[["c54e919c.18d7f"]]},{"id":"44541499.4ac23c","type":"inject","z":"cbf5e9f1.23a9e","name":"default","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"default","payloadType":"str","x":630,"y":300,"wires":[["c54e919c.18d7f"]]},{"id":"92b01a9d.669a58","type":"inject","z":"cbf5e9f1.23a9e","name":"queue","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"queue","payloadType":"str","x":630,"y":460,"wires":[["c54e919c.18d7f"]]},{"id":"5f8708d.abc59f8","type":"inject","z":"cbf5e9f1.23a9e","name":"flush","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"flush","payloadType":"str","x":630,"y":540,"wires":[["c54e919c.18d7f"]]},{"id":"fb8c7d0f.40eec","type":"inject","z":"cbf5e9f1.23a9e","name":"trigger","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"trigger","payloadType":"str","x":630,"y":500,"wires":[["c54e919c.18d7f"]]},{"id":"b7688480.6338","type":"inject","z":"cbf5e9f1.23a9e","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"reset","payloadType":"str","x":630,"y":580,"wires":[["c54e919c.18d7f"]]},{"id":"44127021.988758","type":"inject","z":"cbf5e9f1.23a9e","name":"peek","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"peek","payloadType":"str","x":630,"y":620,"wires":[["c54e919c.18d7f"]]},{"id":"cc39c2df.44d0b8","type":"inject","z":"cbf5e9f1.23a9e","name":"drop","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"drop","payloadType":"str","x":630,"y":660,"wires":[["c54e919c.18d7f"]]},{"id":"a94f3db7.55ae18","type":"inject","z":"cbf5e9f1.23a9e","name":"status","props":[{"p":"payload","v":"status","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"status","payloadType":"str","x":630,"y":340,"wires":[["c54e919c.18d7f"]]},{"id":"d2740316.7035c","type":"debug","z":"cbf5e9f1.23a9e","name":"output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":970,"y":400,"wires":[]},{"id":"ae120d0f.ade318","type":"status","z":"cbf5e9f1.23a9e","name":"q-gate status","scope":null,"x":810,"y":460,"wires":[["ea4d90f9.61a5b"]]},{"id":"ea4d90f9.61a5b","type":"debug","z":"cbf5e9f1.23a9e","name":"status text","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"status.text","targetType":"msg","x":970,"y":460,"wires":[]},{"id":"17f0fca4.70eb6b","type":"comment","z":"cbf5e9f1.23a9e","name":"Drop first in queue","info":"","x":310,"y":660,"wires":[]},{"id":"c8b7ddbb.0656b8","type":"comment","z":"cbf5e9f1.23a9e","name":"Show first in queue","info":"","x":310,"y":620,"wires":[]},{"id":"4776d0d4.2052f","type":"comment","z":"cbf5e9f1.23a9e","name":"Clear all in queue","info":"","x":300,"y":580,"wires":[]},{"id":"3db44a17.fbaf3e","type":"comment","z":"cbf5e9f1.23a9e","name":"Deliver all in queue","info":"","x":310,"y":540,"wires":[]},{"id":"5085f497.e29b0c","type":"comment","z":"cbf5e9f1.23a9e","name":"Deliver first in queue","info":"","x":310,"y":500,"wires":[]},{"id":"e8db383c.27c12","type":"comment","z":"cbf5e9f1.23a9e","name":"Show # of queued","info":"","x":310,"y":460,"wires":[]},{"id":"9c2b192f.8b0dc8","type":"comment","z":"cbf5e9f1.23a9e","name":"Timestamp","info":"","x":280,"y":400,"wires":[]},{"id":"2a954899.db6dd8","type":"comment","z":"cbf5e9f1.23a9e","name":"Deliver all in queue and passthrough","info":"","x":360,"y":180,"wires":[]},{"id":"4b6a30f3.55b928","type":"comment","z":"cbf5e9f1.23a9e","name":"Clear all in queue and block","info":"","x":340,"y":220,"wires":[]},{"id":"6cdc3f34.01cf4","type":"comment","z":"cbf5e9f1.23a9e","name":"Open-Close or Open-Queuing","info":"","x":340,"y":260,"wires":[]},{"id":"6e967344.fde55c","type":"comment","z":"cbf5e9f1.23a9e","name":"Set node to default state set in properties","info":"","x":380,"y":300,"wires":[]},{"id":"e2888b7d.ffeb88","type":"comment","z":"cbf5e9f1.23a9e","name":"Show node status","info":"","x":310,"y":340,"wires":[]},{"id":"be0c3959.840948","type":"comment","z":"cbf5e9f1.23a9e","name":"msg.topic = control","info":"","x":310,"y":100,"wires":[]},{"id":"c900db0f.e6c2f","type":"comment","z":"cbf5e9f1.23a9e","name":"msg.payload = \"the command\"","info":"","x":560,"y":100,"wires":[]},{"id":"eb0a12e9.c810a8","type":"debug","z":"cbf5e9f1.23a9e","name":"output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":970,"y":360,"wires":[]}]

Give it a test run, I think you can resolve the issue with it.

Yes, sorry, when you get an error you need to send back msg.reset to clear out any queued messages in the delay node.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.