Created
December 3, 2019 06:19
-
-
Save AndreRenaud/712abbef1340d907fca6bdadafb4ac92 to your computer and use it in GitHub Desktop.
Simple RS485 stress
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
#include <linux/serial.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <string.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <sys/ioctl.h> | |
#include <signal.h> | |
#include <termios.h> | |
#include <errno.h> | |
int main(int argc, char *argv[]) | |
{ | |
struct serial_rs485 rs485; | |
int fd; | |
struct termios tio; | |
const char *port_name = "/dev/ttymxc2"; | |
if (argc >= 2) | |
port_name = argv[1]; | |
printf("Opening %s\n", port_name); | |
fd = open(port_name, O_RDWR | O_NONBLOCK | O_NOCTTY); | |
if (fd < 0) { | |
perror("open"); | |
return -1; | |
} | |
rs485.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND; | |
ioctl(fd, TIOCSRS485, &rs485); | |
if (tcgetattr(fd, &tio) == -1) { | |
perror("tcgetattr"); | |
return -1; | |
} | |
// cfmakeraw(&tio); | |
tio.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | |
| INLCR | IGNCR | ICRNL | IXON); | |
tio.c_oflag &= ~OPOST; | |
tio.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); | |
tio.c_cflag &= ~(CSIZE | PARENB); | |
tio.c_cflag |= CS8; | |
tio.c_cflag = B115200 | CS8 | CLOCAL | CREAD; | |
tio.c_iflag = IGNPAR | ICRNL; | |
tio.c_oflag = 0; | |
tio.c_lflag = ICANON; | |
tio.c_cc[VMIN] = 1; | |
tio.c_cc[VTIME] = 0; | |
if (tcflush(fd, TCIFLUSH) == -1) { | |
fprintf(stderr, "tcflush failed: %d %s\n", errno, strerror(errno)); | |
} | |
if (tcsetattr(fd, TCSANOW, &tio) == -1) { | |
printf("tcsetattr failed: %d\n", errno); | |
} | |
const char* data = "foo\r\n"; | |
char buf[10]; | |
int i = 1, ret; | |
unsigned int read_count = 0, write_count = 0; | |
while (i++) { | |
if ((ret = write(fd, data, 5)) == -1) { | |
fprintf(stderr, "write failed: %d %s\n", errno, strerror(errno)); | |
} | |
else { | |
write_count += ret; | |
} | |
while ((ret = read(fd, buf, 10)) != -1) { | |
read_count += ret; | |
} | |
usleep(100 * 1000); | |
if (i % 10 == 0) { | |
printf("data: read=%d write=%d\n", read_count, write_count); | |
} | |
} | |
close(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment