Merged in paulpatience/nuttx-apps/serialrx (pull request #25)
examples/serialrx: Add configuration options to print the bytes received
This commit is contained in:
commit
c195b5415e
@ -4,7 +4,7 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
config EXAMPLES_SERIALRX
|
config EXAMPLES_SERIALRX
|
||||||
bool "Serial Rx example"
|
bool "Serial RX example"
|
||||||
default n
|
default n
|
||||||
---help---
|
---help---
|
||||||
Enable the serial RX example
|
Enable the serial RX example
|
||||||
@ -12,11 +12,50 @@ config EXAMPLES_SERIALRX
|
|||||||
if EXAMPLES_SERIALRX
|
if EXAMPLES_SERIALRX
|
||||||
|
|
||||||
config EXAMPLES_SERIALRX_STACKSIZE
|
config EXAMPLES_SERIALRX_STACKSIZE
|
||||||
int "Serial Rx stack size"
|
int "Serial RX stack size"
|
||||||
default 2048
|
default 2048
|
||||||
|
|
||||||
config EXAMPLES_SERIALRX_PRIORITY
|
config EXAMPLES_SERIALRX_PRIORITY
|
||||||
int "Serial Rx task priority"
|
int "Serial RX task priority"
|
||||||
default 50
|
default 50
|
||||||
|
|
||||||
|
config EXAMPLES_SERIALRX_BUFFERED
|
||||||
|
bool "Buffered I/O"
|
||||||
|
default y
|
||||||
|
---help---
|
||||||
|
Use buffered I/O
|
||||||
|
|
||||||
|
config EXAMPLES_SERIALRX_BUFSIZE
|
||||||
|
int "Buffer size"
|
||||||
|
default 11520
|
||||||
|
---help---
|
||||||
|
Specifies the default buffer size
|
||||||
|
|
||||||
|
config EXAMPLES_SERIALRX_DEVPATH
|
||||||
|
string "Serial device path"
|
||||||
|
default "/dev/ttyS0"
|
||||||
|
---help---
|
||||||
|
The default path to the serial device
|
||||||
|
|
||||||
|
choice
|
||||||
|
prompt "Output method"
|
||||||
|
default EXAMPLES_SERIALRX_PRINTHYPHEN
|
||||||
|
|
||||||
|
config EXAMPLES_SERIALRX_PRINTHYPHEN
|
||||||
|
bool "Hyphen"
|
||||||
|
---help---
|
||||||
|
Print a hyphen after receiving a full buffer
|
||||||
|
|
||||||
|
config EXAMPLES_SERIALRX_PRINTHEX
|
||||||
|
bool "Hexadecimal"
|
||||||
|
---help---
|
||||||
|
Print the bytes received in hexadecimal
|
||||||
|
|
||||||
|
config EXAMPLES_SERIALRX_PRINTSTR
|
||||||
|
bool "String"
|
||||||
|
---help---
|
||||||
|
Print the bytes received as a string
|
||||||
|
|
||||||
|
endchoice
|
||||||
|
|
||||||
endif
|
endif
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
* examples/serialrx/serialrx_main.c
|
* examples/serialrx/serialrx_main.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
|
* Copyright (C) 2015 Omni Hoverboards Inc. All rights reserved.
|
||||||
* Authors: Gregory Nutt <gnutt@nuttx.org>
|
* Authors: Gregory Nutt <gnutt@nuttx.org>
|
||||||
* Bob Doiron
|
* Bob Doiron
|
||||||
|
* Paul Alexander Patience <paul-a.patience@polymtl.ca>
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@ -40,27 +42,21 @@
|
|||||||
|
|
||||||
#include <nuttx/config.h>
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <nuttx/clock.h>
|
|
||||||
|
|
||||||
#include <nuttx/fs/fs.h>
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#define BUFFERED_IO
|
|
||||||
#define CHUNK 11520
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Data
|
* Private Data
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static int count = 0;
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Functions
|
* Public Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -75,27 +71,138 @@ int main(int argc, FAR char *argv[])
|
|||||||
int serialrx_main(int argc, char *argv[])
|
int serialrx_main(int argc, char *argv[])
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
FAR char *buf = (FAR char *)malloc(CHUNK);
|
#ifdef CONFIG_EXAMPLES_SERIALRX_BUFFERED
|
||||||
FAR FILE *f;
|
FAR FILE *f;
|
||||||
printf("Reading from %s\n", argv[1]);
|
|
||||||
f = fopen(argv[1], "r");
|
|
||||||
|
|
||||||
while(1)
|
|
||||||
{
|
|
||||||
#ifdef BUFFERED_IO
|
|
||||||
int ret = fread(buf, 1, CHUNK, f);
|
|
||||||
#else
|
#else
|
||||||
int ret = read(f->fs_fd, buf, CHUNK);
|
int fd;
|
||||||
#endif
|
#endif
|
||||||
count += ret;
|
#ifdef CONFIG_EXAMPLES_SERIALRX_PRINTHYPHEN
|
||||||
if (count >= CHUNK)
|
int count = 0;
|
||||||
|
#endif
|
||||||
|
#ifdef CONFIG_EXAMPLES_SERIALRX_PRINTHEX
|
||||||
|
int i;
|
||||||
|
#endif
|
||||||
|
bool eof = false;
|
||||||
|
FAR char *buf;
|
||||||
|
FAR char *devpath;
|
||||||
|
|
||||||
|
if (argc == 1)
|
||||||
|
{
|
||||||
|
devpath = CONFIG_EXAMPLES_SERIALRX_DEVPATH;
|
||||||
|
}
|
||||||
|
else if (argc == 2)
|
||||||
|
{
|
||||||
|
devpath = argv[1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: %s [devpath]\n", argv[0]);
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef CONFIG_EXAMPLES_SERIALRX_PRINTSTR
|
||||||
|
buf = (FAR char *)malloc(CONFIG_EXAMPLES_SERIALRX_BUFSIZE + 1);
|
||||||
|
#else
|
||||||
|
buf = (FAR char *)malloc(CONFIG_EXAMPLES_SERIALRX_BUFSIZE);
|
||||||
|
#endif
|
||||||
|
if (buf == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: malloc failed: %d\n", errno);
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef CONFIG_EXAMPLES_SERIALRX_BUFFERED
|
||||||
|
f = fopen(devpath, "r");
|
||||||
|
if (f == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: fopen failed: %d\n", errno);
|
||||||
|
goto errout_with_buf;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
fd = open(devpath, O_RDONLY);
|
||||||
|
if (fd < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: open failed: %d\n", errno);
|
||||||
|
goto errout_with_buf;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
printf("Reading from %s\n", devpath);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
while (!eof)
|
||||||
|
{
|
||||||
|
#ifdef CONFIG_EXAMPLES_SERIALRX_BUFFERED
|
||||||
|
size_t n = fread(buf, 1, CONFIG_EXAMPLES_SERIALRX_BUFSIZE, f);
|
||||||
|
if (n < CONFIG_EXAMPLES_SERIALRX_BUFSIZE)
|
||||||
{
|
{
|
||||||
printf("-");
|
if (feof(f))
|
||||||
|
{
|
||||||
|
eof = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("fread failed: %d\n", errno);
|
||||||
|
clearerr(f);
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
ssize_t n = read(fd, buf, CONFIG_EXAMPLES_SERIALRX_BUFSIZE);
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
eof = true;
|
||||||
|
}
|
||||||
|
else if (n < 0)
|
||||||
|
{
|
||||||
|
printf("read failed: %d\n", errno);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
count -= CHUNK;
|
}
|
||||||
|
#endif
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#if defined(CONFIG_EXAMPLES_SERIALRX_PRINTHYPHEN)
|
||||||
|
count += (int)n;
|
||||||
|
if (count >= CONFIG_EXAMPLES_SERIALRX_BUFSIZE)
|
||||||
|
{
|
||||||
|
printf("-");
|
||||||
|
fflush(stdout);
|
||||||
|
count -= CONFIG_EXAMPLES_SERIALRX_BUFSIZE;
|
||||||
|
}
|
||||||
|
#elif defined(CONFIG_EXAMPLES_SERIALRX_PRINTHEX)
|
||||||
|
printf("Received:\n");
|
||||||
|
for (i = 0; i < (int)n; i++)
|
||||||
|
{
|
||||||
|
printf("%d: 0x%02x\n", i, buf[i]);
|
||||||
|
}
|
||||||
|
fflush(stdout);
|
||||||
|
#elif defined(CONFIG_EXAMPLES_SERIALRX_PRINTSTR)
|
||||||
|
buf[n] = '\0';
|
||||||
|
printf("Received: %s", buf);
|
||||||
|
fflush(stdout);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef CONFIG_EXAMPLES_SERIALRX_PRINTHYPHEN
|
||||||
|
printf("\n");
|
||||||
|
#endif
|
||||||
|
printf("EOF reached\n");
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
#ifdef CONFIG_EXAMPLES_SERIALRX_BUFFERED
|
||||||
|
fclose(f);
|
||||||
|
#else
|
||||||
|
close(fd);
|
||||||
|
#endif
|
||||||
|
|
||||||
free(buf);
|
free(buf);
|
||||||
return 0;
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
|
errout_with_buf:
|
||||||
|
free(buf);
|
||||||
|
|
||||||
|
errout:
|
||||||
|
fflush(stderr);
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user