【问题标题】:Linux UART transmitting ProblemsLinux UART传输问题
【发布时间】:2018-02-02 09:48:19
【问题描述】:

我正在尝试通过 C 语言中的 UART/tty 与外部 IC 通信。我使用的是 nanoPI neo2 (allwinner h5) 和 friendlyARM 自己的 Linux 操作系统(它是修改后的 Ubuntu 内核)。

我试图让它工作,但是 pi 的 TX 输出是“逻辑反转的?”。您可以在示波器图片中看到我的意思。在那里你可以看到我是否选择了“极性反转” - 模式数据正常,但在正常模式下它不起作用。

过去,我通过设置 MCU(例如 PIC MCU)的反转位来解决这个问题,但我现在使用的 IC 不支持此功能。

http://wiki.friendlyarm.com/wiki/index.php/NanoPi_NEO2

oscilloscope picture 1

oscilloscope picture 2

c source

#include <stdio.h>
//#include <errno.h>
//#include <termios.h>
//#include <unistd.h>
#include <string.h>


#include <fcntl.h>  /* File Control Definitions          */
#include <termios.h>/* POSIX Terminal Control Definitions*/
#include <unistd.h> /* UNIX Standard Definitions         */
#include <errno.h>  /* ERROR Number Definitions          */
#include <unistd.h>
#include <sys/types.h>
#include <sys/signal.h>
/* source : http://stackoverflow.com/a/6947758 */

int fd;

int set_interface_attribs (int fd, int speed, int parity) {
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0) {
                perror ("error from tcgetattr");
                return -1;
        }

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

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag &= ~IGNBRK;         // disable break processing
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                        // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = 0;            // read doesn't block
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0) {
                perror ("error from tcsetattr");
                return -1;
        }
        return 0;
}

void set_blocking (int fd, int should_block) {
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                perror ("error from tggetattr");
                return;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
                perror ("error setting term attributes");
}

int main()
{
    char *portname = "/dev/ttyS0";
    int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0)
    {
            //perror ("error %d opening %s: %s", errno, portname, strerror (errno));
            return;
    }
    set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
    set_blocking (fd, 0);                // set no blocking
    write (fd, "hello!\n", 7);           // send 7 character greeting
    usleep ((7 + 25) * 100);             // sleep enough to transmit the 7 plus
                                         // receive 25:  approx 100 uS per char transmit
    char buf [100];
    int n = read (fd, buf, sizeof buf);  // read up to 100 characters if ready to read

}

提前致谢

阿克塞尔

【问题讨论】:

    标签: c linux serial-port uart tty


    【解决方案1】:

    示波器上的图片看起来不错 - 这是一个 UART 信号。这里的问题不是编程相关的,而是看不懂UART和RS-232的区别。

    您在示波器上选择了 RS-232 编码,与 UART 相比,RS-232 使用不同的反转极性。

    • UART 空闲高电压=Vdd,RS-232 在负电压
    • 在 UART 上,高电压 = Vdd 位表示 1,低电压 = 0V 表示 0。
    • 在 RS-232 上,高电压 > +3V 位表示 0,负电压

    因此,当您尝试使用 RS-232 编码解码 UART 信号时,它会在您反转极性时起作用。显然,Rigol 对实际电压水平并不挑剔,因为它根本不了解信号不是 RS-232。

    【讨论】:

    • IIRC,如果你使用V24驱动芯片,它会反转逻辑。
    • @MartinJames 任何 RS-232 收发器(MAX232 和朋友)都会将 UART 逻辑反转为 RS-232 逻辑。
    猜你喜欢
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2020-08-04
    • 2019-06-15
    • 1970-01-01
    • 2021-08-07
    相关资源
    最近更新 更多