You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.7 KiB
C++
64 lines
1.7 KiB
C++
#include <cstring>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <iostream>
|
|
#include <linux/serial.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
|
|
/* Include definition for RS485 ioctls: TIOCGRS485 and TIOCSRS485 */
|
|
#include <sys/ioctl.h>
|
|
|
|
int main()
|
|
{
|
|
uint8_t send_buffer[14] = {0x7E, 0x01, 0x02, 8, 0x01, 0x02, 0x03,
|
|
0x7D, 0x5E, 0x05, 0x06, 0x07, 0x08, 0xFF};
|
|
|
|
/* Open your specific device (e.g., /dev/mydevice): */
|
|
int fd = open("/dev/ttyAMA1", O_RDWR);
|
|
if (fd < 0)
|
|
{
|
|
/* Error handling. See errno. */
|
|
fprintf(stderr, "Failed... error:%s", strerror(errno));
|
|
}
|
|
|
|
struct serial_rs485 rs485conf;
|
|
|
|
/* Enable RS485 mode: */
|
|
rs485conf.flags |= SER_RS485_ENABLED;
|
|
|
|
/* Set logical level for RTS pin equal to 1 when sending: */
|
|
rs485conf.flags |= SER_RS485_RTS_ON_SEND;
|
|
/* or, set logical level for RTS pin equal to 0 when sending: */
|
|
rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
|
|
|
|
/* Set logical level for RTS pin equal to 1 after sending: */
|
|
rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
|
|
/* or, set logical level for RTS pin equal to 0 after sending: */
|
|
rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
|
|
|
|
// /* Set rts delay before send, if needed: */
|
|
// rs485conf.delay_rts_before_send = ...;
|
|
|
|
// /* Set rts delay after send, if needed: */
|
|
// rs485conf.delay_rts_after_send = ...;
|
|
|
|
/* Set this flag if you want to receive data even while sending data */
|
|
// rs485conf.flags |= SER_RS485_RX_DURING_TX;
|
|
|
|
if (ioctl(fd, TIOCSRS485, &rs485conf) < 0)
|
|
{
|
|
/* Error handling. See errno. */
|
|
}
|
|
|
|
/* Use read() and write() syscalls here... */
|
|
ssize_t rc = 0;
|
|
write(fd, send_buffer, 14);
|
|
|
|
/* Close the device when finished: */
|
|
if (close(fd) < 0)
|
|
{
|
|
/* Error handling. See errno. */
|
|
}
|
|
}
|