小编Emb*_*bed的帖子

用C语言初始化、读写linux串口设备

我正在开发一个新项目,我想与连接到我的 debian 机器的 FTDI 建立连接。我打算用 C 编写代码,而不是 C++。我的问题就在这里。我找到的所有示例都不完整,或者是为 c++ 编译器而不是 GCC 编译器制作的。

目标是与连接到 FTDI 的微控制器进行通信。为了调试,我想开始构建一个 Linux 应用程序,它能够:

  • 在启动时使用 ttyUSB1 初始化串行连接
  • 发送一个字符串
  • PC接收到字符串后显示
  • 将通信保存到 .txt 文件

是否有任何示例代码或教程可以实现此目的?

如果我成功了,我会将代码放在这里,以便新观众可以使用它!

编辑:

就像我说的,如果我有代码,我会发布代码,这对我有用:

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

#define MODEM "/dev/ttyUSB0"
#define BAUDRATE B115200    

int main(int argc,char** argv)
{   
    struct termios tio;
    struct termios stdio;
    struct termios old_stdio;
    int tty_fd, flags;
    unsigned char c='D';
    tcgetattr(STDOUT_FILENO,&old_stdio);
    printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
    memset(&stdio,0,sizeof(stdio));
    stdio.c_iflag=0;
    stdio.c_oflag=0;
    stdio.c_cflag=0;
    stdio.c_lflag=0; …
Run Code Online (Sandbox Code Playgroud)

c linux serial-port tty

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1

linux ×1

serial-port ×1

tty ×1