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
FFT / amplitude spectrum
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.
- Follow the static HTML installation steps to place
nmrlink/in your working directory. - Save the following code as
example.htmlin the same directory. - Review and adjust
parametersfor your device and sample. - 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.
<!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
NMRControllermanages connection and measurement. UseonStateChangeto enable or disable the available buttons.- Call
connect()directly from the button click, without awaiting unrelated work first. - Pass the measurement settings to
measure(parameters, { mode: "single" }). - Receive the record in
onResult, analyze it withanalyzeMeasurement, and display it withgraphs.show. - Show communication and measurement errors with
onError. Raw data remains incontroller.resulteven 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.