Yes. The usual design is a small local C# agent that owns the serial port and exposes a local HTTP endpoint such as http://127.0.0.1:8080/GetWt?format=json. Your third party web page calls that endpoint with JavaScript. A browser cannot directly access a COM port unless the page uses a browser-specific device API and the user grants permission.
A WinForms application can host this agent, but it must remain running. For unattended use, a Windows Service is usually a better host.
using System.IO.Ports;
using System.Net;
using System.Text;
using System.Text.Json;
var port = new SerialPort("COM3", 9600) { ReadTimeout = 2000 };
port.Open();
var listener = new HttpListener();
listener.Prefixes.Add("http://127.0.0.1:8080/");
listener.Start();
while (true)
{
var context = await listener.GetContextAsync();
var origin = context.Request.Headers["Origin"];
if (origin != "https://your-third-party-site.example")
{
context.Response.StatusCode = 403;
context.Response.Close();
continue;
}
context.Response.Headers.Add("Access-Control-Allow-Origin", origin);
if (context.Request.HttpMethod == "OPTIONS")
{
context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, OPTIONS");
context.Response.Close();
continue;
}
// Replace this with the command and response format from the scale vendor.
string weight = port.ReadLine().Trim();
var json = JsonSerializer.Serialize(new { weight });
byte[] bytes = Encoding.UTF8.GetBytes(json);
context.Response.ContentType = "application/json";
context.Response.ContentLength64 = bytes.Length;
await context.Response.OutputStream.WriteAsync(bytes);
context.Response.Close();
}
Do not use Access-Control-Allow-Origin: *. Bind only to 127.0.0.1, allow only the known web origin, and serialize access to the serial port if multiple requests are possible. The scale command, baud rate, parity, terminator, and whether it continuously transmits data must come from the device documentation.
Sangeetha SRank #212 · 11.4K reputationPosted Nov 18, 2026, 6:04 AM EST
The WinForms application:
Receives the HTTP request.
Reads weight from COM port.
Returns JSON response.
Browser displays the weight.
allow only your web application:
context.Response.Headers.Add(
"Access-Control-Allow-Origin",
"https://yourwebsite.com");