rs485 working again. had to add termios stuff and set baud

stable
Penguin 3 years ago
parent f7fc00d5c0
commit 26fe06fcc7

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -31,11 +31,11 @@
"-Icfg", "-Icfg",
"-Iinc", "-Iinc",
"-o", "-o",
"build/p_idp.o", "build/p_serial_packet.o",
"src/p_idp.cpp" "src/p_serial_packet.cpp"
], ],
"directory": "/storage/Shared/Projects/Penguinator/pi_serial_bus_manager", "directory": "/storage/Shared/Projects/Penguinator/pi_serial_bus_manager",
"file": "src/p_idp.cpp" "file": "src/p_serial_packet.cpp"
}, },
{ {
"arguments": [ "arguments": [
@ -69,10 +69,10 @@
"-Icfg", "-Icfg",
"-Iinc", "-Iinc",
"-o", "-o",
"build/p_serial_packet.o", "build/p_idp.o",
"src/p_serial_packet.cpp" "src/p_idp.cpp"
], ],
"directory": "/storage/Shared/Projects/Penguinator/pi_serial_bus_manager", "directory": "/storage/Shared/Projects/Penguinator/pi_serial_bus_manager",
"file": "src/p_serial_packet.cpp" "file": "src/p_idp.cpp"
} }
] ]

@ -1,18 +1,22 @@
#ifndef __P_SERIAL_BUS_H__ #ifndef __P_SERIAL_BUS_H__
#define __P_SERIAL_BUS_H__ #define __P_SERIAL_BUS_H__
#include "p_serial_packet.h"
#include <linux/serial.h>
#include <termio.h>
class ps_bus class ps_bus
{ {
public: public:
ps_bus(const char* dev); ps_bus(const char* dev);
~ps_bus(); ~ps_bus();
void ps_bus_start(); bool send_pkt(const ps_packet* const pkt);
bool recv_pkt(ps_packet* const pkt);
private: private:
struct serial_rs485 rs485conf;
int dev_fd; int dev_fd;
struct termios toptions;
}; };
void p_sbus_start(void);
#endif #endif

@ -19,38 +19,46 @@ void sigint_handler(int param)
{ {
b_quit = true; b_quit = true;
std::cout << "Exiting..." << std::endl; std::cout << "Exiting..." << std::endl;
return;
} }
int main() int main()
{ {
signal(SIGINT, sigint_handler); signal(SIGINT, sigint_handler);
ps_bus sbus(SERIAL_DEV); ps_bus sbus(SERIAL_DEV);
zmq::context_t ctx; zmq::context_t ctx;
zmq::socket_t sub(ctx, ZMQ_SUB); zmq::socket_t sub(ctx, ZMQ_SUB);
sub.connect(ZMQ_IPC_ADDR); sub.connect(ZMQ_IPC_ADDR);
sub.setsockopt(ZMQ_SUBSCRIBE, "", 0); sub.setsockopt(ZMQ_SUBSCRIBE, "", 0);
zmq::message_t msg_recv;
zmq::message_t msg_send;
while (!b_quit) while (!b_quit)
{ {
zmq::message_t msg_recv;
int32_t x = 0; int32_t x = 0;
int32_t y = 0; int32_t y = 0;
try
{
if (sub.recv(&msg_recv, 0)) if (sub.recv(&msg_recv, 0))
{ {
uint8_t xy[64]; uint8_t xy[64];
memset(xy, 0, 64); memset(xy, 0, 64);
memcpy(xy, msg_recv.data(), msg_recv.size()); memcpy(xy, msg_recv.data(), msg_recv.size());
sscanf((const char*)xy, "X:%" PRId32 " Y:%" PRId32 "\n", &x, &y); sscanf((const char*)xy, "X:%" PRId32 " Y:%" PRId32 "\n", &x,
&y);
ps_packet xy_packet = ps_packet::from_xy(x, y); ps_packet xy_packet = ps_packet::from_xy(x, y);
xy_packet.encode(); xy_packet.encode();
xy_packet.show_packet(); // xy_packet.show_packet();
if (!sbus.send_pkt(&xy_packet))
for (size_t ind = 0; ind < xy_packet.msg_len; ind++)
{ {
printf("[%lu]: %02x ", ind, xy_packet.data[ind]); printf("Some failure on transmission\n");
} }
printf("\n"); }
}
catch (zmq::error_t& e)
{
std::cout << e.what() << std::endl;
} }
} }

@ -2,8 +2,6 @@
#include <cstring> #include <cstring>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <iostream>
#include <linux/serial.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -13,15 +11,16 @@
ps_bus::ps_bus(const char* dev) ps_bus::ps_bus(const char* dev)
{ {
/* Open your specific device (e.g., /dev/mydevice): */ /* Open your specific device (e.g., /dev/mydevice): */
int fd = open(dev, O_RDWR); printf("%s\n", dev);
if (fd < 0) dev_fd = open(dev, O_RDWR | O_NOCTTY);
if (dev_fd < 0)
{ {
/* Error handling. See errno. */ /* Error handling. See errno. */
fprintf(stderr, "Creation of the bus failed... error: %s", fprintf(stderr, "Creation of the bus failed... error: %s",
strerror(errno)); strerror(errno));
exit(1);
} }
memset(&rs485conf, 0, sizeof(rs485conf));
struct serial_rs485 rs485conf;
/* Enable RS485 mode: */ /* Enable RS485 mode: */
rs485conf.flags |= SER_RS485_ENABLED; rs485conf.flags |= SER_RS485_ENABLED;
@ -29,10 +28,10 @@ ps_bus::ps_bus(const char* dev)
/* Set logical level for RTS pin equal to 1 when sending: */ /* Set logical level for RTS pin equal to 1 when sending: */
rs485conf.flags |= SER_RS485_RTS_ON_SEND; rs485conf.flags |= SER_RS485_RTS_ON_SEND;
/* or, set logical level for RTS pin equal to 0 when sending: */ /* or, set logical level for RTS pin equal to 0 when sending: */
rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND); // rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
/* Set logical level for RTS pin equal to 1 after sending: */ /* Set logical level for RTS pin equal to 1 after sending: */
rs485conf.flags |= SER_RS485_RTS_AFTER_SEND; // rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
/* or, set logical level for RTS pin equal to 0 after sending: */ /* or, set logical level for RTS pin equal to 0 after sending: */
rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND); rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
@ -40,15 +39,36 @@ ps_bus::ps_bus(const char* dev)
// rs485conf.delay_rts_before_send = ...; // rs485conf.delay_rts_before_send = ...;
// /* Set rts delay after send, if needed: */ // /* Set rts delay after send, if needed: */
// rs485conf.delay_rts_after_send = ...; // rs485conf.delay_rts_after_send = 10;
/* Set this flag if you want to receive data even while sending data */ /* Set this flag if you want to receive data even while sending data */
// rs485conf.flags |= SER_RS485_RX_DURING_TX; // rs485conf.flags |= SER_RS485_RX_DURING_TX;
if (ioctl(fd, TIOCSRS485, &rs485conf) < 0) if (ioctl(dev_fd, TIOCSRS485, &rs485conf) < 0)
{ {
printf("ERROR\n");
/* Error handling. See errno. */ /* Error handling. See errno. */
} }
memset(&toptions, 0, sizeof(toptions));
tcgetattr(dev_fd, &toptions);
toptions.c_cflag &= ~(CSIZE | PARENB);
// enable receiver
toptions.c_cflag |= CREAD;
// 8 data bit
toptions.c_cflag |= CS8;
// Ignore framing errors and parity errors
toptions.c_lflag &= ~(ICANON);
toptions.c_lflag &= ~(ECHO);
toptions.c_lflag &= ~(ECHOE);
// disable signal chars
toptions.c_lflag &= ~(ISIG);
// set baud rate
cfsetspeed(&toptions, B115200);
// flush cache
tcflush(dev_fd, TCIFLUSH);
// apply
tcsetattr(dev_fd, TCSANOW, &toptions);
} }
ps_bus::~ps_bus() ps_bus::~ps_bus()
@ -60,5 +80,17 @@ ps_bus::~ps_bus()
} }
} }
void ps_bus::ps_bus_start() bool ps_bus::send_pkt(const ps_packet* const pkt)
{} {
bool ret = true;
if (write(dev_fd, pkt->data, pkt->msg_len) != pkt->msg_len)
{
ret = false;
}
return ret;
}
bool ps_bus::recv_pkt(ps_packet* const pkt)
{
bool ret = true;
return ret;
}

Loading…
Cancel
Save