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

Read async implementation #37

Open
Xman176 opened this issue Jan 30, 2017 · 0 comments
Open

Read async implementation #37

Xman176 opened this issue Jan 30, 2017 · 0 comments

Comments

@Xman176
Copy link

Xman176 commented Jan 30, 2017

Hello,
I´m trying to create asynchronous read data from rtl-sdr. I use sample rate 2Mhz, but this case what I push doesn´t work (Throw Exception), I would like to read data without lost samples.
I whould like to ask for some help or write example with comments. I saw at rtl_adbc.c where I wound this type of solution.

`static pthread_cond_t ready;
static pthread_mutex_t ready_m;

//Shared Global Memory
uint8_t mem1[100000], mem2[50000];

//Lock for read and write
pthread_mutex_t mem1Lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mem2Lock = PTHREAD_MUTEX_INITIALIZER;

BOOL end;

static void* saveData()
{
FILE *output;
output = fopen("D:\black\zkouska.dat", "w");

while (!end) {
	//pthread_mutex_lock(&mem1Lock);
	pthread_cond_wait(&ready, &ready_m);

	for (int i = 0; i < 100000; i += 2)
		fprintf(output, "%d\n%d\n", mem1[i], mem1[i+1]);

	/*pthread_mutex_unlock(&mem1Lock);


	pthread_mutex_lock(&mem2Lock);

	for (int i = 0; i < 50000; i += 2)
		fprintf(output, "%d\n%d\n", mem2[i], mem2[i + 1]);

	pthread_mutex_unlock(&mem2Lock);*/
}

fclose(output);
return NULL;

}

void* readData(rtlsdr_dev_t *dev)
{
int read;

while (!end) {
	pthread_mutex_lock(&mem1Lock);
	rtlsdr_read_sync(dev, mem1, 50000, &read);
	pthread_mutex_unlock(&mem1Lock);


	pthread_mutex_lock(&mem2Lock);
	rtlsdr_read_sync(dev, mem2, 50000, &read);
	pthread_mutex_unlock(&mem2Lock);
}
return NULL;

}

static void getData(unsigned char *buf, uint32_t len, void *ctx)
{
//pthread_mutex_lock(&mem1Lock);
memcpy(mem1, buf, len);
pthread_cond_signal(&ready);
/*pthread_mutex_unlock(&mem1Lock);

pthread_mutex_lock(&mem2Lock);
memcpy(mem2, buf, len);
pthread_mutex_unlock(&mem2Lock);*/

}

void test(rtlsdr_dev_t *dev, double time)
{
pthread_t threadRead, threadWrite;
int rc; end = FALSE;
pthread_cond_init(&ready, NULL);
pthread_mutex_init(&ready_m, NULL);

/*rc = pthread_create(&threadRead, NULL, readData, dev);
if (rc) {
	printf("ERROR; return code from pthread_create() is %d\n", rc);
	exit(-1);
}*/

rtlsdr_reset_buffer(dev);

rc = pthread_create(&threadWrite, NULL, saveData, (void *) NULL);
if (rc) {
	printf("ERROR; return code from pthread_create() is %d\n", rc);
	exit(-1);
}
rtlsdr_read_async(dev, getData, (void *)NULL, 12, 100000);


Sleep(time);

end = TRUE;
rtlsdr_cancel_async(dev);
//pthread_join(threadRead, NULL);
//pthread_join(threadWrite, NULL);

}
`

Thanks for any help

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

1 participant