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

Only getting 1 byte in readAsync #5

Open
fishcharlie opened this issue Aug 26, 2024 · 5 comments
Open

Only getting 1 byte in readAsync #5

fishcharlie opened this issue Aug 26, 2024 · 5 comments

Comments

@fishcharlie
Copy link

The following code only prints 1 to the console a bunch of times.

import * as rtljs from "rtljs";
import * as fs from "fs";

const DEFAULT_RATE = 3_000_000;
const DEFAULT_FREQ = 1090 * rtljs.mhz; // 1090 MHz
const ASYNC_BUF_NUMBER = 12;
const DATA_LEN = 16 * 16384; // 256k

const device = rtljs.open(0);

device.setTunerGainMode(1);
device.setTunerGain(50);
device.setAGCMode(0);
device.setCenterFreq(DEFAULT_FREQ);
device.setSampleRate(DEFAULT_RATE);
device.resetBuffer();

fs.writeFileSync("data.bin", Buffer.alloc(0));
device.readAsync((data) => {
	console.log(data.length);
	fs.appendFileSync("data.bin", Buffer.from(data));
}, ASYNC_BUF_NUMBER, DATA_LEN);

However, the following code prints 262144 to the console a bunch of times.

import * as rtljs from "rtljs";
import * as fs from "fs";

const DEFAULT_RATE = 3_000_000;
const DEFAULT_FREQ = 1090 * rtljs.mhz; // 1090 MHz
const ASYNC_BUF_NUMBER = 12;
const DATA_LEN = 16 * 16384; // 256k

const device = rtljs.open(0);

device.setTunerGainMode(1);
device.setTunerGain(50);
device.setAGCMode(0);
device.setCenterFreq(DEFAULT_FREQ);
device.setSampleRate(DEFAULT_RATE);
device.resetBuffer();

fs.writeFileSync("data.bin", Buffer.alloc(0));

while (true) {
	const data = device.readSync(DATA_LEN);
	fs.appendFileSync("data.bin", Buffer.from(data));
	console.log(data.length);
}

Why would readSync give me the data length I want, whereas readAsync does not?

@Arkanic
Copy link
Owner

Arkanic commented Sep 6, 2024

Sorry mate - readAsync is really broken at the moment, and I sort of entirely forgot about it
ffi-napi, which is what the library uses to interact with librtlsdr, really doesn't like the way that librtlsdr uses a callback to a c function for readAsync to work, and I haven't figured out a way to get it working yet.

I might take a look at it sometime soon but from past attempts and my minimal experience I haven't had the best of luck, for now it might be best to readSync repeatedly either in a seperate thread on the nodejs side, or just in small enough sizes that it doesn't interfere with the main thread.

Sorry about the hassle, I can understand the frustration but I simply don't have the answers right now.

Best regards

@fishcharlie
Copy link
Author

@Arkanic Got it. Just concerned that I'll miss data from the device if I do the sync version. But maybe that isn't as big of a concern.

@Arkanic
Copy link
Owner

Arkanic commented Sep 8, 2024

That is actually a valid concern I haven't thought about, again i'll take a look at some point but for now it remains unresolved.

@Arkanic
Copy link
Owner

Arkanic commented Sep 11, 2024

update: ffi-napi seems to have been abandoned entirely, last release to npm was 3 years ago, and it doesn't build on modern versions of node anymore - which was the whole point of this library
might be time for a rewrite, i'll go hunting for a good way to interface and potentially remake

@Arkanic
Copy link
Owner

Arkanic commented Sep 21, 2024

Just reworked the entire backend with a new library, it works on the latest version of node now
Everything is working except readasync/cancelasync - which i know is what you wanted, but i've had some trouble with the readAsync function blocking the entire nodejs process regardless of whether or not readAsync is on a seperate thread or not :(
I'll play around a bit more but i'm not too sure of which angle to tackle it from at the moment, just know that the code as a whole is just a little bit closer to actually doing the job it is supposed to do....

cheers

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

No branches or pull requests

2 participants