买了一个无线通信模块,在windows下还要装驱动才能读。在windows下测试无线模块没问题后,在ubantu14.2中测试怎么读串口。步骤如下:

1.查看看系统信息

dmesg | tail -f 

输出如下:

[12812.940613] usb 1-9: New USB device found, idVendor=10c4, idProduct=ea60
[12812.940621] usb 1-9: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[12812.940627] usb 1-9: Product: CP2102 USB to UART Bridge Controller
[12812.940631] usb 1-9: Manufacturer: Silicon Labs
[12812.940634] usb 1-9: SerialNumber: 0001
[12812.941748] cp210x 1-9:1.0: cp210x converter detected
[12813.106603] usb 1-9: reset full-speed USB device number 5 using xhci_hcd
[12813.123424] xhci_hcd 0000:00:14.0: xHCI xhci_drop_endpoint called with disabled ep ffff88023eec6fc0
[12813.123433] xhci_hcd 0000:00:14.0: xHCI xhci_drop_endpoint called with disabled ep ffff88023eec6f80
[12813.124130] usb 1-9: cp210x converter now attached to ttyUSB0

该模块为CP2102,最后一句表示设备连接到了ttyUSB0

2.查看模块装载的情况

dmesg | grep ttyUSB0

输出
[12796.093492] usb 1-9: cp210x converter now attached to ttyUSB0
[12810.605949] cp210x ttyUSB0: cp210x converter now disconnected from ttyUSB0
[12813.124130] usb 1-9: cp210x converter now attached to ttyUSB0
表示已经装载了模块。

3.安装minicom

sudo apt-get install minicom

4.配置minicom

配置minicom

sudo minicom -s

参考博客
http://www.cnblogs.com/shishiteng/p/5801826.html
选择serial port setup
只要配置三项就行了
按E键 设置 Bps/Par/Bit 为9600(根据个人的波特率设置)
按A键,设置Serial Device为: /dev/ttyUSB0
按F键,设置Hardware Flow Control 为No

5.保存收到的数据到log文件和查看串口数据

sudo minicom
这里提示按”Ctrl+A”,再按”Z”键进入主配置目录
(1)保存收到的数据到log文件
按L进入
输入输出文件的保存路径
参考
http://www.2cto.com/os/201111/110568.html
(2)查看串口数据
启动发送就有数据显示了,并能在文件中查看数据

6.使用程序打开串口

参考
http://blog.csdn.net/specialshoot/article/details/50707965
使用minicom能够读到数据后,新建以下工程,内容如下

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include<sys/ioctl.h>
int set_opt(int,int,int,char,int);
int open_port(char* uartname)
{int fd = open(uartname, O_RDWR|O_NOCTTY|O_NONBLOCK);if (-1 == fd){perror("Can't Open Serial Port");return(-1);}/*恢复串口为阻塞状态*/if(fcntl(fd, F_SETFL, 0)<0){printf("fcntl failed!\n");}else{printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));}/*测试是否为终端设备*/if(isatty(STDIN_FILENO)==0){printf("standard input is not a terminal device\n");}else{printf("isatty success!\n");}printf("fd-open=%d\n",fd);return fd;
}
int main(int argc, char* argv[])
{printf("hello,run ok\n");int fd=1, read_num = 0;char buffer[1024];memset(buffer, 0, 1024);char* uartname="/dev/ttyUSB0";
//    if(argc < 2)
//    {//        printf("usage: ./uarttest /dev/ttyUSB0 \n");
//        return 0;
//    }if((fd=open_port(uartname))<0){printf("open %s is failed\n",uartname);return 0;}else{set_opt(fd, 115200, 8, 'N', 1);printf("set_opt fd=%d\n",fd);while(1){char c=getchar();printf("%c",c);if('q'==c){return 0;}sleep(1);memset(buffer, 0, 1024);read_num = read(fd, buffer, 1024);printf("read_num=%d\n",read_num);if(read_num>0){printf("rev char=%s\n",buffer);int num=atoi(buffer);printf("rev int=%d\n",num);}else{printf("read error\n");}}fd=close(fd);}return 0;
}int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{struct termios newtio,oldtio;if  ( tcgetattr( fd,&oldtio)  !=  0) {perror("SetupSerial 1");return -1;}bzero( &newtio, sizeof( newtio ) );newtio.c_cflag  |=  CLOCAL | CREAD;newtio.c_cflag &= ~CSIZE;switch( nBits ){case 7:newtio.c_cflag |= CS7;break;case 8:newtio.c_cflag |= CS8;break;}switch( nEvent ){case 'O':newtio.c_cflag |= PARENB;newtio.c_cflag |= PARODD;newtio.c_iflag |= (INPCK | ISTRIP);break;case 'E':newtio.c_iflag |= (INPCK | ISTRIP);newtio.c_cflag |= PARENB;newtio.c_cflag &= ~PARODD;break;case 'N':newtio.c_cflag &= ~PARENB;break;}switch( nSpeed ){case 2400:cfsetispeed(&newtio, B2400);cfsetospeed(&newtio, B2400);break;case 4800:cfsetispeed(&newtio, B4800);cfsetospeed(&newtio, B4800);break;case 9600:cfsetispeed(&newtio, B9600);cfsetospeed(&newtio, B9600);break;case 115200:cfsetispeed(&newtio, B115200);cfsetospeed(&newtio, B115200);break;case 460800:cfsetispeed(&newtio, B460800);cfsetospeed(&newtio, B460800);break;case 921600:printf("B921600\n");cfsetispeed(&newtio, B921600);cfsetospeed(&newtio, B921600);break;default:cfsetispeed(&newtio, B9600);cfsetospeed(&newtio, B9600);break;}if( nStop == 1 )newtio.c_cflag &=  ~CSTOPB;else if ( nStop == 2 )newtio.c_cflag |=  CSTOPB;newtio.c_cc[VTIME]  = 0;newtio.c_cc[VMIN] = 0;tcflush(fd,TCIFLUSH);if((tcsetattr(fd,TCSANOW,&newtio))!=0){perror("com set error");return -1;}//printf("set done!\n\r");return 0;
}

编译运行提示
Can’t Open Serial Port: Permission denied
解决方法:
改变usb设备的执行权限# sudo chmod 777 /dev/ttyUSB0

linux 使用usb转串口模块并读串口数据相关推荐

  1. linux 下串口转usb不能发送数据包,Linux ,USB转串口驱动,没法读到数据

    Linux ,USB转串口驱动,无法读到数据 usb 1-1.1: new full-speed USB device number 5 using ehci-pci usb 1-1.1: New U ...

  2. ttyUSB 串口编程 Linux下 USB转串口

    转:http://www.360doc.com/content/12/0222/15/1317564_188649565.shtml 在Linux下对设备的操作方法与对文件的操作方法是一样的,因此对串 ...

  3. linux下usb转串口驱动分析

    linux下usb转串口驱动分析 分类: linux driver 2012-06-08 15:11 456人阅读 评论(0) 收藏 举报 linux struct interface returni ...

  4. Android OTG之USB转串口模块通讯

    飞哥语录:要有自己做事的原则. 1.背景简介 我们公司开发了一款室内机平板APP应用,要求平板能去控制智能门锁.等其他智能设备,智能门锁不是我们公司开发的,与我们公司属于合作关系. 2.分析及实现思路 ...

  5. linux内核 usb转串口,Linux 使用usb转串口作为调试串口

    芯片串口用来做数据通信使用,需要调试串口时则使用usb转串口debug用. 内核需要几个配置的地方: 1) -> Device Drivers |       -> USB support ...

  6. 快速上手CH340N电路设计(CH340N USB转串口模块 USB Type-C接口 CH340系列芯片讲解)

    一.上模块 二.功能分析 l  芯片:CH340N l  输入接口:USB.TYPE-C l  输出接口:TTL(5V\3.3V\GND\TX\RX) l  指示灯:电源.TX.RX 三.CH340x ...

  7. linux内核 usb转串口,求助:usb转serial串口设备在linux内核中创建及生成tty设备的改写...

    当把一个usb转serial串口设备插到linux系统上时,系统自动识别设备后会把设备和/dev目录下设备节点相关联,但是默认的关联是从ttyUSB0开始,依次ttyUSB1,ttyUSB2..... ...

  8. linux驱动 usb转串口ch344 改变读取缓冲区大小

    开发环境 核心板:IMX6 内核版本:linux4.1.5 问题 通过USB扩展出来的串口接收数据会出现截断现象,而且每次截断的大小都一样.而核心板提供的串口UART就没有这个现象. 核心板自带串口正 ...

  9. Arch LInux 使用USB转串口(CH340)

    一.CH340/CH341 1. 卸载系统已有驱动 查看系统版本: uname -r 这里我是 5.17.1-arch1-1 版本,进入内核源码目录: cd /lib/modules/5.17.1-a ...

最新文章

  1. HEVC/H.265 的未来必须是使用并行处理(OpenCL?) OpenCV和OpenCL区别
  2. Intellij IDEA debug模式下项目启动慢/无法启动的事件解决过程记录
  3. java注释风格 与javadoc
  4. iOS UIlabel内容之后添加全文/展开
  5. 关于数据库的增删改查
  6. iOS开发使用半透明模糊效果方法整理
  7. 微服务和分布式学习专栏
  8. 测者的测试技术手册:揭开java method的一个秘密--巨型函数
  9. Pytest高级进阶之Fixture
  10. Oracle 多表 连接 顺序 与 性能关系 测试
  11. HDFS报错:there are 15871 missing blocks,the following files may be corrupted
  12. vux在ISO中异常 this.$vux.confirm.show
  13. JMeter测试实例
  14. 骨骼动画编辑器Spine的纹理打包器(texture packer)
  15. 计算机效果图线稿的制作方法,如何只用PS将线稿图变成高大上的效果图?
  16. dw相对路径怎么改_Dreamweaver绝对路径和相对路径
  17. Python打字练习小游戏源代码
  18. mybatis获取map中的key和value
  19. Vue - 判断访问网页客户端设备是手机移动端还是 PC 电脑端(判断设备类型是否是移动端手机)
  20. 贪心背后的故事Codeforces 995B(Suit and Tie)

热门文章

  1. 基于椭球 磁补偿 matlab,基于椭球拟合的三轴磁传感器误差补偿方法.pdf
  2. 特征值的几何重复度不大于代数重复度
  3. nas 软件 性能测试,理论读写性能测试
  4. 服务器无线信号差怎么办,wifi隔墙信号不好怎么办
  5. SAMSUNG三星70年之崛起与ECS精英的销售“铁三角”
  6. 华为近场通讯nfc在哪里打开_华为手机怎么使用NFC功能?华为手机使用NFC交通卡功能教程...
  7. Intel新CEO敲定,斯旺终”转正“ 1
  8. git时出现! [rejected] master -> master (non-fast-forward)解决方法
  9. flash 小游戏大全
  10. matplotlib的plt.ion()没用/不能交互