From 667313401d072bb7966893fa460875085207d2b7 Mon Sep 17 00:00:00 2001 From: Christian Schnell Date: Thu, 30 Oct 2025 18:35:05 +0100 Subject: [PATCH] Fix UTF-8 encoding in SerialAdapterXtermJS (#1439) * avoid possibly creating unused SerialAdapter instance * use UTF-8 encoding when exchanging strings between XTerm and serial port Use TextEncoder.encode() instead of String.charCodeAt() in Terminal.onData(data_str) to encode user input strings (keyboard or paste). --- src/browser/serial.js | 11 ++++++----- src/browser/starter.js | 11 +++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/browser/serial.js b/src/browser/serial.js index 0f049b5b..f0371b91 100644 --- a/src/browser/serial.js +++ b/src/browser/serial.js @@ -234,16 +234,17 @@ export function SerialAdapterXtermJS(element, bus) }); term.write("This is the serial console. Whatever you type or paste here will be sent to COM1"); - const on_data_disposable = term["onData"](function(data) { - for(let i = 0; i < data.length; i++) + const utf8_encoder = new TextEncoder(); + const on_data_disposable = term["onData"](function(data_str) { + for(const utf8_byte of utf8_encoder.encode(data_str)) { - bus.send("serial0-input", data.charCodeAt(i)); + bus.send("serial0-input", utf8_byte); } }); - bus.register("serial0-output-byte", function(byte) + bus.register("serial0-output-byte", function(utf8_byte) { - term.write(Uint8Array.of(byte)); + term.write(Uint8Array.of(utf8_byte)); }, this); this.destroy = function() { diff --git a/src/browser/starter.js b/src/browser/starter.js index 8aec8fad..87b66859 100644 --- a/src/browser/starter.js +++ b/src/browser/starter.js @@ -289,16 +289,15 @@ V86.prototype.continue_init = async function(emulator, options) settings.screen = this.screen_adapter; settings.screen_options = screen_options; - if(options.serial_container) - { - this.serial_adapter = new SerialAdapter(options.serial_container, this.bus); - //this.recording_adapter = new SerialRecordingAdapter(this.bus); - } - if(options.serial_container_xtermjs) { this.serial_adapter = new SerialAdapterXtermJS(options.serial_container_xtermjs, this.bus); } + else if(options.serial_container) + { + this.serial_adapter = new SerialAdapter(options.serial_container, this.bus); + //this.recording_adapter = new SerialRecordingAdapter(this.bus); + } if(!options.disable_speaker) {