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.
102 lines
2.5 KiB
C++
102 lines
2.5 KiB
C++
#include "p_serial_bus.h"
|
|
#include "p_util.h"
|
|
#include <cstring>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
|
|
/* Include definition for RS485 ioctls: TIOCGRS485 and TIOCSRS485 */
|
|
#include <sys/ioctl.h>
|
|
|
|
ps_bus::ps_bus(const char* dev)
|
|
{
|
|
/* Open your specific device (e.g., /dev/mydevice): */
|
|
printf("%s\n", dev);
|
|
dev_fd = open(dev, O_RDWR | O_NOCTTY);
|
|
if (dev_fd < 0)
|
|
{
|
|
/* Error handling. See errno. */
|
|
fprintf(stderr, "Creation of the bus failed... error: %s",
|
|
strerror(errno));
|
|
exit(1);
|
|
}
|
|
memset(&rs485conf, 0, sizeof(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 &= (uint32_t) ~(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 = 10;
|
|
|
|
/* Set this flag if you want to receive data even while sending data */
|
|
// rs485conf.flags |= SER_RS485_RX_DURING_TX;
|
|
|
|
memset(&toptions, 0, sizeof(toptions));
|
|
tcgetattr(dev_fd, &toptions);
|
|
toptions.c_cflag &= (uint32_t) ~(CSIZE | PARENB);
|
|
// enable receiver
|
|
toptions.c_cflag |= CREAD;
|
|
// 8 data bit
|
|
toptions.c_cflag |= CS8;
|
|
|
|
// Ignore framing errors and parity errors
|
|
toptions.c_lflag &= (uint32_t) ~(ICANON);
|
|
toptions.c_lflag &= (uint32_t) ~(ECHO);
|
|
toptions.c_lflag &= (uint32_t) ~(ECHOE);
|
|
// disable signal chars
|
|
toptions.c_lflag &= (uint32_t) ~(ISIG);
|
|
// set baud rate
|
|
cfsetspeed(&toptions, B115200);
|
|
// flush cache
|
|
tcflush(dev_fd, TCIFLUSH);
|
|
// apply
|
|
tcsetattr(dev_fd, TCSANOW, &toptions);
|
|
if (ioctl(dev_fd, TIOCSRS485, &rs485conf) < 0)
|
|
{
|
|
printf("ERROR\n");
|
|
/* Error handling. See errno. */
|
|
}
|
|
}
|
|
|
|
ps_bus::~ps_bus()
|
|
{
|
|
/* Close the device when finished: */
|
|
if (close(dev_fd) < 0)
|
|
{
|
|
/* Error handling. See errno. */
|
|
}
|
|
}
|
|
|
|
bool ps_bus::send_pkt(const ps_packet* const pkt)
|
|
{
|
|
bool ret = true;
|
|
pkt->show_packet();
|
|
if (write(dev_fd, pkt->data, pkt->msg_len) != (int)pkt->msg_len)
|
|
{
|
|
ret = false;
|
|
}
|
|
tcflush(dev_fd, TCIFLUSH);
|
|
usleep(1000);
|
|
return ret;
|
|
}
|
|
bool ps_bus::recv_pkt(ps_packet* const pkt)
|
|
{
|
|
UNUSED(pkt);
|
|
bool ret = true;
|
|
return ret;
|
|
}
|