nuc980开发板使用Agile Modbus软件包-基于 rs485 通讯

一、nuc980开发板电路

打开 nuc980-eth2p 开发板原理图,如下:
在这里插入图片描述
将JP1跳线帽连接到rs485。使用rs485转usb连接到电脑即可。
除了收发引脚,多了一个控制引脚。

linux内核使能串口4
在这里插入图片描述

二、Agile Modbus软件包

1、软件包的获取

下载网址
选择最新版即可。
在这里插入图片描述

2、软件包的demo

打开下载的文件,文件工程的结构如下,包含doc、examples、inc、src等文件。
在这里插入图片描述
因为我们是 rtu-master,所以打开官方自带的 rtu_master 文件夹。查看串口初始化部分。
在这里插入图片描述

int serial_init(const char *device,
                int baud, char parity, int data_bit,
                int stop_bit, struct termios *old_tios)
{

    struct termios tios;
    speed_t speed;
    int flags;

    /* The O_NOCTTY flag tells UNIX that this program doesn't want
       to be the "controlling terminal" for that port. If you
       don't specify this then any input (such as keyboard abort
       signals and so forth) will affect your process

       Timeouts are ignored in canonical input mode or when the
       NDELAY option is set on the file via open or fcntl */
    flags = O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL;
#ifdef O_CLOEXEC
    flags |= O_CLOEXEC;
#endif

    int s = open(device, flags);
    if (s == -1) {
        LOG_E("ERROR Can't open the device %s (%s)", device, strerror(errno));
        return -1;
    }

    flags = fcntl(s, F_GETFL, 0);
    flags |= O_NONBLOCK;
    fcntl(s, F_SETFL, flags);

    flags = fcntl(s, F_GETFD);
    flags |= FD_CLOEXEC;
    fcntl(s, F_SETFD, flags);

    /* Save */
    tcgetattr(s, old_tios);

    memset(&tios, 0, sizeof(struct termios));

    /* C_ISPEED     Input baud (new interface)
       C_OSPEED     Output baud (new interface)
    */
    switch (baud) {
    case 110:
        speed = B110;
        break;
    case 300:
        speed = B300;
        break;
    case 600:
        speed = B600;
        break;
    case 1200:
        speed = B1200;
        break;
    case 2400:
        speed = B2400;
        break;
    case 4800:
        speed = B4800;
        break;
    case 9600:
        speed = B9600;
        break;
    case 19200:
        speed = B19200;
        break;
    case 38400:
        speed = B38400;
        break;
#ifdef B57600
    case 57600:
        speed = B57600;
        break;
#endif
#ifdef B115200
    case 115200:
        speed = B115200;
        break;
#endif
#ifdef B230400
    case 230400:
        speed = B230400;
        break;
#endif
#ifdef B460800
    case 460800:
        speed = B460800;
        break;
#endif
#ifdef B500000
    case 500000:
        speed = B500000;
        break;
#endif
#ifdef B576000
    case 576000:
        speed = B576000;
        break;
#endif
#ifdef B921600
    case 921600:
        speed = B921600;
        break;
#endif
#ifdef B1000000
    case 1000000:
        speed = B1000000;
        break;
#endif
#ifdef B1152000
    case 1152000:
        speed = B1152000;
        break;
#endif
#ifdef B1500000
    case 1500000:
        speed = B1500000;
        break;
#endif
#ifdef B2500000
    case 2500000:
        speed = B2500000;
        break;
#endif
#ifdef B3000000
    case 3000000:
        speed = B3000000;
        break;
#endif
#ifdef B3500000
    case 3500000:
        speed = B3500000;
        break;
#endif
#ifdef B4000000
    case 4000000:
        speed = B4000000;
        break;
#endif
    default:
        speed = B9600;
        LOG_W("WARNING Unknown baud rate %d for %s (B9600 used)", baud, device);
    }

    /* Set the baud rate */
    if ((cfsetispeed(&tios, speed) < 0) ||
        (cfsetospeed(&tios, speed) < 0)) {
        close(s);
        s = -1;
        return -1;
    }

    /* C_CFLAG      Control options
       CLOCAL       Local line - do not change "owner" of port
       CREAD        Enable receiver
    */
    tios.c_cflag |= (CREAD | CLOCAL);
    /* CSIZE, HUPCL, CRTSCTS (hardware flow control) */

    /* Set data bits (5, 6, 7, 8 bits)
       CSIZE        Bit mask for data bits
    */
    tios.c_cflag &= ~CSIZE;
    switch (data_bit) {
    case 5:
        tios.c_cflag |= CS5;
        break;
    case 6:
        tios.c_cflag |= CS6;
        break;
    case 7:
        tios.c_cflag |= CS7;
        break;
    case 8:
    default:
        tios.c_cflag |= CS8;
        break;
    }

    /* Stop bit (1 or 2) */
    if (stop_bit == 1)
        tios.c_cflag &= ~CSTOPB;
    else /* 2 */
        tios.c_cflag |= CSTOPB;

    /* PARENB       Enable parity bit
       PARODD       Use odd parity instead of even */
    if (parity == 'N') {
        /* None */
        tios.c_cflag &= ~PARENB;
    } else if (parity == 'E') {
        /* Even */
        tios.c_cflag |= PARENB;
        tios.c_cflag &= ~PARODD;
    } else {
        /* Odd */
        tios.c_cflag |= PARENB;
        tios.c_cflag |= PARODD;
    }

    /* Read the man page of termios if you need more information. */

    /* This field isn't used on POSIX systems
       tios.c_line = 0;
    */

    /* C_LFLAG      Line options

       ISIG Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals
       ICANON       Enable canonical input (else raw)
       XCASE        Map uppercase \lowercase (obsolete)
       ECHO Enable echoing of input characters
       ECHOE        Echo erase character as BS-SP-BS
       ECHOK        Echo NL after kill character
       ECHONL       Echo NL
       NOFLSH       Disable flushing of input buffers after
       interrupt or quit characters
       IEXTEN       Enable extended functions
       ECHOCTL      Echo control characters as ^char and delete as ~?
       ECHOPRT      Echo erased character as character erased
       ECHOKE       BS-SP-BS entire line on line kill
       FLUSHO       Output being flushed
       PENDIN       Retype pending input at next read or input char
       TOSTOP       Send SIGTTOU for background output

       Canonical input is line-oriented. Input characters are put
       into a buffer which can be edited interactively by the user
       until a CR (carriage return) or LF (line feed) character is
       received.

       Raw input is unprocessed. Input characters are passed
       through exactly as they are received, when they are
       received. Generally you'll deselect the ICANON, ECHO,
       ECHOE, and ISIG options when using raw input
    */

    /* Raw input */
    tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

    /* C_IFLAG      Input options

       Constant     Description
       INPCK        Enable parity check
       IGNPAR       Ignore parity errors
       PARMRK       Mark parity errors
       ISTRIP       Strip parity bits
       IXON Enable software flow control (outgoing)
       IXOFF        Enable software flow control (incoming)
       IXANY        Allow any character to start flow again
       IGNBRK       Ignore break condition
       BRKINT       Send a SIGINT when a break condition is detected
       INLCR        Map NL to CR
       IGNCR        Ignore CR
       ICRNL        Map CR to NL
       IUCLC        Map uppercase to lowercase
       IMAXBEL      Echo BEL on input line too long
    */
    if (parity == 'N') {
        /* None */
        tios.c_iflag &= ~INPCK;
    } else {
        tios.c_iflag |= INPCK;
    }

    /* Software flow control is disabled */
    tios.c_iflag &= ~(IXON | IXOFF | IXANY);

    /* C_OFLAG      Output options
       OPOST        Postprocess output (not set = raw output)
       ONLCR        Map NL to CR-NL

       ONCLR ant others needs OPOST to be enabled
    */

    /* Raw ouput */
    tios.c_oflag &= ~OPOST;

    /* C_CC         Control characters
       VMIN         Minimum number of characters to read
       VTIME        Time to wait for data (tenths of seconds)

       UNIX serial interface drivers provide the ability to
       specify character and packet timeouts. Two elements of the
       c_cc array are used for timeouts: VMIN and VTIME. Timeouts
       are ignored in canonical input mode or when the NDELAY
       option is set on the file via open or fcntl.

       VMIN specifies the minimum number of characters to read. If
       it is set to 0, then the VTIME value specifies the time to
       wait for every character read. Note that this does not mean
       that a read call for N bytes will wait for N characters to
       come in. Rather, the timeout will apply to the first
       character and the read call will return the number of
       characters immediately available (up to the number you
       request).

       If VMIN is non-zero, VTIME specifies the time to wait for
       the first character read. If a character is read within the
       time given, any read will block (wait) until all VMIN
       characters are read. That is, once the first character is
       read, the serial interface driver expects to receive an
       entire packet of characters (VMIN bytes total). If no
       character is read within the time allowed, then the call to
       read returns 0. This method allows you to tell the serial
       driver you need exactly N bytes and any read call will
       return 0 or N bytes. However, the timeout only applies to
       the first character read, so if for some reason the driver
       misses one character inside the N byte packet then the read
       call could block forever waiting for additional input
       characters.

       VTIME specifies the amount of time to wait for incoming
       characters in tenths of seconds. If VTIME is set to 0 (the
       default), reads will block (wait) indefinitely unless the
       NDELAY option is set on the port with open or fcntl.
    */
    /* Unused because we use open with the NDELAY option */
    tios.c_cc[VMIN] = 0;
    tios.c_cc[VTIME] = 0;

    if (tcsetattr(s, TCSANOW, &tios) < 0) {
        close(s);
        s = -1;
        return -1;
    }

    return s;
}

修改Makefile文件
在这里插入图片描述
将 gcc 改为 arm-linux-gcc

CC = arm-linux-gcc -std=gnu99

此时编译并下载到开发板,肯定不能正常通信,还需要对 serial.c 文件进行适当的修改。

三、修改思路

思路:通过对控制引脚的控制,来控制数据的接收和发送。
方法1
不修改串口的初始化,通过配置控制引脚的输出高低电平来操作数据的收发。
方法2
修改串口的初始化,配置为rs485模式,配置 RTS的引脚即可。
文章主要介绍方法2。
rs485收发测试可参考linux 串口测试指令和测试程序

四、串口配置修改

添加头文件

#include <sys/types.h>
#include <sys/stat.h> 
#include <pthread.h>
#include <linux/serial.h>

初始化添加代码

int portfd;
    #if (__GNUC__ == 4 && __GNUC_MINOR__ == 3)
	struct my_serial_rs485 rs485conf;
	struct my_serial_rs485 rs485conf_bak;
#else
	struct serial_rs485 rs485conf;
#endif	
    portfd=s;
	if (ioctl (portfd, TIOCGRS485, &rs485conf) < 0) 
	{
		/* Error handling.*/ 
		printf("ioctl TIOCGRS485 error.\n");
	}
	/* Enable RS485 mode: */
	rs485conf.flags |= SER_RS485_ENABLED;

	/* Set logical level for RTS pin equal to 1 when sending: */
	rs485conf.flags |= SER_RS485_RTS_ON_SEND;


	/* set logical level for RTS pin equal to 0 after sending: */ 
	rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);

	/* Set rts delay after send, if needed: */
	rs485conf.delay_rts_after_send = 0x80;

	if (ioctl (portfd, TIOCSRS485, &rs485conf) < 0)
	{
		/* Error handling.*/ 
		printf("ioctl TIOCSRS485 error.\n");
	}
	else
	{
		printf("rs485conf.flags 0x%x.\n", rs485conf.flags);
		printf("rs485conf.delay_rts_before_send 0x%x.\n", rs485conf.delay_rts_before_send);
		printf("rs485conf.delay_rts_after_send 0x%x.\n", rs485conf.delay_rts_after_send);
	}

serial.c 整体代码

#include "serial.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h> 
#include <pthread.h>
#include <linux/serial.h>

#define DBG_ENABLE
#define DBG_COLOR
#define DBG_SECTION_NAME "serial"
#define DBG_LEVEL        DBG_LOG
#include "dbg_log.h"


int serial_init(const char *device,
                int baud, char parity, int data_bit,
                int stop_bit, struct termios *old_tios)
{

    struct termios tios;
    speed_t speed;
    int flags;

    /* The O_NOCTTY flag tells UNIX that this program doesn't want
       to be the "controlling terminal" for that port. If you
       don't specify this then any input (such as keyboard abort
       signals and so forth) will affect your process

       Timeouts are ignored in canonical input mode or when the
       NDELAY option is set on the file via open or fcntl */
    flags = O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL;
#ifdef O_CLOEXEC
    flags |= O_CLOEXEC;
#endif

    int s = open(device, flags);
    if (s == -1) {
        LOG_E("ERROR Can't open the device %s (%s)", device, strerror(errno));
        return -1;
    }

    flags = fcntl(s, F_GETFL, 0);
    flags |= O_NONBLOCK;
    fcntl(s, F_SETFL, flags);

    flags = fcntl(s, F_GETFD);
    flags |= FD_CLOEXEC;
    fcntl(s, F_SETFD, flags);

    /* Save */
    tcgetattr(s, old_tios);

    memset(&tios, 0, sizeof(struct termios));

    /* C_ISPEED     Input baud (new interface)
       C_OSPEED     Output baud (new interface)
    */
    switch (baud) {
    case 110:
        speed = B110;
        break;
    case 300:
        speed = B300;
        break;
    case 600:
        speed = B600;
        break;
    case 1200:
        speed = B1200;
        break;
    case 2400:
        speed = B2400;
        break;
    case 4800:
        speed = B4800;
        break;
    case 9600:
        speed = B9600;
        break;
    case 19200:
        speed = B19200;
        break;
    case 38400:
        speed = B38400;
        break;
#ifdef B57600
    case 57600:
        speed = B57600;
        break;
#endif
#ifdef B115200
    case 115200:
        speed = B115200;
        break;
#endif
#ifdef B230400
    case 230400:
        speed = B230400;
        break;
#endif
#ifdef B460800
    case 460800:
        speed = B460800;
        break;
#endif
#ifdef B500000
    case 500000:
        speed = B500000;
        break;
#endif
#ifdef B576000
    case 576000:
        speed = B576000;
        break;
#endif
#ifdef B921600
    case 921600:
        speed = B921600;
        break;
#endif
#ifdef B1000000
    case 1000000:
        speed = B1000000;
        break;
#endif
#ifdef B1152000
    case 1152000:
        speed = B1152000;
        break;
#endif
#ifdef B1500000
    case 1500000:
        speed = B1500000;
        break;
#endif
#ifdef B2500000
    case 2500000:
        speed = B2500000;
        break;
#endif
#ifdef B3000000
    case 3000000:
        speed = B3000000;
        break;
#endif
#ifdef B3500000
    case 3500000:
        speed = B3500000;
        break;
#endif
#ifdef B4000000
    case 4000000:
        speed = B4000000;
        break;
#endif
    default:
        speed = B9600;
        LOG_W("WARNING Unknown baud rate %d for %s (B9600 used)", baud, device);
    }

    /* Set the baud rate */
    if ((cfsetispeed(&tios, speed) < 0) ||
        (cfsetospeed(&tios, speed) < 0)) {
        close(s);
        s = -1;
        return -1;
    }

    /* C_CFLAG      Control options
       CLOCAL       Local line - do not change "owner" of port
       CREAD        Enable receiver
    */
    tios.c_cflag |= (CREAD | CLOCAL);
    /* CSIZE, HUPCL, CRTSCTS (hardware flow control) */

    /* Set data bits (5, 6, 7, 8 bits)
       CSIZE        Bit mask for data bits
    */
    tios.c_cflag &= ~CSIZE;
    switch (data_bit) {
    case 5:
        tios.c_cflag |= CS5;
        break;
    case 6:
        tios.c_cflag |= CS6;
        break;
    case 7:
        tios.c_cflag |= CS7;
        break;
    case 8:
    default:
        tios.c_cflag |= CS8;
        break;
    }

    /* Stop bit (1 or 2) */
    if (stop_bit == 1)
        tios.c_cflag &= ~CSTOPB;
    else /* 2 */
        tios.c_cflag |= CSTOPB;

    /* PARENB       Enable parity bit
       PARODD       Use odd parity instead of even */
    if (parity == 'N') {
        /* None */
        tios.c_cflag &= ~PARENB;
    } else if (parity == 'E') {
        /* Even */
        tios.c_cflag |= PARENB;
        tios.c_cflag &= ~PARODD;
    } else {
        /* Odd */
        tios.c_cflag |= PARENB;
        tios.c_cflag |= PARODD;
    }

    /* Read the man page of termios if you need more information. */

    /* This field isn't used on POSIX systems
       tios.c_line = 0;
    */

    /* C_LFLAG      Line options

       ISIG Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals
       ICANON       Enable canonical input (else raw)
       XCASE        Map uppercase \lowercase (obsolete)
       ECHO Enable echoing of input characters
       ECHOE        Echo erase character as BS-SP-BS
       ECHOK        Echo NL after kill character
       ECHONL       Echo NL
       NOFLSH       Disable flushing of input buffers after
       interrupt or quit characters
       IEXTEN       Enable extended functions
       ECHOCTL      Echo control characters as ^char and delete as ~?
       ECHOPRT      Echo erased character as character erased
       ECHOKE       BS-SP-BS entire line on line kill
       FLUSHO       Output being flushed
       PENDIN       Retype pending input at next read or input char
       TOSTOP       Send SIGTTOU for background output

       Canonical input is line-oriented. Input characters are put
       into a buffer which can be edited interactively by the user
       until a CR (carriage return) or LF (line feed) character is
       received.

       Raw input is unprocessed. Input characters are passed
       through exactly as they are received, when they are
       received. Generally you'll deselect the ICANON, ECHO,
       ECHOE, and ISIG options when using raw input
    */

    /* Raw input */
    tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

    /* C_IFLAG      Input options

       Constant     Description
       INPCK        Enable parity check
       IGNPAR       Ignore parity errors
       PARMRK       Mark parity errors
       ISTRIP       Strip parity bits
       IXON Enable software flow control (outgoing)
       IXOFF        Enable software flow control (incoming)
       IXANY        Allow any character to start flow again
       IGNBRK       Ignore break condition
       BRKINT       Send a SIGINT when a break condition is detected
       INLCR        Map NL to CR
       IGNCR        Ignore CR
       ICRNL        Map CR to NL
       IUCLC        Map uppercase to lowercase
       IMAXBEL      Echo BEL on input line too long
    */
    if (parity == 'N') {
        /* None */
        tios.c_iflag &= ~INPCK;
    } else {
        tios.c_iflag |= INPCK;
    }

    /* Software flow control is disabled */
    tios.c_iflag &= ~(IXON | IXOFF | IXANY);

    /* C_OFLAG      Output options
       OPOST        Postprocess output (not set = raw output)
       ONLCR        Map NL to CR-NL

       ONCLR ant others needs OPOST to be enabled
    */

    /* Raw ouput */
    tios.c_oflag &= ~OPOST;

    /* C_CC         Control characters
       VMIN         Minimum number of characters to read
       VTIME        Time to wait for data (tenths of seconds)

       UNIX serial interface drivers provide the ability to
       specify character and packet timeouts. Two elements of the
       c_cc array are used for timeouts: VMIN and VTIME. Timeouts
       are ignored in canonical input mode or when the NDELAY
       option is set on the file via open or fcntl.

       VMIN specifies the minimum number of characters to read. If
       it is set to 0, then the VTIME value specifies the time to
       wait for every character read. Note that this does not mean
       that a read call for N bytes will wait for N characters to
       come in. Rather, the timeout will apply to the first
       character and the read call will return the number of
       characters immediately available (up to the number you
       request).

       If VMIN is non-zero, VTIME specifies the time to wait for
       the first character read. If a character is read within the
       time given, any read will block (wait) until all VMIN
       characters are read. That is, once the first character is
       read, the serial interface driver expects to receive an
       entire packet of characters (VMIN bytes total). If no
       character is read within the time allowed, then the call to
       read returns 0. This method allows you to tell the serial
       driver you need exactly N bytes and any read call will
       return 0 or N bytes. However, the timeout only applies to
       the first character read, so if for some reason the driver
       misses one character inside the N byte packet then the read
       call could block forever waiting for additional input
       characters.

       VTIME specifies the amount of time to wait for incoming
       characters in tenths of seconds. If VTIME is set to 0 (the
       default), reads will block (wait) indefinitely unless the
       NDELAY option is set on the port with open or fcntl.
    */
    /* Unused because we use open with the NDELAY option */
    tios.c_cc[VMIN] = 0;
    tios.c_cc[VTIME] = 0;

    if (tcsetattr(s, TCSANOW, &tios) < 0) {
        close(s);
        s = -1;
        return -1;
    }



    /***************************/
    int portfd;
    #if (__GNUC__ == 4 && __GNUC_MINOR__ == 3)
	struct my_serial_rs485 rs485conf;
	struct my_serial_rs485 rs485conf_bak;
#else
	struct serial_rs485 rs485conf;
	//struct serial_rs485 rs485conf_bak;
#endif	
    //struct termios newtios,oldtios; /*termianal settings */
    portfd=s;
/*get serial port parnms,save away */
	// tcgetattr(portfd,&newtios);
	// memcpy(&oldtios,&newtios,sizeof newtios);
	// /* configure new values */
	// cfmakeraw(&newtios); /*see man page */
	// newtios.c_iflag |=IGNPAR; /*ignore parity on input */
	// newtios.c_oflag &= ~(OPOST | ONLCR | OLCUC | OCRNL | ONOCR | ONLRET | OFILL); 
	// newtios.c_cflag = CS8 | CLOCAL | CREAD;
	// newtios.c_cc[VMIN]=1; /* block until 1 char received */
	// newtios.c_cc[VTIME]=0; /*no inter-character timer */
	/* 115200 bps */
	// cfsetospeed(&newtios,B9600);
	// cfsetispeed(&newtios,B9600);
	// /* register cleanup stuff */
	// atexit(reset_tty_atexit);
	// memset(&sa,0,sizeof sa);
	// sa.sa_handler = reset_tty_handler;
	// sigaction(SIGHUP,&sa,NULL);
	// sigaction(SIGINT,&sa,NULL);
	// sigaction(SIGPIPE,&sa,NULL);
	// sigaction(SIGTERM,&sa,NULL);
	// /*apply modified termios */
	// saved_portfd=portfd;
	// tcflush(portfd,TCIFLUSH);
	// tcsetattr(portfd,TCSADRAIN,&newtios);
	
		
	if (ioctl (portfd, TIOCGRS485, &rs485conf) < 0) 
	{
		/* Error handling.*/ 
		printf("ioctl TIOCGRS485 error.\n");
	}
	/* Enable RS485 mode: */
	rs485conf.flags |= SER_RS485_ENABLED;

	/* Set logical level for RTS pin equal to 1 when sending: */
	rs485conf.flags |= SER_RS485_RTS_ON_SEND;
	//rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;

	/* set logical level for RTS pin equal to 0 after sending: */ 
	rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
	//rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);

	/* Set rts delay after send, if needed: */
	rs485conf.delay_rts_after_send = 0x80;

	if (ioctl (portfd, TIOCSRS485, &rs485conf) < 0)
	{
		/* Error handling.*/ 
		printf("ioctl TIOCSRS485 error.\n");
	}
	else
	{
		printf("rs485conf.flags 0x%x.\n", rs485conf.flags);
		printf("rs485conf.delay_rts_before_send 0x%x.\n", rs485conf.delay_rts_before_send);
		printf("rs485conf.delay_rts_after_send 0x%x.\n", rs485conf.delay_rts_after_send);
	}
    /****************************/

    return s;
}

void serial_close(int s, struct termios *old_tios)
{
    if (s != -1) {
        tcsetattr(s, TCSANOW, old_tios);
        close(s);
    }
}

int serial_send(int s, const uint8_t *buf, int length)
{
    return write(s, buf, length);
}

int serial_receive(int s, uint8_t *buf, int bufsz, int timeout)
{
    int len = 0;
    int rc = 0;
    fd_set rset;
    struct timeval tv;

    while (bufsz > 0) {
        FD_ZERO(&rset);
        FD_SET(s, &rset);

        tv.tv_sec = timeout / 1000;
        tv.tv_usec = (timeout % 1000) * 1000;
        rc = select(s + 1, &rset, NULL, NULL, &tv);
        if (rc == -1) {
            if (errno == EINTR)
                continue;
        }

        if (rc <= 0) {
            break;
        }

        rc = read(s, buf + len, bufsz);
        if (rc <= 0) {
            break;
        }
        len += rc;
        bufsz -= rc;

        timeout = 20;
    }

    if (rc >= 0) {
        rc = len;
    }

    return rc;
}

int serial_flush(int s)
{
    if (s != -1) {
        tcflush(s, TCIOFLUSH);
    }

    return 0;
}

五、编译验证

将修改后的代码进行交叉编译,并下载到开发板。
打开测试软件,启动程序。
软件通信日志部分:
在这里插入图片描述
开发板串口打印信息:
在这里插入图片描述
程序能能够通过rs485正常获取到modbus数据。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/272059.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

SQLite 安装与使用

SQLite 安装与使用 文章目录 SQLite 安装与使用1.什么是 SQLite&#xff1f;2.为什么要用 SQLite&#xff1f;3 安装3.1 在 Windows 上安装 SQLite3.2 安装路径3.3 接下来需要配置环境变量3.4 配置完变量测试成功3.5 创建数据库成功3.6 图形化界面操作3.7 数据表的增删改查新增…

代码随想录算法训练营Day10 | 239.滑动窗口的最大值、347.前K个高频元素

LeetCode 239 滑动窗口的最大值 本题思路: 采用单调队列来完成&#xff0c;单调队列就是队列里的元素顺序&#xff0c;是单调递减/递增的情况。 那么我们应该如何维护这个单调队列呢&#xff0c;此处既然是最大值&#xff0c;那么采用的是单调递减的队列。让队列的出口处是当前…

基于JSP+Servlet+Mysql的宠物管理系统(简单增删改查)

基于JSPServletMysql的宠物管理系统_简单增删改查 一、系统介绍二、功能展示1.主页2.增加3.修改4.查询5.删除 四、其它1.其他系统实现五.获取源码 一、系统介绍 项目名称&#xff1a;基于JSPServletMysql的宠物管理系统(简单增删改查) 项目架构&#xff1a;B/S架构 开发语言…

链表的详细介绍

目录 链表的简单定义&#xff1a; 链表的分类 单项带头非循环 单向不带头循环链表 实现单向非循环无头链表 定义链表&#xff1a; 实现链表方法 打印链表 头插法&#xff1a; 尾插法&#xff1a; 指定插入&#xff1a; 通过对应值删除节点&#xff1a; 删除所有对应…

业财一体化是什么意思?有哪些好用的业财一体化软件?

你所在的企业是否为这些问题所困扰&#xff1f; 数据割裂&#xff1a;系统之间的数据不互通&#xff0c;财务数据与业务数据分离&#xff0c;数据统计口径不一致&#xff0c;缺乏关联性&#xff0c;管理统筹难度大。数据滞后&#xff1a;企业管理层获取数据信息的时效性低&…

绝缘电阻测试仪档位的选择技巧有哪些?这么一看就明白了!

电子绝缘电阻测试仪是电力检测领域的一款重要设备&#xff0c;他对于那些电力检测人员来说&#xff0c;是工作的设备之一&#xff0c;虽然它的使用频率相对较高&#xff0c;但是在使用绝缘电阻测试仪时&#xff0c;该如何选择合适的档位是一个关键问题。下面我们就来说说电子绝…

中国社科大与新加坡新跃社科联合培养博士—金融学和经济学差别

经济学和金融学是两个紧密联系的学科&#xff0c;但两者在研究问题上的侧重点有所不同。我在通过中国社科大与新加坡新跃社科联合培养博士项目课堂上&#xff0c;彻底分清了金融学和经济学差别。 经济学通常被归为社会科学&#xff0c;主要着眼于研究宏观上的生产、消费、以及…

谷歌大裁员,3 万员工面临被 AI 取代;网易、暴雪疑似「复合」!丨 RTE 开发者日报 Vol.113

开发者朋友们大家好&#xff1a; 这里是**「RTE 开发者日报」**&#xff0c;每天和大家一起看新闻、聊八卦。我们的社区编辑团队会整理分享 RTE &#xff08;Real Time Engagement&#xff09; 领域内「有话题的 新闻 」 、「有态度的 观点 」、「有意思的 数据 」、「…

Redis缓存穿透、缓存击穿、缓存雪崩介绍

一、Redis的缓存穿透 1.什么是缓存穿透&#xff1f; 缓存穿透是指&#xff1a;客户端请求的数据在缓存中和数据库中都不存在&#xff0c;这时缓存就永远不会生效&#xff0c;这些请求都打到数据库从而导致数据库压力过大。 2.出现缓存穿透的解决方案&#xff0c;以下是常用的两…

从AMI镜像恢复AWS Amazon Linux 2实例碰到的VNC服务以及Chrome浏览器无法启动的问题

文章目录 小结问题及解决VNC服务无法启动Chrome浏览器无法启动 参考 小结 将Amazon Linux 2保存为AMI (Amazon Machine Images)后&#xff0c;恢复成EC2 Instance (实例)后&#xff0c;VNC服务以及Chrome浏览器无法启动&#xff0c;进行了解决。 问题及解决 如果要将一个EC2…

Redis分布式缓存之主从哨兵分片集群

Redis主从 数据同步原理 Redis哨兵 Redis分片集群 集群伸缩&#xff1a;在集群中插入或删除某个节点 集群故障转移

HubSpot到底好不好用?

HubSpot被认为是一款强大的综合营销平台&#xff0c;然而&#xff0c;其是否适合你的业务取决于多种因素。以下是一些关于HubSpot的优点和考虑因素&#xff1a; HubSpot的优点&#xff1a; 一体化平台&#xff1a; HubSpot集成了营销、销售和服务功能&#xff0c;使得企业可以…

excel统计分析——CVM正态性检验

参考资料&#xff1a;统计推断——正态性检验&#xff08;图形方法、偏度和峰度、统计&#xff08;拟合优度&#xff09;检验&#xff09;_sm.distributions.ecdf-CSDN博客 29_张达成_从经验过程出发建立 Cramer-von Mises 统计量的性质 - 豆丁网 https://cran.r-project.org…

LeetCode 每日一题 Day 24(Hard) ||dp动态规划

1349. 参加考试的最大学生数 给你一个 m * n 的矩阵 seats 表示教室中的座位分布。如果座位是坏的&#xff08;不可用&#xff09;&#xff0c;就用 ‘#’ 表示&#xff1b;否则&#xff0c;用 ‘.’ 表示。 学生可以看到左侧、右侧、左上、右上这四个方向上紧邻他的学生的答…

解析d3dx9_43.dll文件,有效修复d3dx9_43.dll文件丢失

最近有小伙伴给小编反映说他的电脑老是出现d3dx9_43.dll文件丢失的问题&#xff0c;问为啥会这样&#xff1f;要怎么解决&#xff1f;那么今天小编就来给大家详细的解析这个d3dx9_43.dll文件吧&#xff0c;同时教大家如何去进行修复。 一. d3dx11_43.dll是什么文件有啥用 1.d3…

【数据库系统概论】第3章-关系数据库标准语言SQL(3)

文章目录 3.5 数据更新3.5.1 插入数据3.5.2 修改数据3.5.3 删除数据 3.6 空值的处理3.7 视图3.7.1 建立视图3.7.2 查询视图3.7.3 更新视图3.7.4 视图的作用 3.5 数据更新 3.5.1 插入数据 注意&#xff1a;插入数据时要满足表或者列的约束条件&#xff0c;否则插入失败&#x…

SpringBoot整合jwt(小白入门)

本文项目所用版本为&#xff1a; https://blog.csdn.net/weixin_39570751/article/details/133386557 代码仓库: https://gitee.com/skyblue0678/springboot-demo 目录 什么是JWT JWT依赖 写一个jwt工具类 测试一下jwt 优化&#xff1a;将过期时间配置在文件中 答疑&…

mysql根据条件修改字段

数据 sql语句 根据field2 字段情况&#xff0c;修改field1字段 update t1 tt1 set tt1.field1 ( case when tt1.field2 in (我家2) then 1111 when tt1.field2 in (你的家11) then 2222 else tt1.field2 end )结果

ros2+python,报错:`GLIBCXX_3.4.30‘ not found

在执行代码import rclpy&#xff0c;出现如下图所示报错 如报错信息所示&#xff0c;/home/shui/miniconda3/envs/ros2/bin/…/lib/libstdc.so.6中没有找到GLIBCXX_3.4.30’ 检查/home/shui/miniconda3/envs/ros2/bin/…/lib/的libstdc.so.6文件 cd /home/shui/miniconda3/e…