ubuntu下串口发送或者接收minicom调试

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

进入正题!!

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.串口发送程序

//

//作者:CSDN 麦子点点

//ubutnu下串口通信,数据收发

//

#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"//设置你的端口号

int nFd = 0;

struct termios stNew;

struct termios stOld;

//Open Port & Set Port

int SerialInit()

{undefined

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

    if(-1 == nFd)

    {undefined

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

        return -1;

    }

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

    {undefined

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

        return -1;

    }

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

    {undefined

        perror("tcgetattr error!\n");

        return -1;

    }

    stNew = stOld;

    cfmakeraw(&stNew);//将终端设置为原始模式,该模式下全部的输入数据以字节为单位被处理

    //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 &= ~INPCK;

    //set stopbits

stNew.c_cflag &= ~CSTOPB;

    stNew.c_cc[VTIME]=0;    //指定所要读取字符的最小数量

    stNew.c_cc[VMIN]=1; //指定读取第一个字符的等待时间,时间的单位为n*100ms

                //假设设置VTIME=0,则无字符输入时read()操作无限期的堵塞

    tcflush(nFd,TCIFLUSH);  //清空终端未完毕的输入/输出请求及数据。

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

    {undefined

        perror("tcsetattr Error!\n");

        return -1;

    }

    return nFd;

}

int main(int argc, char **argv)

{   int i;

    int nRet = 0;

    char *sendmsg="麦子";

    int SIZE=sizeof(sendmsg);

    char buf[SIZE];

    if( SerialInit() == -1 )

    {undefined

        perror("SerialInit Error!\n");

        return -1;

    }

    bzero(buf, SIZE);

    while(1)

    {   sleep(1);

        write(nFd,sendmsg,sizeof(sendmsg));//向串口发送数据

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

        /串口接收部分

        nRet = read(nFd, buf, SIZE);

        if(-1 == nRet)

        {undefined

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

            break;

        }

        if(0 < nRet)

        {undefined

            buf[nRet] = 0;

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

        }

        

    }

    close(nFd);

    return 0;

}

今天测试一个linux下串口的程序,执行时 提示

open serial port: Permission denied
open_port: Permission denied

环境:

ubuntu12.04

pl2303 usb转串口

后来在网上搜到此文,原来是使用者权限不够

处理办法

1,改变ttyUSB0的执行权限# sudo chmod 777 /dev/ttyUSB0 

2,直接su root  用超级用户执行

  • 15
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值