Skip to content

Build your first Web UI

Connect, measure, and display FID signals and an FFT spectrum in one HTML file. NMRLink handles communication and graph rendering.

If you have not installed the library, start with Installation.

Try it without hardware

Click Connect, then Measure. This preview uses a simulated device. It does not open a USB connection or send commands to hardware.

Preparing the preview…

FID / time-domain signal

Measure to display the graphs.

FFT / amplitude spectrum

Measure to display the graphs.

The blue and orange lines are the two FID channels; the lower graph shows the complex FFT amplitude spectrum. The shaded band marks the FFT analysis range.

Copy and connect to hardware

The HTML below is for actual hardware. Clicking Connect opens the browser’s serial port picker.

  1. Follow the static HTML installation steps to place nmrlink/ in your working directory.
  2. Save the following code as example.html in the same directory.
  3. Review and adjust parameters for your device and sample.
  4. Serve the directory with a web server, open it in a supported browser, and click Connect, then Measure.

The example uses 402000 Hz, a 21-cycle first pulse, a 42-cycle second pulse, and 128 scans. These values are not suitable for every device and sample.

html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Your first NMR measurement</title>
  <style>
    body { max-width: 800px; margin: 24px auto; padding: 0 16px; font: 14px/1.6 sans-serif; }
    button { padding: 8px 16px; font: inherit; }
    #fid, #fft { min-width: 0; }
  </style>
</head>
<body>
  <h1>Your first NMR measurement</h1>
  <p>Check the measurement settings for your device and sample.</p>
  <button id="connect">Connect</button>
  <button id="measure" disabled>Measure</button>
  <button id="disconnect" disabled>Disconnect</button>
  <p id="status" role="status">Disconnected</p>
  <h2>FID / time-domain signal</h2>
  <div id="fid"></div>
  <h2>FFT / amplitude spectrum</h2>
  <div id="fft"></div>

  <script type="module">
    import { NMRController, analyzeMeasurement } from "./nmrlink/index.js";
    import { NMRGraphs } from "./nmrlink/graphs.js";

    const connect = document.getElementById("connect");
    const measure = document.getElementById("measure");
    const disconnect = document.getElementById("disconnect");
    const status = document.getElementById("status");

    const controller = new NMRController();
    const graphs = new NMRGraphs({
      fid: document.getElementById("fid"),
      fft: document.getElementById("fft"),
      locale: "en",
    });

    const parameters = {
      frequencyHz: 402000,
      pulse1Cycles: 21, phase1Degrees: 0,
      pulse2Cycles: 42, phase2Degrees: 90,
      pulseIntervalMs: 5,
      measurementIntervalMs: 200,
      scans: 128,
    };

    controller.onStateChange(state => {
      connect.disabled = !state.canConnect;
      measure.disabled = !state.canMeasure;
      disconnect.disabled = !state.canDisconnect;
      if (state.busy) status.textContent = "Working…";
    });
    controller.onError(error => { status.textContent = error.message; });
    controller.onResult(record => {
      try {
        graphs.show(record, analyzeMeasurement(record));
        status.textContent = "Measurement complete";
      } catch (error) {
        graphs.clear();
        status.textContent = "Analysis error: " + error.message;
      }
    });

    connect.onclick = async () => {
      if ((await controller.connect()).ok) status.textContent = "Connected";
    };
    measure.onclick = () => controller.measure(parameters, { mode: "single" });
    disconnect.onclick = async () => {
      if ((await controller.disconnect()).ok) status.textContent = "Disconnected";
    };
  </script>
</body>
</html>

For an npm project, change the import paths from ./nmrlink/index.js to nmrlink and from ./nmrlink/graphs.js to nmrlink/graphs, then use your project’s bundler.

How the code works

  1. NMRController manages connection and measurement. Use onStateChange to enable or disable the available buttons.
  2. Call connect() directly from the button click, without awaiting unrelated work first.
  3. Pass the measurement settings to measure(parameters, { mode: "single" }).
  4. Receive the record in onResult, analyze it with analyzeMeasurement, and display it with graphs.show.
  5. Show communication and measurement errors with onError. Raw data remains in controller.result even if analysis fails.

Measurements are not retried automatically and cannot be stopped immediately. Wait for the measurement to finish. Disconnect is not a measurement-stop command.

For connection issues, see Supported browsers and connection troubleshooting.

Next: Control measurements →