Example Code

Retrieve time from NTP or W32Time server

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Operating System

  • NI Linux Real-Time
  • Windows

    Programming Language

  • LabVIEW G

Code and Documents

Attachment

Description

This simple example shows to read the time directly from an NTP server (or a W32Time server)

The most basic possible client request is sent over UDP to port 123 on the server which should respond with the time. This can be useful, for example to keep an RT target in sync with other devices on the network. 

NTP is not all that accurate. Sometimes though, events need to be timed according the server, but that no code can be added to the server to provide the trigger.

This code allows you to request the time to keep track of the local clock offset. I wouldn’t try altering the system clock though, just subtract the measured offset as this is sufficient.

The local ports can be whatever you want but port 123 is always used by the server.

How to Use

This was written in LabVIEW 2025 but saved back to 2020 for compatibility. It will work on a Linux RT targets or on Windows.

"Basic Time Request.vi" is a top-level example, calling the "Query NTP Server.vi" action engine. This has just three actions, opening the local port, querying servers and closing the local port. You can call different servers on the port once opened.

Point the code at the name or IP address of the server and run the example.

If you need to find the W32Time server used by your local machine, try issuing the command ”w32tm /query /source” in a command prompt with admin rights. This should give you its name.

For an NTP server, you may find that the gateway on your LAN has NTP enabled. If not, then use an external server such as NIST. Be aware that NTP servers are not designed to be queried very often and you will be blocked if you query too frequently.

 

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.