Serial. Ws | TRENDING |
The Web Serial API allows web applications to communicate with serial devices (e.g., Arduino, RFID readers) directly from a browser, without intermediate native apps. No standard serial.ws domain exists — the term likely refers to a WebSocket-to-serial bridge.
Classrooms teaching Arduino or Micro:bit often struggle with driver installation. A serial.ws local server eliminates the need for the IDE. Students write JavaScript in a browser to blink LEDs or read sensors, while the background bridge handles the serial heavy lifting.
To understand the power of serial.ws, you must understand the two core technologies it fuses: the Web Serial API and WebSockets.
Never expose a raw serial.ws endpoint to the public internet without a token system. Use JSON Web Tokens (JWT) in the WebSocket handshake:
wss.on('connection', (ws, req) =>
const token = new URL(req.url, 'ws://localhost').searchParams.get('token');
if (!isValidToken(token))
ws.close(1008, 'Unauthorized');
return;
// proceed...
);
WebSockets: WebSockets provide a way to establish a persistent, low-latency, full-duplex communication channel between a client (usually a web browser) and a server over the web. This allows for real-time communication, enabling efficient, bidirectional data transfer.
Serial Communication: Serial communication refers to the process of transmitting data one bit at a time over a communication channel or computer bus. It's commonly used for computer-to-computer, computer-to-device, or device-to-device communication. serial. ws
const WebSocket = require('ws'); const SerialPort = require('serialport');// Configure your serial port (change path and baudRate as needed) const port = new SerialPort( path: '/dev/ttyUSB0', // Windows: 'COM3' baudRate: 9600 );
const wss = new WebSocket.Server( port: 8080 );
wss.on('connection', (ws) => console.log('WebSocket client connected to serial.ws');
// Forward serial data -> WebSocket clients port.on('data', (data) => ws.send(data.toString('utf8')); );
// Forward WebSocket messages -> Serial device ws.on('message', (message) => port.write(message.toString() + '\r\n'); ); The Web Serial API allows web applications to
ws.on('close', () => console.log('Client disconnected'); ); );
console.log('serial.ws bridge running on ws://localhost:8080');
<!DOCTYPE html> <html> <head> <title>serial.ws Client</title> </head> <body> <textarea id="output" rows="10" cols="50"></textarea> <input type="text" id="command" placeholder="Send command..."> <button onclick="sendCommand()">Send</button><script> const socket = new WebSocket('ws://localhost:8080'); const output = document.getElementById('output'); socket.onmessage = (event) => output.value += event.data + '\n'; output.scrollTop = output.scrollHeight; ; function sendCommand() const cmd = document.getElementById('command').value; socket.send(cmd); </script>
</body> </html>
Run node bridge.js and open index.html. You now have a working serial.ws system.
Here are some code examples to get you started:
Arduino Example (C++)
// Arduino example code
void setup()
Serial.begin(9600);
void loop()
if (Serial.available() > 0)
char data = Serial.read();
Serial.print(data);
Python Example using PySerial
import serial
# Open serial port
ser = serial.Serial('COM3', 9600)
# Send data
ser.write(b'Hello, serial device!')
# Receive data
data = ser.readline()
print(data.decode())
