Skip to content

Commit 456d03b

Browse files
committed
Added support for tauriserial
1 parent f11154a commit 456d03b

File tree

9 files changed

+193
-42
lines changed

9 files changed

+193
-42
lines changed

eslint.config.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,26 @@ export default defineConfig([
4747
},
4848
],
4949
},
50-
ignores: ["dist/", "*.json", "*.html", "*.less", "*.css", "package.json"],
50+
ignores: [
51+
"dist/",
52+
"src/dist/",
53+
"**/dist/**",
54+
"**/src/dist/**",
55+
"*.json",
56+
"*.html",
57+
"*.less",
58+
"*.css",
59+
"package.json",
60+
],
5161
},
5262
{
5363
files: ["**/*.vue"],
5464
languageOptions: {
5565
parser: vueParser,
5666
},
67+
plugins: {
68+
vue: vuePlugin,
69+
},
5770
processor: "vue/vue",
5871
},
5972
]);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"@capacitor/core": "^7.0.1",
7171
"@fortawesome/fontawesome-free": "^6.5.2",
7272
"@tauri-apps/api": "^2.5.0",
73-
"@tauri-apps/cli": "^2.5.0",
73+
"@tauri-apps/cli": "^2.5.0",
7474
"@vitejs/plugin-vue": "^6.0.1",
7575
"crypto-es": "^2.1.0",
7676
"d3": "^7.9.0",

src-tauri/Cargo.lock

Lines changed: 95 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ tauri-build = { version = "2.5", features = [] }
1818
[dependencies]
1919
tauri = { version = "2.5", features = [] }
2020
tauri-plugin-shell = "2.3"
21+
tauri-plugin-serialplugin = "2.11"
2122
serde = { version = "1.0", features = ["derive"] }
2223
serde_json = "1.0"
2324

src-tauri/capabilities/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"windows": ["*"],
66
"permissions": [
77
"core:default",
8-
"shell:allow-open"
8+
"shell:allow-open",
9+
"serialplugin:default"
910
]
1011
}

src-tauri/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
fn mobile_entry() {
44
tauri::Builder::default()
55
.plugin(tauri_plugin_shell::init())
6+
.plugin(tauri_plugin_serialplugin::init())
67
.run(tauri::generate_context!())
78
.expect("error while running tauri application");
89
}

src-tauri/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
fn main() {
77
tauri::Builder::default()
88
.plugin(tauri_plugin_shell::init())
9+
.plugin(tauri_plugin_serialplugin::init())
910
.run(tauri::generate_context!())
1011
.expect("error while running tauri application");
1112
}

src/js/port_handler.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EventBus } from "../components/eventBus";
33
import { serial } from "./serial.js";
44
import WEBUSBDFU from "./protocols/webusbdfu";
55
import { reactive } from "vue";
6+
import { isTauri } from "@tauri-apps/api/core";
67
import {
78
checkBrowserCompatibility,
89
checkWebBluetoothSupport,
@@ -37,7 +38,8 @@ const PortHandler = new (function () {
3738
checkBrowserCompatibility();
3839

3940
this.showBluetoothOption = checkWebBluetoothSupport();
40-
this.showSerialOption = checkWebSerialSupport();
41+
// In Tauri, native serial is available via plugin even if Web Serial API isn't.
42+
this.showSerialOption = checkWebSerialSupport() || isTauri();
4143
this.showUsbOption = checkWebUSBSupport();
4244

4345
console.log(`${this.logHead} Bluetooth available: ${this.showBluetoothOption}`);
@@ -47,23 +49,30 @@ const PortHandler = new (function () {
4749
this.showVirtualMode = getConfig("showVirtualMode", false).showVirtualMode;
4850
this.showManualMode = getConfig("showManualMode", false).showManualMode;
4951
this.showAllSerialDevices = getConfig("showAllSerialDevices", false).showAllSerialDevices;
52+
// Decide which serial protocol to prefer at runtime
53+
this.serialProtocol = isTauri() ? "tauriserial" : "webserial";
54+
console.log(`${this.logHead} ### Using serial protocol: ${this.serialProtocol}`);
5055
})();
5156

5257
PortHandler.initialize = function () {
5358
EventBus.$on("ports-input:request-permission-bluetooth", () => this.requestDevicePermission("webbluetooth"));
54-
EventBus.$on("ports-input:request-permission-serial", () => this.requestDevicePermission("webserial"));
59+
EventBus.$on("ports-input:request-permission-serial", () => this.requestDevicePermission(this.serialProtocol));
5560
EventBus.$on("ports-input:request-permission-usb", () => this.requestDevicePermission("usb"));
5661
EventBus.$on("ports-input:change", this.onChangeSelectedPort.bind(this));
5762

5863
// Use serial for all protocol events
5964
serial.addEventListener("addedDevice", (event) => {
6065
const detail = event.detail;
66+
const proto = (detail?.protocolType || "").toLowerCase();
6167

62-
if (detail?.path?.startsWith("bluetooth")) {
68+
if (detail?.path?.startsWith("bluetooth") || proto === "webbluetooth") {
6369
this.handleDeviceAdded(detail, "webbluetooth");
70+
} else if (proto === "tauriserial") {
71+
this.handleDeviceAdded(detail, "tauriserial");
6472
} else {
6573
this.handleDeviceAdded(detail, "webserial");
6674
}
75+
console.log(`${this.logHead} #### Device addition event received:`, event.detail, proto);
6776
});
6877

6978
serial.addEventListener("removedDevice", (event) => {
@@ -81,7 +90,7 @@ PortHandler.initialize = function () {
8190
PortHandler.refreshAllDeviceLists = async function () {
8291
// Update all device lists in parallel
8392
return Promise.all([
84-
this.updateDeviceList("webserial"),
93+
this.updateDeviceList(this.serialProtocol),
8594
this.updateDeviceList("webbluetooth"),
8695
this.updateDeviceList("usb"),
8796
]).then(() => {
@@ -108,11 +117,12 @@ PortHandler.removedSerialDevice = function (device) {
108117

109118
// Get device path safely
110119
const devicePath = device?.path || (typeof device === "string" ? device : null);
120+
const proto = (device?.protocolType || "").toLowerCase();
111121

112122
if (!devicePath) {
113123
console.warn(`${this.logHead} Device removal event missing path information`, device);
114124
// Still update ports, but don't try to use the undefined path
115-
this.updateDeviceList("webserial").then(() => {
125+
this.updateDeviceList(this.serialProtocol).then(() => {
116126
this.selectActivePort();
117127
});
118128
return;
@@ -121,7 +131,7 @@ PortHandler.removedSerialDevice = function (device) {
121131
// Update the appropriate ports list based on the device type
122132
const updatePromise = devicePath.startsWith("bluetooth")
123133
? this.updateDeviceList("webbluetooth")
124-
: this.updateDeviceList("webserial");
134+
: this.updateDeviceList(proto === "tauriserial" ? "tauriserial" : "webserial");
125135

126136
const wasSelectedPort = this.portPicker.selectedPort === devicePath;
127137

@@ -274,7 +284,9 @@ PortHandler.handleDeviceAdded = function (device, deviceType) {
274284

275285
// Update the appropriate device list
276286
const updatePromise =
277-
deviceType === "webbluetooth" ? this.updateDeviceList("webbluetooth") : this.updateDeviceList("webserial");
287+
deviceType === "webbluetooth"
288+
? this.updateDeviceList("webbluetooth")
289+
: this.updateDeviceList(deviceType === "tauriserial" ? "tauriserial" : "webserial");
278290

279291
updatePromise.then(() => {
280292
const selectedPort = this.selectActivePort(device);
@@ -310,6 +322,11 @@ PortHandler.updateDeviceList = async function (deviceType) {
310322
ports = await serial.getDevices("webserial");
311323
}
312324
break;
325+
case "tauriserial":
326+
if (this.showSerialOption) {
327+
ports = await serial.getDevices("tauriserial");
328+
}
329+
break;
313330
default:
314331
console.warn(`${this.logHead} Unknown device type: ${deviceType}`);
315332
return [];
@@ -331,6 +348,7 @@ PortHandler.updateDeviceList = async function (deviceType) {
331348
console.log(`${this.logHead} Found DFU port(s)`, orderedPorts);
332349
break;
333350
case "webserial":
351+
case "tauriserial":
334352
this.portAvailable = orderedPorts.length > 0;
335353
this.currentSerialPorts = [...orderedPorts];
336354
console.log(`${this.logHead} Found serial port(s)`, orderedPorts);

0 commit comments

Comments
 (0)