linux 使用usb转串口模块并读串口数据

时间:2021-12-21 07:49:05

买了一个无线通信模块,在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