Problem combining data from two Serial ports

Hi I am using two external devices connected to two COM ports.
The data coming to COM 1 is at 50Hz rate. The data coming to COM2 is at 10Hz rate
I am parsing the data from COM1 and I have:

msg.payload = {"timestamp":1234, "P1":123} //This is at 50Hz

from COM2 I have:

msg.payload = {"P2":456}// This is at 10Hz

I need to include P2 into the COM1 data when there is new value. So I save it value to
flow.set("P2",p2)

then into COM1 data I am doing this:

let p2 = flow.get("P2");
let dataFrom1 = {
{"timestamp":1234, "P1":123, "P2":p2}
} 
flow.set("P2",'')

msg.payload = dataFrom1

as the data from COM2 is at 10Hz and the data from COM1 is at 50Hz, I must have p2 value on every 10-th COM1 data. Unfortunately this is not consistent. Vary between 6th and 12th. Does anyone have idea why the data is not updated at every 10th data vale from COM1?

Thanks in advance!

Hard to say for sure but I suspect that you are hitting the same problem as in another recent thread. You are relying on exact timing in node-red and that does not work because not only is node-red not a realtime system neither is its underlying OS.

Therefore, you will never get exact timings on any input data. If you need realtime processing, you need to do it on a realtime system not a general purpose system.

Thank you very much for your answer. But if the data comes with 10Hz stable, isn't it be able to update a variable at same frequency?

Not necessarily. Even a 1Hz signal might not be exactly processed as 1Hz. This is not the fault of Node-RED, it is simply that:

  • Linux has many other processes running and some may throw off the timing
  • Node-RED is a node.js app and node.js is mostly single threaded so any number of processes hitting the thread can throw off the exact timing.

If you want exact ms or better timing, use a microprocessor with sufficient power and with a real-time OS such as the ESP32.

Thank you very much! I will try with ESP32

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