C 如何利用Linux';虚拟串行端口?

C 如何利用Linux';虚拟串行端口?,c,linux,serial-port,pyserial,termios,C,Linux,Serial Port,Pyserial,Termios,我想将数据从一个C程序发送到一个Python程序中,该程序将可视化这些数据。开发环境是一台Linux(Ubuntu 18.04LTS)计算机。更清楚地说,这两个程序都在同一台计算机上运行 我使用termios在C程序中打开串行端口,在Python端使用pySerial。至于串口,我使用的是“ttyS0”。问题是,当我从C程序向Python程序发送“Hello”并在终端上打印它时,我看到的是空格字符,基本上我得到了这个“” 我的问题是,我可以使用“ttyS0”串行端口(我猜这是一个虚拟端口)来实现

我想将数据从一个C程序发送到一个Python程序中,该程序将可视化这些数据。开发环境是一台Linux(Ubuntu 18.04LTS)计算机。更清楚地说,这两个程序都在同一台计算机上运行

我使用termios在C程序中打开串行端口,在Python端使用pySerial。至于串口,我使用的是“ttyS0”。问题是,当我从C程序向Python程序发送“Hello”并在终端上打印它时,我看到的是空格字符,基本上我得到了这个“”

我的问题是,我可以使用“ttyS0”串行端口(我猜这是一个虚拟端口)来实现这个目的吗

这是C代码:

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

// Termios init functions are not posted because the configuration
// is correct and proved that they are working.

int main()
{
    char *portname = "/dev/ttyS0";
    int fd;
    int wlen;
    unsigned char writeBuffer[] = "Hello!";

    fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0) {
        printf("Error opening %s: %s\n", portname, strerror(errno));
        return -1;
    }
    /*baudrate 115200, 8 bits, no parity, 1 stop bit */
    set_interface_attribs(fd, B115200);

    do{
        wlen = write(fd, writeBuffer, sizeof(writeBuffer));
        printf("Sent data is: \"%s\"\n", writeBuffer);
        delay(500);
    } while(1);
}

ttyS0是计算机的串行端口——它没有任何“虚拟”功能。写入此设备将尝试使用该端口从计算机中传输数据,从该设备读取将尝试从连接到该端口的外部设备接收数据。同一台计算机上的两个程序无法使用串行端口进行有效通信

我想你要找的不是a,就是a。哪一个最合适将取决于您的具体要求