Alan Carvalho de Assis cdc5968c67 testing: Add Serial Error Reporting testing app
This commit adds support to Serial Error Reporting using the ioctl
TIOCGICOUNT.

This examples was inspired on this sample code:
https://stackoverflow.com/questions/78796301/c-serial-communication-why-does-read-lose-some-data-bytes-at-high-baud-rates/78800245#78800245

Signed-off-by: Alan C. Assis <acassis@gmail.com>
2025-03-24 21:52:26 +08:00

100 lines
2.7 KiB
C

/****************************************************************************
* apps/testing/drivers/ser/ser.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <nuttx/serial/serial.h>
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, FAR char *argv[])
{
struct serial_icounter_s rcount;
struct serial_icounter_s scount;
int fd;
int ret;
fd = open(CONFIG_TESTING_SERIAL_PORT, O_RDONLY);
if (fd < 0)
{
ret = -ENODEV;
goto out;
}
ret = ioctl(fd, TIOCGICOUNT, &scount);
if (ret < 0)
{
goto out_close;
}
while (1)
{
ret = ioctl(fd, TIOCGICOUNT, &rcount);
if (rcount.frame > scount.frame)
{
printf("ERROR: framing %d\n", rcount.frame - scount.frame);
}
if (rcount.overrun > scount.overrun)
{
printf("ERROR: overrun %d\n", rcount.overrun - scount.overrun);
}
if (rcount.parity > scount.parity)
{
printf("ERROR: parity %d\n", rcount.parity - scount.parity);
}
if (rcount.brk > scount.brk)
{
printf("ERROR: break %d\n", rcount.brk - scount.brk);
}
if (rcount.buf_overrun > scount.buf_overrun)
{
printf("ERROR: buffer overrun %d\n",
rcount.buf_overrun - scount.buf_overrun);
}
scount = rcount;
usleep(1000000);
}
out_close:
close(fd);
out:
return ret;
}