当前位置:   article > 正文

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

ubuntu c语言接受端口流量

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

进入正题!!

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

  1. Welcome to minicom 2.7
  2. OPTIONS: I18n
  3. Compiled on Feb 7 2016, 13:37:27.
  4. Port /dev/ttyUSB0, 14:06:56
  5. Press CTRL-A Z for help on special keys

上面就是命令模式了

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

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

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

3.串口发送程序

  1. /
  2. //Author: CSDN Maizidian
  3. //Serial communication under ubutnu, data sending and receiving
  4. /
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <termios.h>
  14. #define BAUDRATE B115200 ///Baud rate : 115200
  15. #define DEVICE "/dev/ttyUSB0"//Set your port number
  16. int nFd = 0;
  17. struct termios stNew;
  18. struct termios stOld;
  19. //Open Port & Set Port
  20. int SerialInit()
  21. {
  22.     nFd = open(DEVICE, O_RDWR|O_NOCTTY|O_NDELAY);
  23.     if(-1 == nFd)
  24.     {
  25.         perror("Open Serial Port Error!\n");
  26.         return -1;
  27.     }
  28.     if( (fcntl(nFd, F_SETFL, 0)) < 0 )
  29.     {
  30.         perror("Fcntl F_SETFL Error!\n");
  31.         return -1;
  32.     }
  33.     if(tcgetattr(nFd, &stOld) != 0)
  34.     {
  35.         perror("tcgetattr error!\n");
  36.         return -1;
  37.     }
  38.     stNew = stOld;
  39.     cfmakeraw(&stNew);//Set the terminal to raw mode, in which all input data is processed in bytes
  40.     //set speed
  41.     cfsetispeed(&stNew, BAUDRATE);//115200
  42.     cfsetospeed(&stNew, BAUDRATE);
  43.     //set databits
  44.     stNew.c_cflag |= (CLOCAL|CREAD);
  45.     stNew.c_cflag &= ~CSIZE;
  46.     stNew.c_cflag |= CS8;
  47.     //set parity
  48. stNew.c_cflag &= ~PARENB;
  49. stNew.c_iflag &= ~INNPCK;
  50.     //set stopbits
  51. stNew.c_cflag &= ~CSTOPB;
  52.     stNew.c_cc[VTIME]=0; //Specify the minimum number of characters to be read
  53.     stNew.c_cc[VMIN]=1; //Specify the waiting time for reading the first character, the unit of time is n*100ms
  54.                 //Assuming VTIME=0 is set, the read() operation is blocked indefinitely when no character is input
  55.     tcflush(nFd,TCIFLUSH); //Clear the terminal's unfinished input/output requests and data.
  56. if( tcsetattr(nFd,TCSANOW,&stNew) != 0 )
  57.     {
  58.         perror("tcsetattr Error!\n");
  59.         return -1;
  60.     }
  61.     return nFd;
  62. }
  63. int main(int argc, char **argv)
  64. { int i;
  65.     int nRet = 0;
  66.     char *sendmsg="Wheat";
  67.     char buf[5];
  68.     if( SerialInit() == -1 )
  69.     {
  70.         perror("SerialInit Error!\n");
  71.         return -1;
  72.     }
  73.     bzero(buf, SIZE);
  74.     while(1)
  75.     { sleep(1);
  76.         write(nFd,sendmsg,sizeof(sendmsg));//Send data to serial port
  77.         printf("%s\n",sendmsg);
  78.         /serial port receiving part
  79.         nRet = read(nFd, buf, SIZE);
  80.         if(-1 == nRet)
  81.         {
  82.             perror("Read Data Error!\n");
  83.             break;
  84.         }
  85.         if(0 < nRet)
  86.         {
  87.             buf[nRet] = 0;
  88.             printf("Recv Data: %s\n", buf);
  89.         }
  90.         /
  91.     }
  92.     close(nFd);
  93.     return 0;
  94. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号