Skip to content

Instantly share code, notes, and snippets.

@Scott31393
Forked from amarburg/rs485_ioctl_test.c
Last active December 12, 2023 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Scott31393/7673659c13c40e3a7e9f36fc0cb29c79 to your computer and use it in GitHub Desktop.
Save Scott31393/7673659c13c40e3a7e9f36fc0cb29c79 to your computer and use it in GitHub Desktop.
A quick test function to demonstrate the Linux serial / RS485 ioctls().
/*
* Test program Linux RS485-mode ioctls.
* Build:
* - source /opt/fsl-imx-xwayland/5.15-kirkstone/environment-setup-armv8a-poky-linux
* - $CC -o test-rs485 rs485_ioctl_test.c
* Usage:
* - ./test-rs485 /dev/ttymxc2
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <linux/serial.h>
/* RS485 ioctls: */
#define TIOCGRS485 0x542E
#define TIOCSRS485 0x542F
int main( int argc, char **argv )
{
unsigned int i;
char buf[80];
char *port = argv[1];
int fd = open(port, O_RDWR);
if (fd < 0) {
/* Error handling. See errno. */
fprintf( stderr, "Error opening port \"%s\" (%d): %s\n", port, errno, strerror( errno ));
exit(-1);
}
struct serial_rs485 rs485conf;
if (ioctl (fd, TIOCGRS485, &rs485conf) < 0) {
fprintf( stderr, "Error reading ioctl port (%d): %s\n", errno, strerror( errno ));
}
printf("Port currently RS485 mode is %s\n", (rs485conf.flags & SER_RS485_ENABLED) ? "set" : "NOT set");
/* Close the device when finished: */
if (close (fd) < 0) {
fprintf( stderr, "Error closing port (%d): %s\n", errno, strerror( errno ));
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment