1

I am currently trying to open a COM port with Deno. This works so far and I can read the data with file.read(buffer). The only problem now is that if there is no more data in the COM port and I try to read the data with file.read(buffer), Deno stops at that point.

How can I tell if there is no more data in the COM port?

This is my current class:

export class COM {
    constructor(COMPort : number) {
        this.COMPort = COMPort
    }
    
    async open() : Promise<void> {
        this.#file = await Deno.open(`\\\.\\COM${this.COMPort}`, {read: true, write: true})
        
    }

    close() : void {
        this.#file.close()
    }

    async #_readChar(readBuffer : Uint8Array) : Promise<Uint8Array> {
        
        await this.#file.read(readBuffer)
        

        return readBuffer
    }

    async readChar(chars : number) : Promise<Uint8Array> {
        return await this.#_readChar(new Uint8Array(chars))
    }
}

this is my main file:

const com = new COM(4)
await com.open()
while (true) {
    result = await com.readChar(1)
    console.log(result, decoder.decode(result))
    if (result == /* ??? empty ??? */) {
        break
    }
}
Mqx
  • 182
  • 7
  • If there is no termination byte sequence to read from the port, you can race a timeout promise against every read. If that idea works for you, then I can write it up as answer — if not, what did I miss? – jsejcksn Feb 09 '23 at 15:57
  • `await this.#file.read(readBuffer)` will return `null` when you've reached EOF – Patrick Roberts Feb 09 '23 at 17:00
  • @jsejcksn even if you use the `Promise.race()` it does not work. because the Promise from the `file.read()` nerver gets resolved. The program goes on but when it reaches the end it does not quit because of the unresolved promise. – Mqx Feb 09 '23 at 17:22
  • @PatrickRoberts when you read from the COM port you never get to the end and so you never get EOF. – Mqx Feb 09 '23 at 17:23
  • 1
    [^](https://stackoverflow.com/questions/75400521/deno-read-abort-if-com-port-has-no-data#comment133045556_75400521) @Mqx If your program has finished, then you can call [`Deno.exit()`](https://deno.land/api@v1.30.3?s=Deno.exit) to terminate. – jsejcksn Feb 09 '23 at 20:22
  • @jsejcksn Yea but thats not the best methode. Because the Promise can get resolved any time soon... – Mqx Feb 10 '23 at 09:38
  • [^](https://stackoverflow.com/questions/75400521/deno-read-abort-if-com-port-has-no-data#comment133057375_75400521) @Mqx If that's how you feel about it, then you won't be able to use the [`Deno.FsFile.read`](https://deno.land/api@v1.30.3?s=Deno.FsFile#method_read_0) method because it doesn't currently support [cancellation](https://github.com/denoland/deno/issues/10181)... so you'll have to read from its [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) (at [`Deno.FsFile.readable`](https://deno.land/api@v1.30.3?s=Deno.FsFile#prop_readable)). – jsejcksn Feb 10 '23 at 11:59
  • [^](https://stackoverflow.com/questions/75400521/deno-read-abort-if-com-port-has-no-data#comment133059854_75400521) If that concept works for you, then I can write an answer with an example showing how to race the stream controller queue against timeouts. If not, what am I still missing? – jsejcksn Feb 10 '23 at 12:02
  • @jsejcksn I think I am just gonna use Promise.race() and than quit the program. But thanks for the suggestions. – Mqx Feb 10 '23 at 21:48
  • Another option is to use [`iteratorReader`](https://deno.land/std@0.177.0/streams/iterate_reader.ts?s=iterateReader) to read the chunks as they come in, buffer them, and then `yield` them to the rest of your code after no new chunks have come in for a specified amount of time (e.g. 50ms). The `read` doesn't get aborted but the rest of your code can move on and if your done with the connection then closing it will end gracefully (avoids needing to call [`Deno.exit()`](https://deno.land/api@v1.30.3?s=Deno.exit)). – mfulton26 Feb 19 '23 at 19:52
  • @mfulton26 the `close()` methode does not abort the `read()` thats the problem... – Mqx Feb 23 '23 at 15:00

0 Answers0