当前位置: 首页 > 知识库问答 >
问题:

如何在C语言中读/写串口数据,打开函数的问题

苏法
2023-03-14
  fd = open("/dev/tty.usbserial-AH02OC4V", O_RDWR | O_NOCTTY);
  printf("fd opened as %i\n", fd);
  printf("Serial Open Passed!\n");
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/ioctl.h>

#define DEBUG 1

int main(int argc, char *argv[])
{
  printf("start\n");
  int fd, n, i;
  char buf[64] = "temp text";
  struct termios toptions;
  printf("start\n");
  /* open serial port */
  fd = open("/dev/tty.usbserial-AH02OC4V", O_RDWR | O_NOCTTY);
  printf("fd opened as %i\n", fd);
  printf("Serial Open Passed!\n");
  /* wait for the Arduino to reboot */
  usleep(3500000);

  /* get current serial port settings */
  tcgetattr(fd, &toptions);
  /* set 9600 baud both ways */
  cfsetispeed(&toptions, B9600);
  cfsetospeed(&toptions, B9600);
  /* 8 bits, no parity, no stop bits */
  toptions.c_cflag &= ~PARENB;
  toptions.c_cflag &= ~CSTOPB;
  toptions.c_cflag &= ~CSIZE;
  toptions.c_cflag |= CS8;
  /* Canonical mode */
  toptions.c_lflag |= ICANON;
  /* commit the serial port settings */
  tcsetattr(fd, TCSANOW, &toptions);

  /* Send byte to trigger Arduino to send string back */
  write(fd, "0", 1);
  /* Receive string from Arduino */
  n = read(fd, buf, 64);
  /* insert terminating zero in the string */
  buf[n] = 0;

  printf("%i bytes read, buffer contains: %s\n", n, buf);

  if(DEBUG)
    {
      printf("Printing individual characters in buf as integers...\n\n");
      for(i=0; i<n; i++)
    {
      printf("Byte %i:%i, ",i+1, (int)buf[i]);
    }
      printf("\n");
    }

  return 0;
}
void loop()
{
  if (Serial.available() > 0)
  {
    Serial.println("Hello");
    Serial.read();
  }
}

共有1个答案

汪泓
2023-03-14

就open()而言,您的代码在我看来是正确的。

我相信open()会阻塞,直到另一端可用为止。它在等待司机承认它可以说话,在此之前,它耐心地(顽固地)等待。请确保正在连接的设备已启动并准备好进行通信。

您还可以尝试标记O_NONBLOCK,它可以设置errno,以便检查open()失败的原因。

 类似资料:
  • 问题内容: 我对读取和写入串行端口有些困惑。我在Linux中有一个使用FTDI USB串行设备转换器驱动程序的USB设备。当我插入它时,它将创建:/ dev / ttyUSB1。 我认为用C打开和读取它很简单。我知道波特率和奇偶校验信息,但是似乎没有标准吗? 我是否缺少某些东西,或者有人可以指出正确的方向? 问题答案: 您必须调用一个从获得。你不能零了,配置它,然后将用。如果使用归零方法,则会遇到

  • 我想把温度数据从arduino保存到mysql数据库。Arduino通过USB串行端口连接MySQL数据库。为了将数据从Arduino保存到MySQL,我使用C编程语言读取Arduino串行端口。问题是,数据存储到mysql中与显示器串行Arduino上显示的数据不同。 串行监视器中来自Arduino的数据如下: 在C中从串行端口读取的程序有:

  • C 语言中的字符串虽然不是一种独立的数据类型,但是这并不影响其重要地位,所以在 C 语言中会有一些专门针对字符串的函数。 1. 字符串函数 字符串函数是专门用来进行字符串操作的。C 语言提供了一个标准的函数库 string.h 。在这个函数库中大致存在了 22 个字符串的函数。我们这里所介绍的字符串函数是来自于这个标准函数库中比较常用的的一部分函数。除了这个函数库,还会有第三方的函数库提供的字符串

  • 本文向大家介绍C语言中编写可变参数函数,包括了C语言中编写可变参数函数的使用技巧和注意事项,需要的朋友参考一下 通过stdarg.h头文件为函数提供了定义可变参数列表的能力。声明一个可变参数的函数类似: void f1(int n,...); 其中n表示参数列表个数,而用省略号来表示未知参数列表。stdarg.h中提供了一个va_list类型,用于存放参数。一个大概的使用过程类似: 看一个求和的例

  • 1. 函数的定义 程序是由一个个函数组成的。我们之前虽然没有正式介绍函数,但是我们早已经开始使用函数了。因为离开了函数,我们的程序没有办法正常的工作。只不过我们使用的是 C 语言内置的标准函数库。 那么函数是什么? 函数是由一组语句组成完成至少一个特定任务的语句的集合。在 C 语言中,我们必须要包含一个函数,就是我们最开始介绍的 mian 函数。 2. 为什么需要函数? 函数帮助我们可以减少代码的

  • 在 Go 语言开篇中我们已经知道,Go 语言与 C 语言之间有着千丝万缕的关系,甚至被称之为 21 世纪的C语言。 所以在 Go 与 C 语言互操作方面,Go 更是提供了强大的支持。尤其是在 Go 中使用 C,你甚至可以直接在 Go 源文件中编写 C 代码,这是其他语言所无法望其项背的。 格式: 在 import "C" 之前通过单行注释或者通过多行注释编写C语言代码 在 import "C" 之