ubuntu下串口发送或者接收(c语言实现)minicom调试

2023-05-16

关于串口的知识这里就不累赘了,看着多又烦,搞这个的都懂串口,不多废话了!!

进入正题!!

1.选择合适的usb串口模块

某宝很多这种模块,有各种型号的(例如ch340),这里要说说了 选哪种合适

打开ubutnu的命令行

$lsmod | grep usbserial

usbserial              45056  2 cp210x,ch341

查看自己电脑有那些驱动比如我电脑就可以买上面型号的模块ch340的也可以

$ dmesg

输出相关信息,部分信息如下:
  [  429.184170] usb 6-1: new full-speed USB device number 3 using uhci_hcd
[  429.345937] usb 6-1: New USB device found, idVendor=1a86, idProduct=7523
[  429.345944] usb 6-1: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[  429.345948] usb 6-1: Product: USB2.0-Ser!
[  429.368123] usbcore: registered new interface driver usbserial
[  429.368396] usbcore: registered new interface driver usbserial_generic
[  429.368875] usbserial: USB Serial support registered for generic
[  429.374125] usbcore: registered new interface driver ch341
[  429.374143] usbserial: USB Serial support registered for ch341-uart
[  429.374165] ch341 6-1:1.0: ch341-uart converter detected
[  429.386851] usb 6-1: ch341-uart converter now attached to ttyUSB0
ID信息与lsusb检测到的一致,ch341-uart converter now attached to ttyUSB0  成功驱动,端口为ttyUSB0!!(很重要,后面的程序中需要,还有minicom也需要设置)

2.下载minicom

$ sudo apt-get install minicom

等待安装成功,然后用sudo minicom -s 设置minicom的端口对应到ttyUSB0(对应使用dmesg命令看到的信息)。
出现配置菜单:

选择“Serial port setup”,出现串口配置菜单:

输入A,修改serial device 由/Dev/tty0修改为/dev/ttyusb0,波特率等修改为115200 8N1。

enter 后 save setup as dfl

Welcome to minicom 2.7

OPTIONS: I18n 
Compiled on Feb  7 2016, 13:37:27.
Port /dev/ttyUSB0, 14:06:56

Press CTRL-A Z for help on special keys

上面就是命令模式了

输入你想发送的数据就可以了,当然也可以在上面收到串口发来数据!

但是!!!可能你看不到你发送的数据,(ctrl A 后按下E选择回显)这样你就可以看到你发送的数据了!!!

ctrl A 后按下Q是退出minicom,打开minicom就在命令行下输入minicom就可以打开了

3.串口发送程序


/

//Author: CSDN Maizidian

//Serial communication under ubutnu, data sending and receiving

/

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <errno.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <termios.h>

#define BAUDRATE B115200 ///Baud rate : 115200

#define DEVICE "/dev/ttyUSB0"//Set your port number

int nFd = 0;

struct termios stNew;

struct termios stOld;



//Open Port & Set Port

int SerialInit()

{

    nFd = open(DEVICE, O_RDWR|O_NOCTTY|O_NDELAY);

    if(-1 == nFd)

    {

        perror("Open Serial Port Error!\n");

        return -1;

    }

    if( (fcntl(nFd, F_SETFL, 0)) < 0 )

    {

        perror("Fcntl F_SETFL Error!\n");

        return -1;

    }

    if(tcgetattr(nFd, &stOld) != 0)

    {

        perror("tcgetattr error!\n");

        return -1;

    }



    stNew = stOld;

    cfmakeraw(&stNew);//Set the terminal to raw mode, in which all input data is processed in bytes



    //set speed

    cfsetispeed(&stNew, BAUDRATE);//115200

    cfsetospeed(&stNew, BAUDRATE);



    //set databits

    stNew.c_cflag |= (CLOCAL|CREAD);

    stNew.c_cflag &= ~CSIZE;

    stNew.c_cflag |= CS8;



    //set parity

stNew.c_cflag &= ~PARENB;

stNew.c_iflag &= ~INNPCK;



    //set stopbits

stNew.c_cflag &= ~CSTOPB;

    stNew.c_cc[VTIME]=0; //Specify the minimum number of characters to be read

    stNew.c_cc[VMIN]=1; //Specify the waiting time for reading the first character, the unit of time is n*100ms

                //Assuming VTIME=0 is set, the read() operation is blocked indefinitely when no character is input

    tcflush(nFd,TCIFLUSH); //Clear the terminal's unfinished input/output requests and data.

if( tcsetattr(nFd,TCSANOW,&stNew) != 0 )

    {

        perror("tcsetattr Error!\n");

        return -1;

    }



    return nFd;

}



int main(int argc, char **argv)

{ int i;

    int nRet = 0;

    char *sendmsg="Wheat";

    char buf[5];

    if( SerialInit() == -1 )

    {

        perror("SerialInit Error!\n");

        return -1;

    }

    bzero(buf, SIZE);

    while(1)

    { sleep(1);

        write(nFd,sendmsg,sizeof(sendmsg));//Send data to serial port

        printf("%s\n",sendmsg);



        /serial port receiving part

        nRet = read(nFd, buf, SIZE);

        if(-1 == nRet)

        {

            perror("Read Data Error!\n");

            break;

        }

        if(0 < nRet)

        {

            buf[nRet] = 0;

            printf("Recv Data: %s\n", buf);

        }

        /

    }

    close(nFd);

    return 0;

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ubuntu下串口发送或者接收(c语言实现)minicom调试 的相关文章

随机推荐

  • prometheus和node_exporter部署

    监控体系 前面一篇文章对prometheus作了简单的入门了解 xff0c 在安装prometheus和node exporter之前先对监控体系做一下梳理 xff0c 更直观的讲可以分为iaas paas xff0c saas三个维度来说
  • ROS的一些基础的知识,自用

    零散小知识 先参考这个博客 再参考这个博客总结的知识点 查看当前所以发布的话题名 rostopic list 让ros一直调用回调函数 xff0c ros spin如下的代码无效函数 ros span class token operato
  • TMS320F28377X芯片can通信心得笔记(2)——流程细节

    一 xff0c 前言 补充上一篇文章 TMS320F28377X芯片can通信心得笔记 xff0c 之前一直找不到清晰又压缩的方式把图片缩小 二 xff0c 补充流程如图片和部分代码 带颜色 和带 号部分为中断配置 1 CAN配置部分代码
  • IDEA创建一个servlet项目

    1 File gt project gt java project 选中web application 如果选择java Enterprise可以免去后期导入servlet api jar包和配置Tomcat xff0c 但是需要在proj
  • STM32系列单片机的标准外设库、HAL库和LL库的区别和介绍

    本文转载自CSDN博客 xff1a ZCShouEXP xff0c 点击此处跳转至原博客 STM32 Embedded Software 工作以来一直使用意法半导体 xff08 ST xff09 的STM32系列MCU xff0c ST为开
  • Github的wiki编写

    全球最大的wiki xff0c 应该就是维基百科吧 xff0c 其实百度百科 xff0c csdn写博客等等也是wiki 不久前自己接触了Github wiki的编写 xff0c 因此做个记录吧 模式一般选择Markdown xff0c 因
  • EGOPlanner—Prometheus代码阅读笔记汇总

    Prometheusv2中的EGOSwarm代码阅读笔记 xff0c 个人学习用 目前还有部分代码没看明白 xff0c 欢迎大家交流 xff01 主要参考资料 xff1a Fast Planner 代码解读参考资料整理 EGO Swarm代
  • UAVControl—Prometheus代码阅读笔记【二】——节点实现

    Prometheusv2中的uav control代码阅读笔记 xff0c 个人学习用 内容正在补充中 xff0c 欢迎大家交流 xff01 代码来源 xff1a https github com amov lab Prometheus 二
  • 记录Ubuntu server不能输入中文的全记录--没有解决(最后靠装的桌面版+x11vnc解决)

    1 网上查到的资料都是针对桌面版本的 xff0c 所以先安装了一个vnc桌面 xff0c 至少右击可以看到包含了系统属性 参考链接 xff1a https blog csdn net wayway0554 article details 8
  • int 和 Integer的区别

    int 和 Integer的区别 分析intIntegerint 与 Integer 的区别相同值下的 int 和 Integer 的比较结果 分析 int int 是java的基本数据类型 Integer Integer 继承了Objec
  • C/C++ 使用信号量控制线程运行顺序

    span class token macro property span class token directive keyword include span span class token string lt stdio h gt sp
  • C++ 问题整理

    说一下C 43 43 和C的区别 设计思想上 xff1a C 43 43 是面向对象的语言 xff0c 而 C 是面向过程的结构化编程语言 C 43 43 具有封装 继承和多态三种特性 C 43 43 支持范式编程 xff0c 比如模板类
  • 职业向导 - 面试题目汇总(嵌入式篇)

    自己踩的坑 存储类操作系统类总线类工具实用类基本电路知识类计算机语言类 存储类 SDRAM SRAM DRAM PSRAM xff0c NOR Flash Nand Flash不同 TablesChineseFull NameSDRAMab
  • ROS笔记(一)xxx.launch文件详解

    ROS笔记 一 xxx launch文件详解 launch文件是ROS中用于同时启动多个节点的重要文件 在大型的ROS项目中使用频繁 所以掌握其主要元素与属性对ROS系统的应用至关重要 xff1a launch标签 元素 说明launch拓
  • 【OpenCV/aruco】第一个AR Demo-二维图片

    说在前面 操作系统 xff1a win10 vs 版本 xff1a 2017 opencv版本 xff1a 4 0 1 opencv contrb版本 xff1a 4 0 1 接上篇 xff1a OpenCV aruco 校准相机 Came
  • 学C++就学服务端,先把apue和unp两卷看了,接着libevent,出来找工作应该没问题

    学C 43 43 就学服务端 xff0c 先把apue和unp两卷看了 xff0c 接着libevent xff0c 出来找工作应该没问题
  • 详解双闭环控制算法(理论篇)

    什么是双闭环控制 双闭环控制算法是一种先进的控制方法 xff0c 它针对控制系统中存在的多种干扰和变化进行优化 xff0c 提高系统的稳定性 精度和响应速度 双闭环控制算法由内环和外环组成 xff0c 分别控制系统的快速响应和系统稳定 内环
  • rtthread中使用sqlite

    一 简介 SQLite是一款嵌入式 轻量级的关系型数据库系统 xff0c 它的设计目标是嵌入式的 xff0c 而且目前已经在很多嵌入式产品中使用了它 它占用资源非常的低 xff0c 在嵌入式设备中 xff0c 可能只需要几百K的内存就够了
  • Pycharm报错:ERROR: Command "python setup.py egg_info" failed with error code 1

    今天在调试程序的时候 xff0c Pycharm报了这个错 xff0c 然后自己弄了半天 最后发现其实原因在一个很简单的地方 xff0c 我却没发现 下面开始介绍怎么处理这个错误 xff0c 只是有可能的解决方法 xff0c 不一定适合所有
  • ubuntu下串口发送或者接收(c语言实现)minicom调试

    关于串口的知识这里就不累赘了 xff0c 看着多又烦 xff0c 搞这个的都懂串口 xff0c 不多废话了 xff01 xff01 进入正题 xff01 xff01 1 选择合适的usb串口模块 某宝很多这种模块 xff0c 有各种型号的