串口232接收数据代码,linux

#include <iostream>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
    int serial_port = open("/dev/ttyS0", O_RDONLY);

    if (serial_port < 0) {
        std::cerr << "Failed to open serial port" << std::endl;
        return -1;
    }

    struct termios tty;
    struct termios tty_old;
    memset(&tty, 0, sizeof(tty));

    if (tcgetattr(serial_port, &tty) != 0) {
        std::cerr << "Failed to get serial port attributes" << std::endl;
        return -1;
    }

    tty_old = tty;
//波特率设置为9600,可以根据具体需求更改
    cfsetospeed(&tty, (speed_t)B9600);
    cfsetispeed(&tty, (speed_t)B9600);

    tty.c_cflag |= (CLOCAL | CREAD);
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;
    tty.c_cflag &= ~PARENB;
    tty.c_cflag &= ~CSTOPB;
    tty.c_cflag &= ~CRTSCTS;

    tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    tty.c_iflag &= ~(IXON | IXOFF | IXANY);
    tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);

    tty.c_oflag &= ~OPOST;

    tty.c_cc[VTIME] = 10;
    tty.c_cc[VMIN] = 0;

    tcsetattr(serial_port, TCSANOW, &tty);

    while (true) {
        char buffer[256];
        int n = read(serial_port, buffer, sizeof(buffer));
        if (n > 0) {
            buffer[n] = '\0';
            std::cout << buffer << std::endl;
        }
    }

    tcsetattr(serial_port, TCSANOW, &tty_old);
    close(serial_port);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用 Linux 系统下的串口通信库来实现发送数据给串口并接收返回值的功能,例如使用 termios 库。以下是一个简单的 C++ 代码示例: ```cpp #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <termios.h> int main() { int fd; char data[] = "Hello, world!"; // 要发送的数据 char buf[1024]; // 用于接收返回值的缓冲区 struct termios opt; fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); // 打开串口设备 if (fd < 0) { perror("open serial port fail\n"); return -1; } memset(&opt, 0, sizeof(opt)); tcgetattr(fd, &opt); // 获取当前串口配置 cfsetispeed(&opt, B9600); // 设置波特率 cfsetospeed(&opt, B9600); opt.c_cflag |= CLOCAL | CREAD; // 本地连接和接收使能 opt.c_cflag &= ~CSIZE; // 清除数据位设置 opt.c_cflag |= CS8; // 设置数据位为8位 opt.c_cflag &= ~PARENB; // 清除校验位设置 opt.c_cflag &= ~CSTOPB; // 设置停止位为1位 opt.c_iflag &= ~(IXON | IXOFF | IXANY); // 禁用软件流控制 opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // 取消标准输入模式 opt.c_cc[VTIME] = 0; // 设置读取超时时间 opt.c_cc[VMIN] = 1; // 设置读取最小字符数 tcsetattr(fd, TCSANOW, &opt); // 设置新的串口配置 write(fd, data, strlen(data)); // 发送数据 int len = read(fd, buf, sizeof(buf)); // 接收返回值 buf[len] = '\0'; printf("Received: %s\n", buf); close(fd); // 关闭串口设备 return 0; } ``` 这个例子中,我们打开了 `/dev/ttyS0` 设备,也就是第一个串口,然后设置了波特率为 9600,数据位为 8 位,无校验位,停止位为 1 位,禁用了软件流控制和标准输入模式,并设置了读取超时时间和最小字符数。然后我们发送了一串数据,接着读取串口返回的数据并打印出来。最后关闭了串口设备。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值