1

我想澄清一下 socat 对串行通信的作用,因为由于某种原因,我只能在使用 socat 时让与我的 Arduino 的通信正常工作。

这是我用来设置 socat 的命令:

socat -d -v -x PTY,link=/tmp/serial,wait-slave,raw /dev/ttyACM0,raw

我在 C++ 中使用的代码是:

int file;

file = open("/tmp/serial", O_RDWR | O_NOCTTY | O_NDELAY);
if (file == -1)
  {

    perror("open_port: Unable to open \n");
  }
  else
    fcntl(file, F_SETFL, 0);

struct termios options;


 //* Get the current options for the port...


tcgetattr(file, &options);

cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);

options.c_oflag = 0;
//options.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OLCUC | OPOST);


options.c_cflag |= (CLOCAL | CREAD);

cfmakeraw(&options);

tcsetattr(file, TCSANOW, &options);

sprintf(sendpack,"$C000C000C000C000\r");
num = write(file,sendpack,18);  
tcflush(file, TCIOFLUSH); // clear buffer

因此,当我通过 socat 运行此程序时,arduino 会做出应有的反应,但如果我将 open() 行从 /tmp/serial 更改为 /dev/ttyACM0,则 arduino 将不再执行。有人可以向我解释一下 socat 对数据做了什么以使事情正常进行,以及如何更改我的 C 代码来做同样的事情?我不能在我的最终实现中使用 socat,因为它会导致延迟,使我的机器人无法预测。

4

0 回答 0