Skip to content

Commit 1cecebc

Browse files
Fix AT32 Configuration can't be saved on MacOS (#4455)
* Fix AT32 Configuration can't be saved on MacOS * Update src/js/protocols/WebSerial.js Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Change the location of the macOS judgment * Adding logging for batch write mode detection * Refactoring for MacOS detection * fix GUI is not exported * Change this.logHead to logHead --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 6696b93 commit 1cecebc

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/js/protocols/WebSerial.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { webSerialDevices, vendorIdNames } from "./devices";
22
import { checkBrowserCompatibility } from "../utils/checkBrowserCompatibilty";
3+
import GUI from "../gui";
34

45
const logHead = "[SERIAL]";
56

@@ -69,7 +70,7 @@ class WebSerial extends EventTarget {
6970
navigator.serial.addEventListener("connect", (e) => this.handleNewDevice(e.target));
7071
navigator.serial.addEventListener("disconnect", (e) => this.handleRemovedDevice(e.target));
7172
}
72-
73+
this.isNeedBatchWrite = false;
7374
this.loadDevices();
7475
}
7576

@@ -181,6 +182,10 @@ class WebSerial extends EventTarget {
181182

182183
const connectionInfo = this.port.getInfo();
183184
this.connectionInfo = connectionInfo;
185+
this.isNeedBatchWrite = this.checkIsNeedBatchWrite();
186+
if (this.isNeedBatchWrite) {
187+
console.log(`${logHead} Enabling batch write mode for AT32 on macOS`);
188+
}
184189
this.writer = this.port.writable.getWriter();
185190
this.reader = this.port.readable.getReader();
186191

@@ -328,6 +333,29 @@ class WebSerial extends EventTarget {
328333
}
329334
}
330335

336+
checkIsNeedBatchWrite() {
337+
const isMac = GUI.operating_system === "MacOS";
338+
return isMac && vendorIdNames[this.connectionInfo.usbVendorId] === "AT32";
339+
}
340+
341+
async batchWrite(data) {
342+
// AT32 on macOS requires smaller chunks (63 bytes) to work correctly due to
343+
// USB buffer size limitations in the macOS implementation
344+
const batchWriteSize = 63;
345+
let remainingData = data;
346+
while (remainingData.byteLength > batchWriteSize) {
347+
const sliceData = remainingData.slice(0, batchWriteSize);
348+
remainingData = remainingData.slice(batchWriteSize);
349+
try {
350+
await this.writer.write(sliceData);
351+
} catch (error) {
352+
console.error(`${logHead} Error writing batch chunk:`, error);
353+
throw error; // Re-throw to be caught by the send method
354+
}
355+
}
356+
await this.writer.write(remainingData);
357+
}
358+
331359
async send(data, callback) {
332360
if (!this.connected || !this.writer) {
333361
console.error(`${logHead} Failed to send data, serial port not open`);
@@ -338,7 +366,11 @@ class WebSerial extends EventTarget {
338366
}
339367

340368
try {
341-
await this.writer.write(data);
369+
if (this.isNeedBatchWrite) {
370+
await this.batchWrite(data);
371+
} else {
372+
await this.writer.write(data);
373+
}
342374
this.bytesSent += data.byteLength;
343375

344376
const result = { bytesSent: data.byteLength };

src/js/utils/checkBrowserCompatibilty.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function getOS() {
66
let os = "unknown";
77
const userAgent = window.navigator.userAgent;
88
const platform = window.navigator?.userAgentData?.platform || window.navigator.platform;
9-
const macosPlatforms = ["Macintosh", "MacIntel", "MacPPC", "Mac68K", "MacOS"];
9+
const macosPlatforms = ["Macintosh", "MacIntel", "MacPPC", "Mac68K", "macOS"];
1010
const windowsPlatforms = ["Win32", "Win64", "Windows", "WinCE"];
1111
const iosPlatforms = ["iPhone", "iPad", "iPod"];
1212

0 commit comments

Comments
 (0)