如何从Linux串口读取数据

发布于 2024-09-08 04:49:49 字数 1144 浏览 5 评论 0原文

我正在研究必须使用无线串行通信进行控制的机器人。机器人在微控制器上运行(通过烧录 .hex 文件)。我想使用我的 Linux (Ubuntu) PC 来控制它。我是串口编程新手。我可以发送数据,但无法读取数据。

在微控制器上运行的几段代码:

发送数据的函数:

void TxData(unsigned char tx_data)
{
    SBUF = tx_data;  // Transmit data that is passed to this function
    while(TI == 0)   // Wait while data is being transmitted
        ;
}

我正在通过字符数组发送数据 data_array[i]:

  for (i=4; i<=6; i++)
  {
      TxData(data_array[i]);
      RI = 0;              // Clear receive interrupt. Must be cleared by the user.
      TI = 0;        // Clear transmit interrupt. Must be cleared by the user.
  }

现在,在 Linux 上运行的 C 程序的代码段...

while (flag == 0) {
    int res = read(fd, buf, 255);
    buf[res] = 0; /* Set end of string, so we can printf */
    printf(":%s:%d\n", buf, res);
    if (buf[0] == '\0')
        flag = 1;
}

它打印出来res = 0 的值。

实际上我想逐个字符地读取数据来执行计算并做出进一步的决定。还有另一种方法可以做到这一点吗?

注:Linux上串口编程有好的学习资料(代码)吗?

如何从Linux串口读取...

I am working on robot which has to control using wireless serial communication. The robot is running on a microcontroller (by burning a .hex file). I want to control it using my Linux (Ubuntu) PC. I am new to serial port programming. I am able to send the data, but I am not able to read data.

A few piece of code which is running over at the microcontroller:

Function to send data:

void TxData(unsigned char tx_data)
{
    SBUF = tx_data;  // Transmit data that is passed to this function
    while(TI == 0)   // Wait while data is being transmitted
        ;
}

I am sending data through an array of characters data_array[i]:

  for (i=4; i<=6; i++)
  {
      TxData(data_array[i]);
      RI = 0;              // Clear receive interrupt. Must be cleared by the user.
      TI = 0;        // Clear transmit interrupt. Must be cleared by the user.
  }

Now the piece of code from the C program running on Linux...

while (flag == 0) {
    int res = read(fd, buf, 255);
    buf[res] = 0; /* Set end of string, so we can printf */
    printf(":%s:%d\n", buf, res);
    if (buf[0] == '\0')
        flag = 1;
}

It prints out value of res = 0.

Actually I want to read data character-by-character to perform calculations and take further decision. Is there another way of doing this?

Note: Is there good study material (code) for serial port programming on Linux?

How can I read from the Linux serial port...

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

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

发布评论

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

评论(2

拧巴小姐 2024-09-15 04:49:56

首先,查看/proc/tty/driver/serial,看看一切都设置正确(即,您看到了应该看到的信号)。然后,看看 termios(3) 的手册页,你可能会对 VMIN 和 VTIME 解释感兴趣。

First, take a look at /proc/tty/driver/serial to see that everything is set up correctly (i.e., you see the signals you should see). Then, have a look at the manual page for termios(3), you may be interested in the VMIN and VTIME explanation.

萌逼全场 2024-09-15 04:49:54

这是一个很好的指南:POSIX 操作系统的串行编程指南

读取调用可能返回无数据,并且 errno 设置为 EAGAIN。如果您期望数据到达,您需要检查返回值并循环再次读取。

This is a good guide: Serial Programming Guide for POSIX Operating Systems

The read call may return with no data and errno set to EAGAIN. You need to check the return value and loop around to read again if you're expecting data to arrive.

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