diff --git a/.cache/clangd/index/p_serial_bus.cpp.ABEC82C874071671.idx b/.cache/clangd/index/p_serial_bus.cpp.ABEC82C874071671.idx index 5d7fed7..2f6dc1d 100644 Binary files a/.cache/clangd/index/p_serial_bus.cpp.ABEC82C874071671.idx and b/.cache/clangd/index/p_serial_bus.cpp.ABEC82C874071671.idx differ diff --git a/.cache/clangd/index/p_serial_bus.h.48DDA2AACC6854FD.idx b/.cache/clangd/index/p_serial_bus.h.48DDA2AACC6854FD.idx index 0a096e8..23e736a 100644 Binary files a/.cache/clangd/index/p_serial_bus.h.48DDA2AACC6854FD.idx and b/.cache/clangd/index/p_serial_bus.h.48DDA2AACC6854FD.idx differ diff --git a/bin/sbm b/bin/sbm index 0d4b01c..5c2f543 100644 Binary files a/bin/sbm and b/bin/sbm differ diff --git a/build/main.o b/build/main.o index 8b5bfed..3ce9329 100644 Binary files a/build/main.o and b/build/main.o differ diff --git a/build/p_serial_bus.o b/build/p_serial_bus.o index 9653e60..20387af 100644 Binary files a/build/p_serial_bus.o and b/build/p_serial_bus.o differ diff --git a/build/p_serial_packet.o b/build/p_serial_packet.o index daec700..a40b500 100644 Binary files a/build/p_serial_packet.o and b/build/p_serial_packet.o differ diff --git a/compile_commands.json b/compile_commands.json index 44ba02e..88a123b 100644 --- a/compile_commands.json +++ b/compile_commands.json @@ -31,11 +31,11 @@ "-Icfg", "-Iinc", "-o", - "build/p_idp.o", - "src/p_idp.cpp" + "build/p_serial_packet.o", + "src/p_serial_packet.cpp" ], "directory": "/storage/Shared/Projects/Penguinator/pi_serial_bus_manager", - "file": "src/p_idp.cpp" + "file": "src/p_serial_packet.cpp" }, { "arguments": [ @@ -69,10 +69,10 @@ "-Icfg", "-Iinc", "-o", - "build/p_serial_packet.o", - "src/p_serial_packet.cpp" + "build/p_idp.o", + "src/p_idp.cpp" ], "directory": "/storage/Shared/Projects/Penguinator/pi_serial_bus_manager", - "file": "src/p_serial_packet.cpp" + "file": "src/p_idp.cpp" } ] \ No newline at end of file diff --git a/inc/p_serial_bus.h b/inc/p_serial_bus.h index 41e6b41..c628da3 100644 --- a/inc/p_serial_bus.h +++ b/inc/p_serial_bus.h @@ -1,18 +1,22 @@ #ifndef __P_SERIAL_BUS_H__ #define __P_SERIAL_BUS_H__ +#include "p_serial_packet.h" +#include +#include class ps_bus { public: ps_bus(const char* dev); ~ps_bus(); - void ps_bus_start(); + bool send_pkt(const ps_packet* const pkt); + bool recv_pkt(ps_packet* const pkt); private: + struct serial_rs485 rs485conf; int dev_fd; + struct termios toptions; }; -void p_sbus_start(void); - #endif diff --git a/src/main.cpp b/src/main.cpp index 77712e8..f2f9d7d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,38 +19,46 @@ void sigint_handler(int param) { b_quit = true; std::cout << "Exiting..." << std::endl; + return; } int main() { signal(SIGINT, sigint_handler); + ps_bus sbus(SERIAL_DEV); zmq::context_t ctx; zmq::socket_t sub(ctx, ZMQ_SUB); sub.connect(ZMQ_IPC_ADDR); sub.setsockopt(ZMQ_SUBSCRIBE, "", 0); - zmq::message_t msg_recv; - zmq::message_t msg_send; while (!b_quit) { + zmq::message_t msg_recv; int32_t x = 0; int32_t y = 0; - if (sub.recv(&msg_recv, 0)) + try { - uint8_t xy[64]; - memset(xy, 0, 64); - memcpy(xy, msg_recv.data(), msg_recv.size()); - sscanf((const char*)xy, "X:%" PRId32 " Y:%" PRId32 "\n", &x, &y); - ps_packet xy_packet = ps_packet::from_xy(x, y); - xy_packet.encode(); - xy_packet.show_packet(); - - for (size_t ind = 0; ind < xy_packet.msg_len; ind++) + if (sub.recv(&msg_recv, 0)) { - printf("[%lu]: %02x ", ind, xy_packet.data[ind]); + uint8_t xy[64]; + memset(xy, 0, 64); + memcpy(xy, msg_recv.data(), msg_recv.size()); + sscanf((const char*)xy, "X:%" PRId32 " Y:%" PRId32 "\n", &x, + &y); + ps_packet xy_packet = ps_packet::from_xy(x, y); + xy_packet.encode(); + // xy_packet.show_packet(); + if (!sbus.send_pkt(&xy_packet)) + { + printf("Some failure on transmission\n"); + } } - printf("\n"); + } + catch (zmq::error_t& e) + { + + std::cout << e.what() << std::endl; } } diff --git a/src/p_serial_bus.cpp b/src/p_serial_bus.cpp index 85f7e8f..6a6c82c 100644 --- a/src/p_serial_bus.cpp +++ b/src/p_serial_bus.cpp @@ -2,8 +2,6 @@ #include #include #include -#include -#include #include #include @@ -13,15 +11,16 @@ ps_bus::ps_bus(const char* dev) { /* Open your specific device (e.g., /dev/mydevice): */ - int fd = open(dev, O_RDWR); - if (fd < 0) + 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); } - - struct serial_rs485 rs485conf; + memset(&rs485conf, 0, sizeof(rs485conf)); /* Enable RS485 mode: */ 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: */ 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); + // 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; + // 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); @@ -40,15 +39,36 @@ ps_bus::ps_bus(const char* dev) // rs485conf.delay_rts_before_send = ...; // /* 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 */ // 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. */ } + 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() @@ -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; +}