c - Linux UART 传输问题

标签 c linux serial-port uart tty

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

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

以前,我通过设置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

}

提前致谢

阿克塞尔

最佳答案

示波器上的图片看起来不错——它是一个 UART 信号。这里的问题与编程无关,而是没有理解 UART 和 RS-232 之间的区别。

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

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

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

关于c - Linux UART 传输问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48579906/

相关文章:

c - 由于 tgmath.h 和 complex.h,在最新的 Cygwin 中通过 GCC 编译 C 程序时出现问题

c - GSL 求解钟摆运动的 ODE

Linux文件删除错误

c - 通过引脚连接时,USB 端口无法识别微 Controller

c - fork : child does not execute code

c - 返回指向自动变量的指针的函数

python - 从 cron 中的 Matlab 进程调用 Python 脚本

c - 内存泄漏使用 malloc 失败

c# - 串行通信超时长电缆超时

c++ - 从arduino中串行接收的数据解析整数