Skip to content

Commit

Permalink
Send the data using serial writer
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianscheit committed Feb 6, 2025
1 parent 4f4a3af commit 2d278b2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
8 changes: 4 additions & 4 deletions pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
<meta name="author" content="" />
<style>
.error {
color: #d00;
background-color: #ffd;
color: #b00;
background-color: #fee;
}

.send {
color: #00d;
background-color: #efe;
color: #00b;
background-color: #eef;
}

table {
Expand Down
22 changes: 18 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,23 @@ document.querySelector('form')!.addEventListener('submit', (event) => {
start(formData, formData.modbusmode === 'ASCII' ? new AsciiModeStrategy() : new RtuModeStrategy());
});

const start = (serialOptions: SerialOptions, dataReceiver: ModeStrategy) => {
let send: ((bytest: number[]) => void) | undefined = undefined;

const start = (serialOptions: SerialOptions, mode: ModeStrategy) => {
clearError();
serial.requestPort().then((serialPort: SerialPort) => {
console.log('serialPort', serialPort);
serialPort.open(serialOptions).then(async () => {
setSerialFieldsetDisable(true);
const writer = serialPort.writable?.getWriter();
if (writer) {
send = async (bytes: number[]) => {
const rawFrame = mode.send(bytes);
await writer.write(rawFrame);
}
} else {
console.error('Port is not writable!');
}

while (serialPort.readable) {
const reader: ReadableStreamDefaultReader<Uint8Array> = serialPort.readable.getReader();
Expand All @@ -53,14 +64,16 @@ const start = (serialOptions: SerialOptions, dataReceiver: ModeStrategy) => {
if (done) {
break;
}
dataReceiver.receive(value);
mode.receive(value);
}
} catch (error) {
reportError(error);
} finally {
reader.releaseLock();
}
}
writer?.releaseLock();
send = undefined;
await serialPort.close();
setSerialFieldsetDisable(false);
}, reportError);
Expand Down Expand Up @@ -88,8 +101,9 @@ document.querySelector('form[name=send]')!.addEventListener('submit', event => {
}
const frameBytes: number[] = [formData.slaveAddress, formData.functionCode, ...bytes];
insertFrameRow(new Frame([...frameBytes], 'send'));
new AsciiModeStrategy().send(frameBytes);
new RtuModeStrategy().send(frameBytes);
if (send) {
send(frameBytes);
}
});

// intTest();
4 changes: 2 additions & 2 deletions src/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class RtuModeStrategy extends ModeStrategy {
const firstCrcByte = rtuCrc.crc & 0xff;
rtuCrc.updateCrc(firstCrcByte);
const result = new Uint8Array([...bytes, firstCrcByte, rtuCrc.crc]);
this.receive(result);
//this.receive(result);
return result;
}
}
Expand Down Expand Up @@ -130,7 +130,7 @@ export class AsciiModeStrategy extends ModeStrategy {
const line = `:${Converters.bytesAsHex(bytes)}${Converters.byteToHex(-lrc & 0xff)}\r\n`;
console.log(bytes, line);
const result = Converters.textAsUInt8Array(line);
this.receive(result);
//this.receive(result);
return result;
}
}

0 comments on commit 2d278b2

Please sign in to comment.