Linux设备与STM32 USB串口通讯

保证STM32 USB串口虚拟成功。

lsusb  

如果STM32单片机通过USB连接到计算机,你应该能够在输出中看到类似以下的行:

Bus XXX Device YYY: ID 0483:5740 STMicroelectronics 

查看USB更多信息

sudo dmesg | grep tty

增加了下面类似信息:

cdc_acm 1-1.4:1.0: ttyACM0: USB ACM device

好的,你的STM32设备已经被识别为一个USB ACM(Abstract Control Model)设备,对应的串口设备文件是 ttyACM0。这是一种常见的在Linux系统上与STM32通信的方式。

现在,你可以使用 ttyACM0 这个设备文件进行串口通信。例如,你可以使用 minicomscreen 等终端工具,或者通过编程语言(如C语言)来与STM32进行通信。

以下是一个简单的C语言代码示例,用于打开 ttyACM0 设备文件并进行读写操作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>

int open_serial_port(const char *port) {
    int fd = open(port, O_RDWR | O_NOCTTY);

    if (fd == -1) {
        perror("open_serial_port: Unable to open port");
    } else {
        fcntl(fd, F_SETFL, 0);
    }

    return fd;
}

void configure_serial_port(int fd) {
    struct termios tty;
    memset(&tty, 0, sizeof(tty));

    if (tcgetattr(fd, &tty) != 0) {
        perror("configure_serial_port: Error from tcgetattr");
        return;
    }

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

    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_oflag &= ~OPOST;

    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 5;               // 串口超时时间0.5s
    tty.c_iflag &= ~(INLCR|ICRNL);     // Linux会把0x0d替换成0x0a,加上这个就不会了    
    tty.c_iflag &= ~(IXON);            // 解决缺少了0x11 0x13问题    
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        perror("configure_serial_port: Error from tcsetattr");
    }
}

int main() {
    const char *port = "/dev/ttyACM0"; // 你的串口设备文件

    int serial_fd = open_serial_port(port);
    if (serial_fd == -1) {
        fprintf(stderr, "Failed to open serial port\n");
        return 1;
    }

    configure_serial_port(serial_fd);

    char buffer[256];
    memset(buffer, 0, sizeof(buffer));

    while (1) {
        int n = read(serial_fd, buffer, sizeof(buffer) - 1);

        if (n > 0) {
            buffer[n] = '\0';
            printf("Received: %s", buffer);
        } else if (n < 0) {
            perror("Error reading from serial port");
            break;
        }

        // 写入串口数据
        // write(serial_fd, "Hello, World!\n", 13);

        usleep(100000); // 100ms延迟
    }

    close(serial_fd);

    return 0;
}

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在C语言中,可以使用串口通讯协议来实现Linuxstm32之间的通讯。 首先,需要在Linux中配置串口,可以使用以下命令: ``` sudo stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb ``` 其中,`/dev/ttyUSB0`是串口设备的路径,`115200`是波特率,`cs8`表示8位数据位,`-cstopb`表示1位停止位,`-parenb`表示无校验位。 然后,在C语言中,可以使用串口通讯库,如`termios`库,来进行串口通讯。 以下是一个简单的示例代码: ```c #include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <termios.h> int main() { int fd; struct termios options; char buffer[255]; // 打开串口设备 fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY); if (fd == -1) { printf("Open serial port failed!\n"); return -1; } // 配置串口 memset(&options, 0, sizeof(options)); cfsetispeed(&options, B115200); cfsetospeed(&options, B115200); options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); options.c_iflag &= ~(IXON | IXOFF | IXANY); options.c_iflag &= ~(INLCR | IGNCR | ICRNL); options.c_oflag &= ~(ONLCR | OCRNL); options.c_cc[VTIME] = 0; options.c_cc[VMIN] = 1; tcsetattr(fd, TCSANOW, &options); // 发送数据 write(fd, "Hello, STM32!", 14); // 接收数据 memset(buffer, 0, sizeof(buffer)); read(fd, buffer, sizeof(buffer)); printf("Received: %s\n", buffer); // 关闭串口设备 close(fd); return 0; } ``` 在上述示例代码中,首先使用`open`函数打开串口设备,然后使用`tcsetattr`函数配置串口参数,包括波特率、数据位、停止位、校验位等等。接着,使用`write`函数发送数据,使用`read`函数接收数据。最后,使用`close`函数关闭串口设备。 需要注意的是,在Linux中,串口设备的路径可能不同,需要根据实际情况进行修改。另外,在使用串口通讯时,需要保证Linuxstm32之间的串口参数一致,否则可能会导致通讯失败。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

laocui1

你的鼓励是我创作的最大动了

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值