串行数据嗅探器未按预期工作

时间:2017-08-03 07:03:31

标签: c++ c serial-port rs485

我试图从一个端口/dev/ttyUSB0获取串行数据并将其传递给第二个端口/dev/ttyUSB1,反之亦然。通过我,显然,炫耀的逻辑,下面的代码应该按预期工作。但数据似乎没有正确到达第二个端口,因为我没有得到正确的响应。我需要自己的程序,因为我需要控制两个串行设备之间的数据流。

#include <iostream>
#include <errno.h>
#include <fcntl.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

using namespace std;
int set_interface_attribs(int fd, int speed)
{
    struct termios tty;

    if (tcgetattr(fd, &tty) < 0) {
        printf("Error from tcgetattr: %s\n", strerror(errno));
        return -1;
    }

    cfsetospeed(&tty, (speed_t)speed);
    cfsetispeed(&tty, (speed_t)speed);

    tty.c_cflag |= (CLOCAL | CREAD);    /* ignore modem controls */
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;         /* 8-bit characters */
    tty.c_cflag &= ~PARENB;     /* no parity bit */
    tty.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
    tty.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */

    /* setup for non-canonical mode */
    tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
    tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    tty.c_oflag &= ~OPOST;

    /* fetch bytes as they become available */
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 1;

    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        printf("Error from tcsetattr: %s\n", strerror(errno));
        return -1;
    }
    return 0;
}

void set_mincount(int fd, int mcount)
{
    struct termios tty;

    if (tcgetattr(fd, &tty) < 0) {
        printf("Error tcgetattr: %s\n", strerror(errno));
        return;
    }

    tty.c_cc[VMIN] = mcount ? 1 : 0;
    tty.c_cc[VTIME] = 5;        /* half second timer */

    if (tcsetattr(fd, TCSANOW, &tty) < 0)
        printf("Error tcsetattr: %s\n", strerror(errno));
}

void read_send(int port1, int port2) 
{
    unsigned char buf[1024];
    int rdlen;
    int wlen;
    printf("Reading port 1\n"); 
    rdlen = read(port1, buf, sizeof(buf) - 1);
    if (rdlen > 0) {
        unsigned char *p;
        printf("Writing %d:\n", rdlen);
        wlen = write (port2, buf, rdlen);
        tcdrain(port2);
        if (wlen != rdlen) {
            printf("Error from write: %d, %s\n", wlen, strerror(errno));
        }

    } else if (rdlen < 0) {
        printf("Error from read: %d: %s\n", rdlen, strerror(errno));
    }

}
void connect_data(int port1, int port2)
{
    //read 1 write on 2; read 2 write on 1
    read_send(port1, port2);
    read_send(port2, port1);
}

int main()
{
    char *portname_cp = "/dev/ttyUSB0";
    char *portname_rfid = "/dev/ttyUSB1";
    int fd_cp;
    int fd_rfid;
    int wlen;

    fd_rfid = open(portname_rfid, O_RDWR | O_NOCTTY | O_SYNC);
    fd_cp = open(portname_cp, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd_rfid < 0) {
        printf("Error opening %s: %s\n", portname_rfid, strerror(errno));
        return -1;
    }
    if (fd_cp < 0) {
        printf("Error opening %s: %s\n", portname_cp, strerror(errno));
        return -1;
    }
    printf("Ports opened\n");
    /*baudrate 115200, 8 bits, no parity, 1 stop bit */
    set_interface_attribs(fd_rfid, B115200);
    //set_mincount(fd_rfid, 0);                /* set to pure timed read */
    set_interface_attribs(fd_cp, B115200);
    //set_mincount(fd_cp, 0);                /* set to pure timed read */
    do {
        connect_data(fd_cp, fd_rfid);
    } while(true);
    return 0;
}

0 个答案:

没有答案