diff --git a/common/modules/mixly-modules/web/burn-upload.js b/common/modules/mixly-modules/web/burn-upload.js index 74dc0d36..1b68670f 100644 --- a/common/modules/mixly-modules/web/burn-upload.js +++ b/common/modules/mixly-modules/web/burn-upload.js @@ -669,12 +669,6 @@ BU.uploadWithAmpy = (portName) => { const mainWorkspace = Workspace.getMain(); const editor = mainWorkspace.getEditorsManager().getActive(); const port = Serial.getPort(portName); - let useBuffer = true, dataLength = 256; - if (port.constructor.name === 'USBDevice') { - dataLength = 64; - } else if (port.constructor.name === 'HIDDevice') { - dataLength = 31; - } const layerNum = layer.open({ type: 1, title: Msg.Lang['shell.uploading'] + '...', @@ -685,7 +679,7 @@ BU.uploadWithAmpy = (portName) => { success: async function (layero, index) { $('#mixly-loader-btn').hide(); const serial = new Serial(portName); - const ampy = new Ampy(serial, useBuffer, dataLength); + const ampy = new Ampy(serial); const code = editor.getCode(); let closePromise = Promise.resolve(); if (statusBarSerial) { diff --git a/common/modules/mixly-modules/web/hid.js b/common/modules/mixly-modules/web/hid.js index b07896df..b97bfced 100644 --- a/common/modules/mixly-modules/web/hid.js +++ b/common/modules/mixly-modules/web/hid.js @@ -103,6 +103,7 @@ class WebHID extends Serial { #reader_ = null; #writer_ = null; #stringTemp_ = ''; + #dataLength_ = 31; constructor(port) { super(port); } @@ -133,7 +134,7 @@ class WebHID extends Serial { const portsName = Serial.getCurrentPortsName(); const currentPortName = this.getPortName(); if (!portsName.includes(currentPortName)) { - reject('无可用设备'); + reject('no device available'); return; } if (this.isOpened()) { @@ -176,20 +177,20 @@ class WebHID extends Serial { } async sendBuffer(buffer) { - return new Promise((resolve, reject) => { - if (buffer instanceof Uint8Array) { - let temp = new Uint8Array(buffer.length + 1); - temp[0] = buffer.length; - temp.set(buffer, 1); - buffer = temp; - } else { - buffer.unshift(buffer.length); - buffer = new Uint8Array(buffer); - } - this.#device_.sendReport(0, buffer) - .then(resolve) - .catch(reject); - }); + if (buffer.constructor.name !== 'Uint8Array') { + buffer = new Uint8Array(buffer); + } + const len = Math.ceil(buffer.length / this.#dataLength_); + for (let i = 0; i < len; i++) { + const start = i * this.#dataLength_; + const end = Math.min((i + 1) * this.#dataLength_, buffer.length); + const writeBuffer = buffer.slice(start, end); + let temp = new Uint8Array(end - start + 1); + temp[0] = writeBuffer.length; + temp.set(writeBuffer, 1); + await this.#device_.sendReport(0, temp); + await this.sleep(10); + } } async setDTRAndRTS(dtr, rts) { diff --git a/common/modules/mixly-modules/web/serialport.js b/common/modules/mixly-modules/web/serialport.js index 5bdf373c..c8c0fcb2 100644 --- a/common/modules/mixly-modules/web/serialport.js +++ b/common/modules/mixly-modules/web/serialport.js @@ -132,7 +132,7 @@ class WebSerialPort extends Serial { const portsName = Serial.getCurrentPortsName(); const currentPortName = this.getPortName(); if (!portsName.includes(currentPortName)) { - reject('无可用串口'); + reject('no device available'); return; } if (this.isOpened()) { @@ -200,22 +200,18 @@ class WebSerialPort extends Serial { } async sendBuffer(buffer) { - return new Promise((resolve, reject) => { - const { writable } = this.#serialport_; - const writer = writable.getWriter(); - if (!(buffer instanceof Uint8Array)) { - buffer = new Uint8Array(buffer); - } - writer.write(buffer) - .then(() => { - writer.releaseLock(); - resolve(); - }) - .catch(() => { - writer.releaseLock(); - reject(); - }); - }); + const { writable } = this.#serialport_; + const writer = writable.getWriter(); + if (buffer.constructor.name !== 'Uint8Array') { + buffer = new Uint8Array(buffer); + } + try { + await writer.write(buffer); + writer.releaseLock(); + } catch (error) { + writer.releaseLock(); + throw Error(error); + } } async setDTRAndRTS(dtr, rts) { diff --git a/common/modules/mixly-modules/web/usb-mini.js b/common/modules/mixly-modules/web/usb-mini.js index 2df0c679..429c1a60 100644 --- a/common/modules/mixly-modules/web/usb-mini.js +++ b/common/modules/mixly-modules/web/usb-mini.js @@ -109,6 +109,7 @@ class USBMini extends Serial { #endpointIn_ = null; #endpointOut_ = null; #interfaceNumber_ = 0; + #dataLength_ = 64; constructor(port) { super(port); } @@ -178,7 +179,7 @@ class USBMini extends Serial { const portsName = Serial.getCurrentPortsName(); const currentPortName = this.getPortName(); if (!portsName.includes(currentPortName)) { - throw new Error('无可用串口'); + throw new Error('no device available'); } if (this.isOpened()) { return; @@ -237,11 +238,17 @@ class USBMini extends Serial { } async sendBuffer(buffer) { - if (typeof buffer.unshift === 'function') { - // buffer.unshift(buffer.length); - buffer = new Uint8Array(buffer).buffer; + if (buffer.constructor.name !== 'Uint8Array') { + buffer = new Uint8Array(buffer); + } + const len = Math.ceil(buffer.length / this.#dataLength_); + for (let i = 0; i < len; i++) { + const start = i * this.#dataLength_; + const end = Math.min((i + 1) * this.#dataLength_, buffer.length); + const writeBuffer = buffer.slice(start, end); + await this.#write_(writeBuffer) + await this.sleep(5); } - return this.#write_(buffer); } async setDTRAndRTS(dtr, rts) { diff --git a/common/modules/mixly-modules/web/usb.js b/common/modules/mixly-modules/web/usb.js index 5bc13e0b..85f0faef 100644 --- a/common/modules/mixly-modules/web/usb.js +++ b/common/modules/mixly-modules/web/usb.js @@ -145,7 +145,7 @@ class USB extends Serial { const portsName = Serial.getCurrentPortsName(); const currentPortName = this.getPortName(); if (!portsName.includes(currentPortName)) { - throw new Error('无可用串口'); + throw new Error('no device available'); } if (this.isOpened()) { return; @@ -192,11 +192,11 @@ class USB extends Serial { } async sendBuffer(buffer) { - if (typeof buffer.unshift === 'function') { + if (buffer.constructor.name !== 'Uint8Array') { buffer.unshift(buffer.length); - buffer = new Uint8Array(buffer).buffer; + buffer = new Uint8Array(buffer); } - return this.#dapLink_.send(132, buffer); + await this.#dapLink_.send(132, buffer); } async setDTRAndRTS(dtr, rts) {