mirror of
https://github.com/siemens-mobile-hacks/node-sie-serial.git
synced 2026-11-25 17:17:17 +00:00
Optimize AsyncSerialPort
This commit is contained in:
parent
2d318bf811
commit
a73eb786a5
3 changed files with 15 additions and 16 deletions
|
|
@ -58,6 +58,12 @@ export class AsyncSerialPort<T extends BindingInterface = BindingInterface> {
|
|||
}
|
||||
|
||||
async read(size: number, timeout?: number): Promise<Buffer | undefined> {
|
||||
if (!this.port.isOpen)
|
||||
throw new Error("Port is not open");
|
||||
|
||||
if (this.port.readableLength >= size)
|
||||
return this.port.read(size);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let result: Buffer | undefined;
|
||||
let bytesRead = 0;
|
||||
|
|
@ -107,8 +113,10 @@ export class AsyncSerialPort<T extends BindingInterface = BindingInterface> {
|
|||
}
|
||||
};
|
||||
|
||||
if (!this.port.isOpen)
|
||||
reject(new Error("Port is not open"));
|
||||
if (this.port.readableLength > 0)
|
||||
onReadable();
|
||||
if (bytesRead == size)
|
||||
return;
|
||||
|
||||
this.port.on("close", onClose);
|
||||
this.port.on("end", onEnd);
|
||||
|
|
@ -121,18 +129,9 @@ export class AsyncSerialPort<T extends BindingInterface = BindingInterface> {
|
|||
}
|
||||
|
||||
async write(data: any): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.port.isOpen)
|
||||
reject(new Error("Port is not open"));
|
||||
this.port.write(data);
|
||||
this.port.drain((err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
if (!this.port.isOpen)
|
||||
throw new Error("Port is not open");
|
||||
this.port.write(data);
|
||||
}
|
||||
|
||||
async getSignals(): Promise<void> {
|
||||
|
|
|
|||
|
|
@ -247,7 +247,7 @@ export class DWD extends BaseSerialProtocol {
|
|||
return ioWriteMemory({
|
||||
debug,
|
||||
align: 4,
|
||||
pageSize: 128,
|
||||
pageSize: MAX_MEMORY_WRITE_CHUNK,
|
||||
maxRetries: 3,
|
||||
write: this.writeMemoryChunk.bind(this)
|
||||
}, address, buffer, options);
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ export function ioProgressTracker(options: IoProgressOptions) {
|
|||
lastSpeedCheck = Date.now();
|
||||
}
|
||||
|
||||
if (!options.progressInterval || (Date.now() - lastProgressCalled > options.progressInterval && cursor > 0)) {
|
||||
if (!options.progressInterval || (Date.now() - lastProgressCalled >= options.progressInterval && cursor > 0)) {
|
||||
reportProgressDirect();
|
||||
progressCalled = true;
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue