-
-
Save Scott31393/7673659c13c40e3a7e9f36fc0cb29c79 to your computer and use it in GitHub Desktop.
A quick test function to demonstrate the Linux serial / RS485 ioctls().
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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