C 反转/dev/ttyUSB0的输出

C 反转/dev/ttyUSB0的输出,c,linux,C,Linux,我试图反转/dev/ttyUSB0的输出。 我在做“C”。我是Linux新手,但如果你告诉我这个愚蠢的问题,你不会伤害我的感情 我有2个RS485源;一个来自树莓皮,看起来像这样: 来自Raspberry Pi RS485“屏蔽”/子板(/dev/serial0): 另一个是Ubuntu机器上的USB到RS485转换器(/dev/ttyUSB0): 我想将第2/Ubuntu版本倒置,使其看起来像Raspberry Pi版本。 这是我的密码: int set_interface_attribs(i

我试图反转/dev/ttyUSB0的输出。 我在做“C”。我是Linux新手,但如果你告诉我这个愚蠢的问题,你不会伤害我的感情

我有2个RS485源;一个来自树莓皮,看起来像这样:

来自Raspberry Pi RS485“屏蔽”/子板(/dev/serial0):

另一个是Ubuntu机器上的USB到RS485转换器(/dev/ttyUSB0):

我想将第2/Ubuntu版本倒置,使其看起来像Raspberry Pi版本。

这是我的密码:

int set_interface_attribs(int fd, int baudRateFlag)
{
    struct termios tty;

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

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

    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 */
    // TODO: CRTSCTS isn't defined in termios.h
    //  but is in "arm-linux-gnueabihf\bits\termios.h"
    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;
}
int set\u interface\u attribs(int fd,int baudratefag)
{
结构termios tty;
如果(tcgetattr(fd和tty)<0)
{
printf(“来自tcgetattr的错误:%s\n”,strerror(errno));
返回-1;
}
cfsetospeed(&tty,(速度)波特率标志);
cfsetispeed(&tty,(速度)波特率标志);
tty.c|u cflag |=(CLOCAL | CREAD);/*忽略调制解调器控制*/
tty.c_cflag&=~CSIZE;
tty.c_cflag |=CS8;/*8位字符*/
tty.c_cflag&=~PARENB;/*无奇偶校验位*/
tty.c_cflag&=~CSTOPB;/*只需要1个停止位*/
//TODO:termios.h中未定义CRTSCTS
//但是在“arm linux gnueabihf\bits\termios.h”中
tty.c\u cflag&=~CRTSCTS;/*无硬件流量控制*/
/*非规范模式的设置*/
tty.c|u iflag&=~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c|lflag&=~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_of lag&=~OPOST;
/*获取可用的字节*/
tty.c_cc[VMIN]=1;
tty.c_cc[VTIME]=1;
如果(tcsetattr(fd、TCSANOW和tty)!=0)
{
printf(“来自tcsetttr的错误:%s\n”,strerror(errno));
返回-1;
}
返回0;
}

编写一个单独的过滤程序来实现所需的数据反转可能会更容易。然后,通过该程序将数据传输到消费者——如果需要,可以双向传输。这是否足以满足您的需要?通过RS485交换+和-导线?RS485是一个差分系统。在这里交换A&B电缆。交换电线没用,但谢谢你。我在T=>T,R=>R之前交换了电线。它们应该是T=>R,R=>T。(T=传输,R=接收)关于以下语句:
cfsetospeed(&tty,(speed\T)baudratefag);cfsetispeed(&tty,(速度)波特率标志)结构术语的内容包括速度指示器。因此,这些语句需要在将
tty
数据写入I/O之后,还有
baudrateflag
的内容是什么?对于不同的速度有几种定义,它们实际上是位图。因此,您不能只输入
9600
    +
    _____  _______  __________
        |  |     |  | 
        |  |     |  | 
        |--|     |--| 
    -
int set_interface_attribs(int fd, int baudRateFlag)
{
    struct termios tty;

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

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

    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 */
    // TODO: CRTSCTS isn't defined in termios.h
    //  but is in "arm-linux-gnueabihf\bits\termios.h"
    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;
}