C++串行通讯

发布于 2024-09-17 01:35:29 字数 1350 浏览 3 评论 0原文

我正在运行 Ubuntu 9.10,但我似乎在使用 termios 时遇到问题。 所以我可以启动minicom以57600波特率、8N1打开串口,没有硬件或软件流控制,效果很好。我输入@17 5,我的设备有响应。当我尝试在 C++ 代码中设置串行端口时,我没有得到响应。我知道软件正在与端口通信,因为 LED 亮了。

这是我的主要内容:

int main(void)
{
  int fd; /* File descriptor for the port */

  fd = open("/dev/keyspan1", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1)
    {
      /*
       * Could not open the port.
       */

      perror("open_port: Unable to open /dev/ttyS0 - ");
    }
  else
    fcntl(fd, F_SETFL, 0);

  /*****************************CHANGE PORT OPTIONS***************************/
  struct termios options;

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

  tcgetattr(fd, &options);

  /*
   * Set the baud rates to 57600...
   */

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

  /*
   * Enable the receiver and set local mode...
   */

  options.c_cflag |= (CLOCAL | CREAD);

  /*
   * Set the new options for the port...
   */


  tcsetattr(fd, TCSANOW, &options);
  /***********************************END PORT OPTIONS***********************/

  int n;
  n = write(fd, "@17 5 \r", 7);
  if (n < 0)
    fputs("write() of 8 bytes failed!\n", stderr);

  char buff[20];

  sleep(1);

  n = read(fd, buff, 10);

  printf("Returned = %d\n", n);

  close(fd);

  return(0);
}

任何建议将不胜感激。谢谢。

I'm running Ubuntu 9.10 and I seem to be having trouble with termios.
So I can start minicom open the serial port at 57600 Baud, 8N1, no hardware or software flow control and it works great. I type in @17 5 and my device responds. When I try to set up my serial port in my C++ code I don't get a response. I know that the software is communicating to the port because an led turns on.

Here is my main:

int main(void)
{
  int fd; /* File descriptor for the port */

  fd = open("/dev/keyspan1", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1)
    {
      /*
       * Could not open the port.
       */

      perror("open_port: Unable to open /dev/ttyS0 - ");
    }
  else
    fcntl(fd, F_SETFL, 0);

  /*****************************CHANGE PORT OPTIONS***************************/
  struct termios options;

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

  tcgetattr(fd, &options);

  /*
   * Set the baud rates to 57600...
   */

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

  /*
   * Enable the receiver and set local mode...
   */

  options.c_cflag |= (CLOCAL | CREAD);

  /*
   * Set the new options for the port...
   */


  tcsetattr(fd, TCSANOW, &options);
  /***********************************END PORT OPTIONS***********************/

  int n;
  n = write(fd, "@17 5 \r", 7);
  if (n < 0)
    fputs("write() of 8 bytes failed!\n", stderr);

  char buff[20];

  sleep(1);

  n = read(fd, buff, 10);

  printf("Returned = %d\n", n);

  close(fd);

  return(0);
}

Any suggestions would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知在何时 2024-09-24 01:35:45

您可能需要将终端初始化为原始模式。我建议您使用 cfmakeraw() 来初始化术语选项结构。其中,cfmakeraw 将确保禁用流量控制、禁用奇偶校验以及输入逐个字符可用。

cfmakeraw 是非 Posix 的。如果您关心可移植性,请查找 cfmakeraw 联机帮助页以了解它所做的设置。

You probably need to initialize your terminal to raw mode. I suggest you use cfmakeraw() to initialize the term options structure. Among others, cfmakeraw will make sure that flow control is disabled, parity checks are disabled, and input is available character by character.

cfmakeraw is non-Posix. If you're concerned with portability, look up the cfmakeraw manpage for the settings it is making.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文