Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binding.read reads only 32 bytes #2272

Open
germancmartinez opened this issue Jun 18, 2021 · 5 comments
Open

binding.read reads only 32 bytes #2272

germancmartinez opened this issue Jun 18, 2021 · 5 comments
Labels
docs Documentation

Comments

@germancmartinez
Copy link

Summary of Problem

When I try to read a large number of bytes (514 in this case) the function ends and returns only 32 bytes.

Code

...
var bytesRead;
var InBuf = Buffer.allocUnsafe(600); 
var portOptions = { baudRate: 9600, dataBits: 8, parity: "even", stopBits: 1, autoOpen: true };  
const binding = new Binding(portOptions);
await binding.open("COM3",portOptions);
...
await binding.write(Buffer.from('1006','hex'));   //OK
bytesRead = await binding.read(InBuf,0,514);  //NOT OK-->I get only 32 bytes

I'm using a Serial Port Monitor where I can see:
image

Versions, Operating System and Hardware

  • SerialPort v9.1.0
  • Node.js v14.17.0
  • Operating system and version: Windows Server 2012 R2
  • Architecture: x64
@reconbot
Copy link
Member

reconbot commented Jun 20, 2021

Hey, sorry the docs are a little light on the bindings. But it will return a minimum of 1 byte and a maximum of specified bytes. You have keep reading until you get the number of bytes you want, read only stays pending while there isn't any data to read. The tradeoff here was for performance, but the usability is a bit annoying I know.

@germancmartinez
Copy link
Author

Hi Francis, thank you! Using a while is working ok.

@GazHank GazHank added the docs Documentation label Jul 5, 2021
@myfreax
Copy link

myfreax commented Mar 5, 2024

@germancmartinez How done fix this issue?

@germancmartinez
Copy link
Author

germancmartinez commented Mar 6, 2024

var portOptions = { baudRate: 9600, dataBits: 8, parity: "even", stopBits: 1, autoOpen: true };   
const binding = new Binding(portOptions);

async function mySerialRead(buff, offset, bytes){
    var bytesRead=0;
    var aux;
    while(bytesRead<bytes) `{`
        aux = await binding.read(buff,offset+bytesRead,bytes-bytesRead);
        bytesRead += aux.bytesRead;
    }
    return bytesRead;
}

@myfreax
Copy link

myfreax commented Mar 17, 2024

var portOptions = { baudRate: 9600, dataBits: 8, parity: "even", stopBits: 1, autoOpen: true };   
const binding = new Binding(portOptions);

async function mySerialRead(buff, offset, bytes){
    var bytesRead=0;
    var aux;
    while(bytesRead<bytes) `{`
        aux = await binding.read(buff,offset+bytesRead,bytes-bytesRead);
        bytesRead += aux.bytesRead;
    }
    return bytesRead;
}

thank your work. I have solved the problem using parser-delimiter package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation
Development

No branches or pull requests

4 participants