赞
踩
关于串口的知识这里就不累赘了,看着多又烦,搞这个的都懂串口,不多废话了!!
进入正题!!
某宝很多这种模块,有各种型号的(例如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也需要设置)
$ 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就可以打开了
-
- /
-
- //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;
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。