Hi, I receive data from the serial port at 50Hz frequency each message. I need to add timestamp to each message, so I do as follow:
msg.timestamp = Date.now()
So the timestamps of the messages needs to be at 20ms each other (because 50Hz frequency), but in the debug console I am receiving inconsistent timestamps as well as equal timestamps that overlaps etc...
Is there a way how to achieve consistent timestamps at 20ms distance each other?
Can you show us an example of what you are seeing?
Note though, that nodejs is not a realtime system. Occasionally it has to do stuff like memory garbage collection, so to get reliable 20ms sampling you would need a significantly powerful processor. You would probably be better doing the real time stuff in something like an Arduino or other mcu and sending the timestamped data to node-red via mqtt
Hi, thanks for your answer. I am using raspberry pi with node-red. I need to read the serial data from external device, so I am using NR. I just need to add a timestamp to each packet from serial. The packets comes at 50Hz. I will make a screenshot of the debug window
I will have a look shortly. What is the speed node? Is it a contrib node, if so then which? Just in case it is corrupting messages and overlaying data from one onto the next one, disconnect it from the function node and connect the debug node direct to the function node to see what you are getting there. The fact that you see identical timestamps suggests that the message may be getting overwritten.
Thank you very much for your answer. Unfortunately the rate is not absolutely stable it's 48-52 Hz, randomly in this range. May be to measure the rate each second an update the variable that I will add to each timestamp?
A stable rate can be ensured with an external hardware buffer that feeds data to the RPI's serial port. If the hardware buffer detects that the serial port is not ready, then the buffer stores the incoming data. When the serial port is ready, then the buffer resumes sending data to the port.
For high speed data rate, the serial in node speed won't be stable no matter how powerful the processor is (because the OS is not real-time). An external hardware buffer is necessary to keep the rate stable.
RPI has a small UART receive hardware buffer, and a larger software buffer provided by the driver. When the buffer is full, then data will be lost.
Sometimes, hardware UART handshaking is used to ensure no data loss (lower the speed automatically when the receiver can not handle the incoming data). But you will have jitter (varying data rate) in this case.