• 【Orangepi Zero2 全志H616】驱动串口通信


    一、串口的基本认知
    二、串口关于电气标准和协议标准
    三、串口通信协议
    四、Linux系统中使用串口通信
    五、orangepi zern2 两组串口
    六、串行测试serialtest

    七、基于wiringPi的串口开发
    八、通过分析源码来实现的串口通信

    一、串口的基本认知

    串口(Serial Port),也被称为串行端口,是一种用于数据通信的物理接口,允许数据以连续的比特流形式在计算机和外部设备之间进行传输。串口通信在各种应用中非常常见,包括传感器连接、嵌入式系统、通信设备、GPS接收器、打印机等。

    以下是关于串口的一些基本认知:

    1. 串口的特点

      • 串口通信是一种点对点通信,通常用于连接一台计算机和一个外部设备。
      • 数据是逐位(比特)传输的,按照特定的时间间隔传送。
      • 串口通信可以采用不同的波特率(通信速率),通常使用的波特率包括 9600、115200 等。
    2. 串口通信的标准

      • 串口通信通常遵循一些标准,如RS-232、RS-485、UART等。这些标准规定了信号电平、数据位数、校验位等通信参数。
      • RS-232 是一个常见的串口通信标准,定义了串行通信的电气和机械特性。
    3. 串口通信参数

      • 串口通信需要配置参数,以确保通信双方之间的一致性。这些参数包括波特率、数据位、停止位、校验位等。
      • 波特率表示每秒传输的比特数,数据位表示每个字节的位数,停止位指定字节之间的停止间隔,校验位用于检测数据的完整性。
    4. 数据传输

      • 串口通信将数据分成一个一个的字节,然后逐位传输。通常,每个字节以起始位开始,然后是数据位,接着是可选的校验位和停止位。
    5. 流控制

      • 串口通信可以使用硬件流控制(RTS/CTS)或软件流控制(XON/XOFF)来管理数据流。流控制用于防止数据的溢出。
    6. 串口设备

      • 串口通信需要使用串口设备,通常通过串口端口(如COM端口)连接到计算机。在Linux系统中,串口设备通常位于 /dev 目录下。
    7. 应用

      • 串口通信在各种领域中广泛应用,包括数据采集、嵌入式系统通信、传感器连接、外部设备控制、GPS数据接收等。
    8. 编程

      • 在编程中,你可以使用合适的编程语言(如C/C++)和串口库来进行串口通信。通常,你需要打开串口设备、配置通信参数、读取和写入数据。

    串口通信是一种非常有用的通信方式,特别适合连接外部设备和嵌入式系统。了解串口通信的基本概念和参数设置对于成功进行串口通信非常重要。

    二、串口关于电气标准和协议标准

    串口通信涉及两个主要方面的标准:电气标准和协议标准。电气标准规定了物理层的信号电平和电气特性,而协议标准规定了数据的格式和传输方式。以下是关于这两个方面的一些常见标准:

    电气标准

    1. RS-232:RS-232 是一种常见的串口电气标准,它定义了串行通信的电气和机械特性。RS-232 接口使用正负电平来表示逻辑值,通常用于连接计算机和外部设备。

    2. RS-485:RS-485 是一种多点半双工串口标准,通常用于工业控制和数据通信应用。RS-485 具有更远的传输距离和更高的噪声抗干扰能力。

    3. TTL(Transistor-Transistor Logic):TTL 是一种数字电平标准,通常用于嵌入式系统和数字电路中。TTL 电平通常以0V表示逻辑低电平,以3.3V或5V表示逻辑高电平。

    4. CMOS(Complementary Metal-Oxide-Semiconductor):CMOS 也是一种数字电平标准,通常用于数字电路和微控制器中。CMOS 电平通常以0V表示逻辑低电平,以接近电源电压的值表示逻辑高电平。

    5. LVDS(Low Voltage Differential Signaling):LVDS 是一种差分信号标准,通常用于高速数据传输,如显示器连接。

    协议标准

    1. UART(Universal Asynchronous Receiver/Transmitter):UART 是一种常见的串口通信协议,通常用于数据的异步传输。它包括起始位、数据位、停止位和可选的校验位。

    2. SPI(Serial Peripheral Interface):SPI 是一种同步串行通信协议,通常用于连接微控制器、传感器和外围设备。SPI 使用主从结构,包括时钟、数据输入和数据输出线。

    3. I2C(Inter-Integrated Circuit):I2C 是一种串行通信协议,通常用于连接多个设备到相同的总线上。它使用双线制,包括数据线(SDA)和时钟线(SCL)。

    4. RS-232、RS-485协议:除了电气标准,RS-232 和 RS-485 还包括通信协议,用于定义数据帧格式和流控制。

    5. Modbus:Modbus 是一种常用于工业自动化的串行通信协议,支持多种物理层标准,包括串口通信。

    6. CAN(Controller Area Network):CAN 是一种串行通信协议,通常用于汽车和工业控制领域。它支持高噪声环境下的可靠通信。

    7. USB(Universal Serial Bus):USB 是一种通用的串行通信标准,用于连接计算机和外部设备。它支持高速、全速和低速传输模式。

    不同的应用和硬件要求通常需要不同的电气标准和协议标准。因此,在进行串口通信时,你需要了解你所使用的设备的规格和要求,以便正确配置串口参数并选择适当的通信协议。

    三、串口通信协议

    串口通信协议中的波特率、奇偶检验位和停止位等参数是非常重要的,它们决定了数据的传输方式和数据的完整性检查。以下是一些常见的串口通信参数:

    1. 波特率(Baud Rate)

      • 波特率是串口通信中的传输速率,它表示每秒传输的比特数。常见的波特率包括 9600、115200、57600 等。
      • 通信的双方必须使用相同的波特率。波特率过高可能会导致数据传输错误,因此需要根据硬件和通信距离来选择适当的波特率。
    2. 数据位(Data Bits)

      • 数据位指定每个数据字节中的位数,通常为 5、6、7 或 8 位。大多数情况下,使用 8 位数据位以支持 8 位的二进制数据。
    3. 奇偶检验位(Parity)

      • 奇偶检验位用于检测数据传输中的错误。通常有以下几种选项:
        • 无校验位:不使用奇偶检验位。
        • 奇校验位:确保数据位中有奇数个 “1”。
        • 偶校验位:确保数据位中有偶数个 “1”。
      • 奇偶检验位通常用于检测单比特错误。
    4. 停止位(Stop Bits)

      • 停止位表示数据字节的结束。通常有 1 位和 2 位停止位选项,其中 1 位停止位是最常见的选择。

    这些参数通常一起组合,以确定数据的帧格式。例如,常见的串口通信设置是:

    • 波特率:9600
    • 数据位:8
    • 无奇偶检验位
    • 1 位停止位

    这意味着每个数据帧由 10 位组成:1 位起始位、8 位数据位、无奇偶检验位和 1 位停止位。

    正确配置这些参数对于串口通信非常重要,因为通信的双方必须使用相同的参数,否则会导致数据传输错误。通常,串口设备的规格表明了所需的通信参数设置。在编程中,你需要使用相应的库函数来设置这些参数,以确保正确的数据传输和校验。

    四、Linux系统中使用串口通信

    串口通信是一种通过串行连接传输数据的通信方式,它通常用于连接计算机和外部设备,如传感器、嵌入式系统、GPS接收器、微控制器等。在Linux系统中,你可以使用串口通信来与这些设备进行数据交换。

    以下是在Linux系统中使用串口通信的一般步骤:

    1. 打开串口设备

      • 在Linux系统中,串口设备通常位于 /dev 目录下,例如 /dev/ttyS0 表示第一个串口设备。你可以使用C语言中的 open 函数打开串口设备文件,类似于打开文件。
    2. 配置串口参数

      • 使用 termios 结构和 tcsetattr 函数来配置串口通信参数,如波特率、数据位、停止位、校验位等。这些参数需要与目标设备匹配,以确保正确的通信。
    3. 读取和写入数据

      • 使用 read 函数从串口读取数据,使用 write 函数将数据写入串口。你可以使用这些函数来与设备进行数据交换。
    4. 关闭串口

      • 当通信结束时,使用 close 函数关闭串口设备。

    以下是一个示例C程序,演示如何在Linux系统中打开串口、配置串口参数、读取和写入数据:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main() {
        int fd;
        struct termios serial;
    
        // 打开串口设备
        fd = open("/dev/ttyS0", O_RDWR);
        if (fd == -1) {
            perror("open");
            return 1;
        }
    
        // 配置串口参数
        tcgetattr(fd, &serial);
        serial.c_cflag = B9600; // 波特率 9600
        serial.c_cflag |= CS8;  // 8位数据位
        serial.c_cflag &= ~PARENB; // 无奇偶校验
        tcsetattr(fd, TCSANOW, &serial);
    
        char data_to_send[] = "Hello, Serial!";
        write(fd, data_to_send, strlen(data_to_send));
    
        char buffer[100];
        ssize_t n = read(fd, buffer, sizeof(buffer));
        if (n > 0) {
            buffer[n] = '\0';
            printf("Received data: %s\n", buffer);
        }
    
        close(fd); // 关闭串口
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    这个示例演示了如何与串口设备进行基本的数据交换。请注意,串口设备的配置参数需要与你连接的设备匹配,因此根据设备的要求来调整波特率和其他参数。此示例中使用的是 /dev/ttyS0,你可能需要根据你的系统和硬件配置来指定正确的串口设备。

    五、orangepi zern2 两组串口

    在这里插入图片描述
    在这里插入图片描述

    六、串行测试serialtest

    在这里插入图片描述

    /*
     * serialTest.c:
     *	Very simple program to test the serial port. Expects
     *	the port to be looped back to itself
     *
     * Copyright (c) 2012-2013 Gordon Henderson. 
     ***********************************************************************
     * This file is part of wiringPi:
     *	https://projects.drogon.net/raspberry-pi/wiringpi/
     *
     *    wiringPi is free software: you can redistribute it and/or modify
     *    it under the terms of the GNU Lesser General Public License as published by
     *    the Free Software Foundation, either version 3 of the License, or
     *    (at your option) any later version.
     *
     *    wiringPi is distributed in the hope that it will be useful,
     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *    GNU Lesser General Public License for more details.
     *
     *    You should have received a copy of the GNU Lesser General Public License
     *    along with wiringPi.  If not, see .
     ***********************************************************************
     */
    
    #include 
    #include 
    #include 
    
    #include 
    #include 
    
    int main ()
    {
      int fd ;
      int count ;
      unsigned int nextTime ;
    
      if ((fd = serialOpen ("/dev/ttyS2", 115200)) < 0)
      {
        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
        return 1 ;
      }
    
      if (wiringPiSetup () == -1)
      {
        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
        return 1 ;
      }
    
      nextTime = millis () + 300 ;
    
      for (count = 0 ; count < 256 ; )
      {
        if (millis () > nextTime)
        {
          printf ("\nOut: %3d: ", count) ;
          fflush (stdout) ;
          serialPutchar (fd, count) ;
          nextTime += 300 ;
          ++count ;
        }
    
        delay (3) ;
    
        while (serialDataAvail (fd))
        {
          printf (" -> %3d", serialGetchar (fd)) ;
          fflush (stdout) ;
        }
      }
    
      printf ("\n") ;
      return 0 ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    在这里插入图片描述

    根据手册修改配置文件

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    根据结果知道代码是通过串口发送ASCII的数据

    在这里插入图片描述
    在这里插入图片描述

    七、基于wiringPi的串口开发

    #include  
    #include  
    #include   
    #include  
    #include 
    #include 
    #include  
    #include 
     
    int fd; 
     
    void* Sendhandler() 
    { 
        char *sendBuf; 
        sendBuf = (char *)malloc(32*sizeof(32));
     
        while (1){ 
            memset(sendBuf, '\0', 32); 
            scanf("%s", sendBuf); 
            while (*sendBuf){ 
                serialPutchar(fd, *sendBuf++); 
            } 
        } 
    }
     
    void* Revhandler() 
    { 
        while (1){ 
            while (serialDataAvail(fd)){ 
                printf("%c", serialGetchar(fd)); 
                fflush(stdout); 
            } 
        }
    }
     
    int main() 
    { 
        int count; 
        unsigned int nextTime; 
     
        pthread_t idSend; 
        pthread_t idRev; 
     
        if ((fd = serialOpen("/dev/ttyS5", 115200)) < 0){ 
            fprintf(stderr, "Unable to open serial device: %s\n", strerror(errno)); 
            return 1; 
        }
        pthread_create(&idSend, NULL, Sendhandler, NULL); 
        pthread_create(&idRev, NULL, Revhandler, NULL); 
     
        if (wiringPiSetup() == -1){ 
            fprintf(stdout, "Unable to start wiringPi: %s\n", strerror(errno)); 
            return 1 ; 
        }
     
        while (1){sleep(10);} 
     
        printf("\n"); 
        return 0; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    在这里插入图片描述

    八、通过分析源码来实现的串口通信

    源码

    /*
     * wiringSerial.c:
     *	Handle a serial port
     ***********************************************************************
     * This file is part of wiringPi:
     *	https://projects.drogon.net/raspberry-pi/wiringpi/
     *
     *    wiringPi is free software: you can redistribute it and/or modify
     *    it under the terms of the GNU Lesser General Public License as published by
     *    the Free Software Foundation, either version 3 of the License, or
     *    (at your option) any later version.
     *
     *    wiringPi is distributed in the hope that it will be useful,
     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *    GNU Lesser General Public License for more details.
     *
     *    You should have received a copy of the GNU Lesser General Public License
     *    along with wiringPi.  If not, see .
     ***********************************************************************
     */
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include "wiringSerial.h"
    
    /*
     * serialOpen:
     *	Open and initialise the serial port, setting all the right
     *	port parameters - or as many as are required - hopefully!
     *********************************************************************************
     */
    
    int serialOpen (const char *device, const int baud)
    {
      struct termios options ;
      speed_t myBaud ;
      int     status, fd ;
    
      switch (baud)
      {
        case      50:	myBaud =      B50 ; break ;
        case      75:	myBaud =      B75 ; break ;
        case     110:	myBaud =     B110 ; break ;
        case     134:	myBaud =     B134 ; break ;
        case     150:	myBaud =     B150 ; break ;
        case     200:	myBaud =     B200 ; break ;
        case     300:	myBaud =     B300 ; break ;
        case     600:	myBaud =     B600 ; break ;
        case    1200:	myBaud =    B1200 ; break ;
        case    1800:	myBaud =    B1800 ; break ;
        case    2400:	myBaud =    B2400 ; break ;
        case    4800:	myBaud =    B4800 ; break ;
        case    9600:	myBaud =    B9600 ; break ;
        case   19200:	myBaud =   B19200 ; break ;
        case   38400:	myBaud =   B38400 ; break ;
        case   57600:	myBaud =   B57600 ; break ;
        case  115200:	myBaud =  B115200 ; break ;
        case  230400:	myBaud =  B230400 ; break ;
        case  460800:	myBaud =  B460800 ; break ;
        case  500000:	myBaud =  B500000 ; break ;
        case  576000:	myBaud =  B576000 ; break ;
        case  921600:	myBaud =  B921600 ; break ;
        case 1000000:	myBaud = B1000000 ; break ;
        case 1152000:	myBaud = B1152000 ; break ;
        case 1500000:	myBaud = B1500000 ; break ;
        case 2000000:	myBaud = B2000000 ; break ;
        case 2500000:	myBaud = B2500000 ; break ;
        case 3000000:	myBaud = B3000000 ; break ;
        case 3500000:	myBaud = B3500000 ; break ;
        case 4000000:	myBaud = B4000000 ; break ;
    
        default:
          return -2 ;
      }
    
      if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
        return -1 ;
    
      fcntl (fd, F_SETFL, O_RDWR) ;
    
    // Get and modify current options:
    
      tcgetattr (fd, &options) ;
    
        cfmakeraw   (&options) ;
        cfsetispeed (&options, myBaud) ;
        cfsetospeed (&options, myBaud) ;
    
        options.c_cflag |= (CLOCAL | CREAD) ;
        options.c_cflag &= ~PARENB ;
        options.c_cflag &= ~CSTOPB ;
        options.c_cflag &= ~CSIZE ;
        options.c_cflag |= CS8 ;
        options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
        options.c_oflag &= ~OPOST ;
    
        options.c_cc [VMIN]  =   0 ;
        options.c_cc [VTIME] = 100 ;	// Ten seconds (100 deciseconds)
    
      tcsetattr (fd, TCSANOW, &options) ;
    
      ioctl (fd, TIOCMGET, &status);
    
      status |= TIOCM_DTR ;
      status |= TIOCM_RTS ;
    
      ioctl (fd, TIOCMSET, &status);
    
      usleep (10000) ;	// 10mS
    
      return fd ;
    }
    
    
    /*
     * serialFlush:
     *	Flush the serial buffers (both tx & rx)
     *********************************************************************************
     */
    
    void serialFlush (const int fd)
    {
      tcflush (fd, TCIOFLUSH) ;
    }
    
    
    /*
     * serialClose:
     *	Release the serial port
     *********************************************************************************
     */
    
    void serialClose (const int fd)
    {
      close (fd) ;
    }
    
    
    /*
     * serialPutchar:
     *	Send a single character to the serial port
     *********************************************************************************
     */
    
    void serialPutchar (const int fd, const unsigned char c)
    {
      int ret;
      ret = write (fd, &c, 1) ;
      if (ret < 0)
      	 printf("Serial Putchar Error\n");
    }
    
    
    /*
     * serialPuts:
     *	Send a string to the serial port
     *********************************************************************************
     */
    
    void serialPuts (const int fd, const char *s)
    { 
    	int ret;
    	ret = write (fd, s, strlen (s));
    	if (ret < 0)
    		printf("Serial Puts Error\n");
    }
    
    /*
     * serialPrintf:
     *	Printf over Serial
     *********************************************************************************
     */
    
    void serialPrintf (const int fd, const char *message, ...)
    {
      va_list argp ;
      char buffer [1024] ;
    
      va_start (argp, message) ;
        vsnprintf (buffer, 1023, message, argp) ;
      va_end (argp) ;
    
      serialPuts (fd, buffer) ;
    }
    
    
    /*
     * serialDataAvail:
     *	Return the number of bytes of data avalable to be read in the serial port
     *********************************************************************************
     */
    
    int serialDataAvail (const int fd)
    {
      int result ;
    
      if (ioctl (fd, FIONREAD, &result) == -1)
        return -1 ;
    
      return result ;
    }
    
    
    /*
     * serialGetchar:
     *	Get a single character from the serial device.
     *	Note: Zero is a valid character and this function will time-out after
     *	10 seconds.
     *********************************************************************************
     */
    
    int serialGetchar (const int fd)
    {
      uint8_t x ;
    
      if (read (fd, &x, 1) != 1)
        return -1 ;
    
      return ((int)x) & 0xFF ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231

    uartTool.h

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include "wiringSerial.h"
    
    int my_serialOpen (const char *device, const int baud) ;
    
    void my_serialSendstring (const int fd, const char *s) ;
    
    int my_serialGetstring (const int fd, char *buffer) ;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    uartTool.c

    #include "wiringSerial.h"
    #include "uartTool.h"
    
    int my_serialOpen (const char *device, const int baud)
    {
    	struct termios options ;   // 创建一个termios结构体,用于串口参数设置
    	speed_t myBaud ;   // 创建一个速度类型的变量 myBaud,用于保存波特率
    	int status, fd ;   // 创建整数类型的变量 status 和 fd,用于保存状态和文件描述符
     
    	switch (baud){   // 根据传入的波特率参数选择合适的波特率常数
    		case   9600: myBaud =   B9600 ; break ; 
    		case 115200: myBaud = B115200 ; break ; 
    	}
    	if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)   // 打开串口设备,设置打开选项
    	return -1 ;   // 如果打开失败,返回错误代码 -1
    	
    	fcntl (fd, F_SETFL, O_RDWR) ;   // 设置文件状态标志
    	
    // Get and modify current options: 获取并修改当前的串口参数:
     
    	tcgetattr (fd, &options) ;   // 获取当前的串口参数
     
    	cfmakeraw (&options) ;   // 初始化 termios 结构体为原始模式
    	cfsetispeed (&options, myBaud) ;  // 设置输入波特率
        cfsetospeed (&options, myBaud) ;  // 设置输出波特率
     
        options.c_cflag |= (CLOCAL | CREAD) ;  // 本地连接和使能接收
        options.c_cflag &= ~PARENB ;  // 禁用奇偶校验
        options.c_cflag &= ~CSTOPB ;  // 1位停止位
        options.c_cflag &= ~CSIZE ;  // 用数据位掩码清空数据位设置
        options.c_cflag |= CS8 ;  // 设置8位数据位
    	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;   // 禁用规范输入
    	options.c_oflag &= ~OPOST ;   // 禁用输出处理
     
    	options.c_cc [VMIN] = 0 ;   // 读取数据的最小字符数
    	options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds) 超时等待时间(十分之一秒100ms)
     
    	tcsetattr (fd, TCSANOW, &options) ;   // 设置新的串口参数
     
    	ioctl (fd, TIOCMGET, &status);   // 获取串口控制模式状态
     
    	status |= TIOCM_DTR ;   // 设置 DTR(数据终端就绪)位
    	status |= TIOCM_RTS ;   // 设置 RTS(请求发送)位
     
    	ioctl (fd, TIOCMSET, &status);   // 设置串口控制模式状态
    	
    	usleep (10000) ;  // 暂停 10 毫秒
    	
    	return fd ;   // 返回串口文件描述符
    }
    
    void my_serialSendstring (const int fd, const char *s)
    {
    	int ret ;
    
    	ret = write (fd, s, strlen (s)) ; 
    	
    	if (ret < 0) 
    		printf ("Serial Sendstring Error\n") ;
    }
    
    int my_serialGetstring (const int fd, char *buffer)
    {
    	int n_read ;
    
    	n_read = read (fd, buffer, 32) ; 
    	
    	return n_read ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    uartTest.c

    
    #include 
    #include "uartTool.h"
    
    int fd;
     
    void* readSerial ()
    {
        char buffer [32] ;
    
        while (1) {
            memset (buffer, '\0', sizeof(buffer)) ;
            my_serialGetstring (fd, buffer) ;
            printf ("GET->%s\n", buffer) ;
        }
    }
     
    void* sendSerial ()
    {
        char buffer [32] ;
    
        while (1) {
    	    memset (buffer,'\0', sizeof(buffer)) ;
        	scanf ("%s", buffer) ;
        	my_serialSendstring (fd, buffer) ;
        }
    }
     
    int main (int argc, char **argv)
    {
        char deviceName [32] = {'\0'} ;
        pthread_t readt ;
        pthread_t sendt ;
     
        if (argc < 2) {
            printf ("uage:%s /dev/ttyS?\n", argv[0]) ;
            return -1 ;
        }
     
        strcpy (deviceName, argv[1]) ;
     
        if ((fd = my_serialOpen (deviceName, 115200)) == -1) {
            printf ("open %s error\n", deviceName) ;
            return -1;
        }
     
        pthread_create (&readt, NULL, readSerial, NULL) ;
        pthread_create (&sendt, NULL, sendSerial, NULL) ;
     
        while (1) {sleep (10);}
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    gcc uartTool.c uartTool.h uartTest.c -lpthread
    ./a.out /dev/ttyS5
    
    • 1
    • 2

    在这里插入图片描述

  • 相关阅读:
    SpringSecurity - 初识 SpringSecurity
    Java 进阶书籍
    NVIDIA 安装 CUDA
    前端实现页面内容的截图与下载(html2canvas)
    java计算机毕业设计ssm+jsp计算机视频学习网站
    error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor‘
    超硬核的Move Dev Meetup上海线下交流会圆满结束
    【无标题】
    活动推广的作用有哪些呢?
    Verilog仿真跨模块调用内部信号的方法
  • 原文地址:https://blog.csdn.net/m0_62140641/article/details/134269259