Add line response feature

Add a simple line response feature to make it possible to send e.g. a
command string to your serial device and easily receive and parse a line
response.

This is a convenience feature for simple request/response interaction
based on lines. For more advanced interaction the socket feature should
be used instead.

The line response feature is detailed via the following options:

 -r, --response-wait

Wait for line response then quit. A line is considered any string ending
with either CR or NL character. If no line is received tio will quit
after response timeout.

Any tio text is automatically muted when piping a string to tio while in
response mode to make it easy to parse the response.

 --response-timeout <ms>

Set timeout [ms] of line response (default: 100).

Example:

Sending a string (SCPI command) to a test instrument (Korad PSU) and
print line response:

 $ echo "*IDN?" | tio /dev/ttyACM0 --response-wait
 KORAD KD3305P V4.2 SN:32477045
This commit is contained in:
Martin Lund 2022-08-13 00:04:00 +02:00
parent a75e04b883
commit e837fd0303
9 changed files with 143 additions and 7 deletions

View file

@ -1015,6 +1015,9 @@ int tty_connect(void)
int status;
bool next_timestamp = false;
char* now = NULL;
struct timeval tv;
struct timeval *tv_p = &tv;
bool ignore_stdin = false;
/* Open tty device */
fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK);
@ -1108,12 +1111,28 @@ int tty_connect(void)
{
FD_ZERO(&rdfs);
FD_SET(fd, &rdfs);
FD_SET(STDIN_FILENO, &rdfs);
if (!ignore_stdin)
{
FD_SET(STDIN_FILENO, &rdfs);
}
maxfd = MAX(fd, STDIN_FILENO);
maxfd = MAX(maxfd, socket_add_fds(&rdfs, true));
/* Manage timeout */
if ((option.response_wait) && (option.response_timeout != 0))
{
// Set response timeout
tv_p->tv_sec = 0;
tv_p->tv_usec = option.response_timeout * 1000;
}
else
{
// No timeout
tv_p = NULL;
}
/* Block until input becomes available */
status = select(maxfd + 1, &rdfs, NULL, NULL, NULL);
status = select(maxfd + 1, &rdfs, NULL, NULL, tv_p);
if (status > 0)
{
bool forward = false;
@ -1181,6 +1200,15 @@ int tty_connect(void)
{
next_timestamp = true;
}
if (option.response_wait)
{
if ((input_char == '\r') || (input_char == '\n'))
{
tty_sync(fd);
exit(EXIT_SUCCESS);
}
}
}
}
else if (FD_ISSET(STDIN_FILENO, &rdfs))
@ -1195,8 +1223,21 @@ int tty_connect(void)
else if (bytes_read == 0)
{
/* Reached EOF (when piping to stdin) */
tty_sync(fd);
exit(EXIT_SUCCESS);
if (option.response_wait)
{
/* Stdin pipe closed but not blocking so stop listening
* to stdin in response mode.
*
* Note: select() really indicates not if data is ready
* but if file descriptor is non-blocking for I/O
* operation. */
ignore_stdin = true;
}
else
{
tty_sync(fd);
exit(EXIT_SUCCESS);
}
}
/* Process input byte by byte */
@ -1257,6 +1298,11 @@ int tty_connect(void)
tio_error_printf("select() failed (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
else
{
// Timeout (only happens in response wait mode)
exit(EXIT_FAILURE);
}
}
return TIO_SUCCESS;