[go: up one dir, main page]

CN116016011B - Master-slave communication method and system based on RS485 bus - Google Patents

Master-slave communication method and system based on RS485 bus

Info

Publication number
CN116016011B
CN116016011B CN202211438451.4A CN202211438451A CN116016011B CN 116016011 B CN116016011 B CN 116016011B CN 202211438451 A CN202211438451 A CN 202211438451A CN 116016011 B CN116016011 B CN 116016011B
Authority
CN
China
Prior art keywords
field
real
data
time
station
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202211438451.4A
Other languages
Chinese (zh)
Other versions
CN116016011A (en
Inventor
刘作斌
章云区
陈木泉
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Fujian Nebula Electronics Co Ltd
Original Assignee
Fujian Nebula Electronics Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Fujian Nebula Electronics Co Ltd filed Critical Fujian Nebula Electronics Co Ltd
Priority to CN202211438451.4A priority Critical patent/CN116016011B/en
Publication of CN116016011A publication Critical patent/CN116016011A/en
Application granted granted Critical
Publication of CN116016011B publication Critical patent/CN116016011B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02PCLIMATE CHANGE MITIGATION TECHNOLOGIES IN THE PRODUCTION OR PROCESSING OF GOODS
    • Y02P90/00Enabling technologies with a potential contribution to greenhouse gas [GHG] emissions mitigation
    • Y02P90/02Total factory control, e.g. smart factories, flexible manufacturing systems [FMS] or integrated manufacturing systems [IMS]

Landscapes

  • Small-Scale Networks (AREA)

Abstract

The invention provides a master-slave communication method and a master-slave communication system based on an RS485 bus, belonging to the technical field of industrial network control, wherein the method comprises the following steps of S10, setting a master station request frame format, a slave station response frame format and field priority; the method comprises the steps of S20, generating a data acquisition request by a master station based on a master station request frame format and a field priority, and sending the data acquisition request to a corresponding slave station based on an RS485 bus, S30, receiving and analyzing the data acquisition request by the slave station, acquiring a real-time field parameter, an extension field parameter and a first check code, checking the real-time field parameter and the extension field parameter by the first check code, and generating real-time field data and extension field data by the slave station based on the real-time field parameter and the extension field parameter, and generating a data response based on the real-time field data, the extension field data and the response frame format of the slave station and sending the data response to the master station. The invention has the advantages of greatly improving the real-time performance, expansibility and reliability of master-slave communication.

Description

Master-slave communication method and system based on RS485 bus
Technical Field
The invention relates to the technical field of industrial network control, in particular to a master-slave communication method and system based on an RS485 bus.
Background
The industrial network is generally controlled by adopting a MODBUS-RTU protocol based on an RS485 bus, the RS485 interface has higher noise suppression capability by adopting a differential transmission mode, the maximum transmission distance is about 1200 meters, the maximum transmission rate is 10Mb/s, and the industrial network also supports multi-point and two-way communication, so that a large number of industrial equipment is used as a standard interface, the MODBUS-RTU protocol is a master-slave protocol, only one master station and a plurality of slave stations can be arranged on the bus at the same moment, the Modbus communication is always initiated by the master station, data cannot be sent when the slave stations do not receive a request from the master station, the slave stations cannot communicate with each other, and the master station can only start one Modbus transaction at one time.
Based on the characteristics, the MODBUS-RTU protocol based on the RS485 bus can only adopt a one-to-one communication mode, namely, the master station inquires the state of the slave station at fixed time, and the slave station replies corresponding data after receiving a polling command of the master station, but has the following defects:
1. When a larger data volume (or average traffic volume is low but burst) needs to be transmitted, the master station requests data to the corresponding slave station through the RS485 bus, the number of data packets transmitted by the slave station is large and the speed is high, the data packets received by the master station at the same moment can exceed the bandwidth of the RS485 bus and possibly cause communication sticky packets, so that communication response errors are caused, the communication reliability is directly influenced, 2, the industrial field environment is bad and is limited by the field environment and the complexity of the application occasion of the RS485 bus, the communication bandwidth of the RS485 bus cannot be infinitely improved, for example, a network formed by the RS485 bus can only be serially wired, the practical wiring design and construction of the complex occasion can be greatly difficult, the practical communication bandwidth is influenced, the MODBUS-RTU protocol can only process 247 addresses on one data link, and the number of slave stations which can be connected to the master station is limited.
Therefore, how to provide a master-slave communication method and system based on an RS485 bus, so as to improve the real-time performance, expansibility and reliability of master-slave communication, becomes a technical problem to be solved urgently.
Disclosure of Invention
The invention aims to solve the technical problem of providing a master-slave communication method and a master-slave communication system based on an RS485 bus, which can improve the real-time performance, expansibility and reliability of master-slave communication.
In a first aspect, the present invention provides a master-slave communication method based on an RS485 bus, including the following steps:
step S10, a request frame format of a master station, a response frame format of a slave station and a field priority are set;
Step S20, the master station generates a data acquisition request based on the master station request frame format and the field priority, and sends the data acquisition request to a corresponding slave station based on an RS485 bus;
step S30, the slave station receives and analyzes the data acquisition request, acquires real-time field parameters, extension field parameters and first check codes, and checks the real-time field parameters and the extension field parameters by using the first check codes;
and step S40, the secondary station generates real-time field data and extension field data based on the real-time field parameters and the extension field parameters, generates a data response based on the real-time field data, the extension field data and the secondary station response frame format, and sends the data response to the primary station to complete communication between the primary station and the secondary station.
Further, in the step S10, the master station request frame format includes a slave station address, a function code, first data and a first check code, and the slave station response frame format includes a slave station address, a function code, second data and a second check code;
The secondary station address has a length of 1 byte, the function code has a length of 1 byte, the first data and the second data have a length range of 0 to 252 bytes, the first check code and the second check code have a length of 2 bytes, the first data comprise a real-time field starting address, a real-time field number, an extension field starting address and an extension field number, the real-time field number is larger than the extension field number, the second data comprise a real-time field reading byte number, a real-time field value, an extension field reading byte number and an extension field number value, and the first check code and the second check code are CRC16 calculated values of the first data and the second data respectively;
The field priority distinguishes whether the field type is a real-time field or an extension field.
Further, the step S20 specifically includes:
The master station distinguishes the field type of the field to be acquired based on the field priority, generates a data acquisition request based on the master station request frame format and the field type, and sends the data acquisition request to the corresponding slave station based on an RS485 bus.
Further, the step S30 specifically includes:
The secondary station receives and analyzes the data acquisition request, acquires real-time field parameters including real-time field start addresses and real-time field quantity, extension field parameters including extension field start addresses and extension field quantity and first check codes, calculates CRC16 of the real-time field parameters and the extension field parameters to obtain third check codes, judges whether the third check codes are equal to the first check codes or not, if so, the check is successful, and enters into step S40, if not, the check is failed, and the flow is ended.
Further, the step S40 specifically includes:
Based on the real-time field parameters and the extension field parameters, the secondary station generates real-time field data comprising the real-time field reading byte number and the real-time field value and extension field data comprising the extension field reading byte number and the extension field value, and performs CRC16 calculation on the real-time field data and the extension field data to obtain a second check code;
And generating a data response based on the real-time field data, the extension field data, the second check code and the response frame format of the slave station, and transmitting the data response to the master station based on an RS485 bus to complete communication between the master station and the slave station.
In a second aspect, the present invention provides a master-slave communication system based on an RS485 bus, including the following modules:
The format setting module is used for setting a master station request frame format, a slave station response frame format and a field priority;
The data acquisition request sending module is used for generating a data acquisition request by the master station based on the master station request frame format and the field priority, and sending the data acquisition request to a corresponding slave station based on an RS485 bus;
The data acquisition request verification module is used for receiving and analyzing the data acquisition request from the station, acquiring real-time field parameters, extension field parameters and first verification codes, and verifying the real-time field parameters and the extension field parameters by utilizing the first verification codes;
And the data response transmitting module is used for generating real-time field data and extension field data based on the real-time field parameters and the extension field parameters by the secondary station, generating a data response based on the real-time field data, the extension field data and the secondary station response frame format and transmitting the data response to the primary station to complete communication between the primary station and the secondary station.
Further, in the format setting module, the master station request frame format comprises a slave station address, a function code, first data and a first check code, and the slave station response frame format comprises a slave station address, a function code, second data and a second check code;
The secondary station address has a length of 1 byte, the function code has a length of 1 byte, the first data and the second data have a length range of 0 to 252 bytes, the first check code and the second check code have a length of 2 bytes, the first data comprise a real-time field starting address, a real-time field number, an extension field starting address and an extension field number, the real-time field number is larger than the extension field number, the second data comprise a real-time field reading byte number, a real-time field value, an extension field reading byte number and an extension field number value, and the first check code and the second check code are CRC16 calculated values of the first data and the second data respectively;
The field priority distinguishes whether the field type is a real-time field or an extension field.
Further, the data acquisition request sending module is specifically configured to:
The master station distinguishes the field type of the field to be acquired based on the field priority, generates a data acquisition request based on the master station request frame format and the field type, and sends the data acquisition request to the corresponding slave station based on an RS485 bus.
Further, the data acquisition request verification module is specifically configured to:
The secondary station receives and analyzes the data acquisition request, acquires real-time field parameters comprising real-time field start addresses and real-time field quantity, extension field parameters comprising extension field start addresses and extension field quantity and first check codes, calculates CRC16 of the real-time field parameters and the extension field parameters to obtain third check codes, judges whether the third check codes are equal to the first check codes or not, if so, the check is successful, the secondary station enters a data response transmitting module, if not, the check is failed, and the flow is ended.
Further, the data response sending module is specifically configured to:
Based on the real-time field parameters and the extension field parameters, the secondary station generates real-time field data comprising the real-time field reading byte number and the real-time field value and extension field data comprising the extension field reading byte number and the extension field value, and performs CRC16 calculation on the real-time field data and the extension field data to obtain a second check code;
And generating a data response based on the real-time field data, the extension field data, the second check code and the response frame format of the slave station, and transmitting the data response to the master station based on an RS485 bus to complete communication between the master station and the slave station.
The invention has the advantages that:
The field type of the field to be acquired is divided into a real-time field and an extension field by setting the format of the request frame of the master station, the format of the response frame of the slave station and the field priority, the real-time field is queried in real time, the extension field is queried in a polling way, the length of the total transmission frame (data acquisition request and data response) is further shortened, the communication period is shortened, the message transmission efficiency is improved, communication response errors caused by communication sticking are avoided, the extension field can be set as required, and the real-time performance, the expansibility and the reliability of master-slave communication are improved greatly finally.
Drawings
The invention will be further described with reference to examples of embodiments with reference to the accompanying drawings.
Fig. 1 is a flow chart of a master-slave communication method based on an RS485 bus.
Fig. 2 is a schematic structural diagram of a master-slave communication system based on an RS485 bus according to the invention.
Fig. 3 is a schematic flow chart of the interaction of the master station and the slave station of the present invention.
Fig. 4 is a signaling diagram of the interaction of the master and slave stations of the present invention.
Detailed Description
The technical scheme of the embodiment of the application has the general idea that the field types of the field to be acquired are divided into a real-time field and an extension field, the real-time field is queried in real time, the extension field is queried in a polling way to shorten the length of a total transmission frame, avoid communication from sticking packets, and the extension field can be set as required to improve the real-time performance, expansibility and reliability of master-slave communication.
Referring to fig. 1 to 4, a preferred embodiment of a master-slave communication method based on an RS485 bus according to the present invention includes the following steps:
step S10, a request frame format of a master station, a response frame format of a slave station and a field priority are set;
Step S20, the master station generates a data acquisition request based on the master station request frame format and the field priority, and sends the data acquisition request to a corresponding slave station based on an RS485 bus, namely the master station divides a field to be acquired into a real-time field and an expansion field based on the field priority, simultaneously carries out real-time query on each real-time field, and carries out polling query on each expansion field in sequence;
step S30, the slave station receives and analyzes the data acquisition request, acquires real-time field parameters, extension field parameters and first check codes, and checks the real-time field parameters and the extension field parameters by using the first check codes;
and step S40, the secondary station generates real-time field data and extension field data based on the real-time field parameters and the extension field parameters, generates a data response based on the real-time field data, the extension field data and the secondary station response frame format, and sends the data response to the primary station to complete communication between the primary station and the secondary station.
In the step S10, the master station request frame format includes a slave station address, a function code, first data and a first check code, and the slave station response frame format includes a slave station address, a function code, second data and a second check code;
The secondary station address has a length of 1 byte, the function code has a length of 1 byte, the first data and the second data have a length ranging from 0 to 252 bytes, the first check code and the second check code have a length of 2 bytes, the first data comprises a real-time field starting address, a real-time field number, an extended field starting address and an extended field number, the real-time field number is larger than the extended field number so as to ensure real-time performance, the second data comprises a real-time field reading byte number, a real-time field value, an extended field reading byte number and an extended field value, and the first check code and the second check code are CRC16 calculated values of the first data and the second data respectively;
The field priority distinguishes whether the field type is a real-time field or an extension field.
The frame format of the conventional MODBUS-RTU protocol does not distinguish between the real-time field and the extension field, and the format is as follows:
secondary station address Function code Data CRC check
1 Byte 1 Byte 0-252 Bytes 2 Bytes
The step S20 specifically includes:
The master station distinguishes the field type of the field to be acquired based on the field priority, generates a data acquisition request based on the master station request frame format and the field type, and sends the data acquisition request to the corresponding slave station based on an RS485 bus. The data acquisition request only queries one field at a time, namely the data acquisition request only queries one field with the field type being an extension field, so as to ensure the real-time performance of communication, for example, the real-time performance requirements of fields such as voltage, current and the like are higher, the real-time query is required, and the real-time performance requirements of the fields such as temperature, capacity and energy are not high, the polling query can be performed, namely the temperature is queried only for the first time, the capacity is queried only for the second time, and the like.
The step S30 specifically includes:
The secondary station receives and analyzes the data acquisition request, acquires real-time field parameters including real-time field start addresses and real-time field quantity, extension field parameters including extension field start addresses and extension field quantity and first check codes, calculates CRC16 of the real-time field parameters and the extension field parameters to obtain third check codes, judges whether the third check codes are equal to the first check codes or not, if so, the check is successful, and enters into step S40, if not, the check is failed, and the flow is ended.
The step S40 specifically includes:
Based on the real-time field parameters and the extension field parameters, the secondary station generates real-time field data comprising the real-time field reading byte number and the real-time field value and extension field data comprising the extension field reading byte number and the extension field value, and performs CRC16 calculation on the real-time field data and the extension field data to obtain a second check code;
And generating a data response based on the real-time field data, the extension field data, the second check code and the response frame format of the slave station, and transmitting the data response to the master station based on an RS485 bus to complete communication between the master station and the slave station.
The invention relates to a preferred embodiment of a master-slave communication system based on an RS485 bus, which comprises the following modules:
The format setting module is used for setting a master station request frame format, a slave station response frame format and a field priority;
The data acquisition request sending module is used for generating a data acquisition request by the master station based on the master station request frame format and the field priority, and sending the data acquisition request to a corresponding slave station based on an RS485 bus, namely, the master station divides a field to be acquired into a real-time field and an extension field based on the field priority, simultaneously carries out real-time inquiry on each real-time field, and carries out polling inquiry on each extension field in sequence;
The data acquisition request verification module is used for receiving and analyzing the data acquisition request from the station, acquiring real-time field parameters, extension field parameters and first verification codes, and verifying the real-time field parameters and the extension field parameters by utilizing the first verification codes;
And the data response transmitting module is used for generating real-time field data and extension field data based on the real-time field parameters and the extension field parameters by the secondary station, generating a data response based on the real-time field data, the extension field data and the secondary station response frame format and transmitting the data response to the primary station to complete communication between the primary station and the secondary station.
The format setting module is used for setting a format of a request frame of a master station, wherein the format of the request frame of the master station comprises a slave station address, a function code, first data and a first check code;
The secondary station address has a length of 1 byte, the function code has a length of 1 byte, the first data and the second data have a length ranging from 0 to 252 bytes, the first check code and the second check code have a length of 2 bytes, the first data comprises a real-time field starting address, a real-time field number, an extended field starting address and an extended field number, the real-time field number is larger than the extended field number so as to ensure real-time performance, the second data comprises a real-time field reading byte number, a real-time field value, an extended field reading byte number and an extended field value, and the first check code and the second check code are CRC16 calculated values of the first data and the second data respectively;
The field priority distinguishes whether the field type is a real-time field or an extension field.
The frame format of the conventional MODBUS-RTU protocol does not distinguish between the real-time field and the extension field, and the format is as follows:
secondary station address Function code Data CRC check
1 Byte 1 Byte 0-252 Bytes 2 Bytes
The data acquisition request sending module is specifically configured to:
The master station distinguishes the field type of the field to be acquired based on the field priority, generates a data acquisition request based on the master station request frame format and the field type, and sends the data acquisition request to the corresponding slave station based on an RS485 bus. The data acquisition request only queries one field at a time, namely the data acquisition request only queries one field with the field type being an extension field, so as to ensure the real-time performance of communication, for example, the real-time performance requirements of fields such as voltage, current and the like are higher, the real-time query is required, and the real-time performance requirements of the fields such as temperature, capacity and energy are not high, the polling query can be performed, namely the temperature is queried only for the first time, the capacity is queried only for the second time, and the like.
The data acquisition request verification module is specifically configured to:
The secondary station receives and analyzes the data acquisition request, acquires real-time field parameters comprising real-time field start addresses and real-time field quantity, extension field parameters comprising extension field start addresses and extension field quantity and first check codes, calculates CRC16 of the real-time field parameters and the extension field parameters to obtain third check codes, judges whether the third check codes are equal to the first check codes or not, if so, the check is successful, the secondary station enters a data response transmitting module, if not, the check is failed, and the flow is ended.
The data response transmitting module is specifically configured to:
Based on the real-time field parameters and the extension field parameters, the secondary station generates real-time field data comprising the real-time field reading byte number and the real-time field value and extension field data comprising the extension field reading byte number and the extension field value, and performs CRC16 calculation on the real-time field data and the extension field data to obtain a second check code;
And generating a data response based on the real-time field data, the extension field data, the second check code and the response frame format of the slave station, and transmitting the data response to the master station based on an RS485 bus to complete communication between the master station and the slave station.
To facilitate an understanding of the present invention, the following are examples of master and slave interactions:
when a host initiates a data acquisition request, a slave communicates in real time according to a master station request frame format and a slave station response frame format to ensure that a data response returned to the host accords with a MODBUS-RTU protocol, and the method specifically comprises the following steps:
s001 [ voltage 1, voltage 2, current ] [ expansion 1: temperature ]
S002: [ Voltage 1, voltage 2, current ] [ expansion 2: capacity ]
...
SOOn [ Voltage 1, voltage 2, current ] [ expansion n: energy ]
Wherein, voltage 1, voltage 2 and current are real-time fields, each round of inquiry reply process should contain the fields, and temperature, capacity and energy are expansion fields. When the master station inquires the non-real-time fields, only 1 expansion field is inquired in each round (the actual application configures the number of the expansion fields according to the needs), and the inquiry is restarted after the n-theory inquiry is finished.
When the data acquisition request sent by the host is a register inquiry request, the first round of inquiry request may be that the ID of the host is 1 (corresponding to byte Ox 01), the real-time field and the extension field are respectively read (the function code is 0x 03), the real-time fields [ voltage 1], [ voltage 2] and [ current ] to be read are assumed to be continuous data stored in three registers, the start address Ox00 x01, the extension field [ temperature ], [ capacity ] and the energy start address are respectively 0x00 0x10,0x00 0x11,0x00 0x12.
In the S001 process, the data acquisition request sent by the host is "0x01 0x03 0x00 0x01 0x00 0x03 0x00 0x10 0x00 0x01 0x x0 x", where the meaning of the data acquisition request is "3 consecutive register data (real-time fields [ voltage 1], [ voltage 2], [ current ]) beginning with 0x00 x01 address to be read and 1 register data (extension field [ temperature ]) beginning with 0x00 x10, and the last two bytes are check codes calculated by CRC 16.
Similarly, the data acquisition request sent by the host in the S002 process is "Ox01 0x03 0x00 0x01 0x00 0x03 0x00 0x11 0x00 0x01 0x x0 x", where the meaning of the data acquisition request is "3 consecutive register data (real-time fields [ voltage 1], [ voltage 2], [ current ]) beginning with 0x00 x01 address to be read and 1 register data (extension field [ capacity ]) beginning with 0x00 x11, and the last two bytes are check codes calculated by CRC 16.
When the data response sent by the slave machine is a register data reply instruction, the data of [ voltage 1], [ voltage 2], [ current ] in the first round of reply instruction are respectively '0 x1E 0x 1F', '0 x2E 0x 2F', '0 x3E 0x 3F', the data of the extension field [ temperature ] is '0 x4E 0x 4F', the instruction can be '0x01 0x03 0x06 0x1E 0x1F 0x2E 0x2F 0x3E 0x3F 0x02 0x4E 0x4F 0x x0 x', '0 x 06' is the byte number of the read real-time field, the total number of the 3 real-time field registers is 6 bytes, the '0x1E 0x1F 0x2E 0x2F 0x3E 0x3F' is the real-time field data, the '0 x 02' is the byte number of the read extension field, the total number of the 1 extension field registers is 2 bytes, and the '0 x4E 0x 4F' is the extension field data.
Similarly, in the second round of instruction, the [ voltage 1], [ voltage 2], [ current ] data are respectively "0×5e0x5f", "0×6e0x6f", "Ox7E 0x7F", and the data of the extension field [ capacity ] is "0×8e0x8f", and then the instruction may be "0x01 0x03 0x06 0x5E 0x5F 0x6E 0x6F 0x7E 0x7F 0x02 0x8E 0x8F 0x ×0xx", "0x06" is the byte number of the read real-time field, the total number of 3 real-time field registers is 6 bytes, the "0x5E 0x5F 0x6E 0x6F 0x7E 0x7F" is the real-time field data, the "0x02" is the byte number of the read extension field, the total number of 1 extension field registers is 2 bytes, and the "0×8e0x8f" is the extension field data.
The S00n process polling is analogized based on the above, and it should be noted that none of the above applied register addresses can be repeated, otherwise the transferred register values will be confused.
In summary, the invention has the advantages that:
The field type of the field to be acquired is divided into a real-time field and an extension field by setting the format of the request frame of the master station, the format of the response frame of the slave station and the field priority, the real-time field is queried in real time, the extension field is queried in a polling way, the length of the total transmission frame (data acquisition request and data response) is further shortened, the communication period is shortened, the message transmission efficiency is improved, communication response errors caused by communication sticking are avoided, the extension field can be set as required, and the real-time performance, the expansibility and the reliability of master-slave communication are improved greatly finally.
While specific embodiments of the invention have been described above, it will be appreciated by those skilled in the art that the specific embodiments described are illustrative only and not intended to limit the scope of the invention, and that equivalent modifications and variations of the invention in light of the spirit of the invention will be covered by the claims of the present invention.

Claims (2)

1.一种基于RS485总线的主从通信方法,其特征在于:包括如下步骤:1. A master-slave communication method based on RS485 bus, characterized in that it comprises the following steps: 步骤S10、设定一主站请求帧格式、一从站响应帧格式以及一字段优先级;Step S10, setting a master station request frame format, a slave station response frame format and a field priority; 所述主站请求帧格式包括从站地址、功能码、第一数据以及第一校验码;所述从站响应帧格式包括从站地址、功能码、第二数据以及第二校验码;The master station request frame format includes the slave station address, function code, first data and first check code; the slave station response frame format includes the slave station address, function code, second data and second check code; 所述从站地址的长度为1字节;所述功能码的长度为1字节;所述第一数据以及第二数据的长度范围均为0至252字节;所述第一校验码以及第二校验码的长度均为2字节;所述第一数据包括实时字段起始地址、实时字段数量、扩展字段起始地址、扩展字段数量;所述实时字段数量大于扩展字段数量;所述第二数据包括实时字段读取字节数、实时字段取值、扩展字段读取字节数、扩展字段数取值;所述第一校验码以及第二校验码分别为第一数据和第二数据的CRC16计算值;The length of the slave address is 1 byte; the length of the function code is 1 byte; the length range of the first data and the second data are both 0 to 252 bytes; the length of the first check code and the second check code are both 2 bytes; the first data includes the real-time field starting address, the number of real-time fields, the extension field starting address, and the number of extension fields; the number of real-time fields is greater than the number of extension fields; the second data includes the number of bytes read in the real-time field, the value of the real-time field, the number of bytes read in the extension field, and the value of the number of extension fields; the first check code and the second check code are the CRC16 calculated values of the first data and the second data respectively; 所述字段优先级用区分字段类型为实时字段还是扩展字段;The field priority is used to distinguish whether the field type is a real-time field or an extended field; 步骤S20、主站基于所述字段优先级区分待获取字段的字段类型,基于所述主站请求帧格式以及字段类型生成一数据获取请求,并将所述数据获取请求基于RS485总线发送给对应的从站;Step S20: The master station distinguishes the field type of the field to be acquired based on the field priority, generates a data acquisition request based on the master station request frame format and field type, and sends the data acquisition request to the corresponding slave station based on the RS485 bus; 步骤S30、从站接收并解析所述数据获取请求,获取包括实时字段起始地址和实时字段数量的实时字段参数、包括扩展字段起始地址和扩展字段数量的扩展字段参数以及第一校验码,对所述实时字段参数以及扩展字段参数进行CRC16计算得到第三校验码,判断所述第三校验码与第一校验码是否相等,若是,则校验成功,并进入步骤S40;若否,则校验失败,并结束流程;Step S30: The slave station receives and parses the data acquisition request, obtains real-time field parameters including a real-time field starting address and a number of real-time fields, extended field parameters including an extended field starting address and a number of extended fields, and a first check code, performs a CRC16 calculation on the real-time field parameters and the extended field parameters to obtain a third check code, and determines whether the third check code is equal to the first check code. If so, the check succeeds, and the process proceeds to step S40; if not, the check fails, and the process ends. 步骤S40、从站基于所述实时字段参数以及扩展字段参数,生成包括实时字段读取字节数和实时字段取值的实时字段数据以及包括扩展字段读取字节数和扩展字段数取值的扩展字段数据,对所述实时字段数据以及扩展字段数据进行CRC16计算得到第二校验码;Step S40: The slave station generates, based on the real-time field parameters and the extended field parameters, real-time field data including the number of bytes read in the real-time field and the real-time field value, and extended field data including the number of bytes read in the extended field and the extended field value, and performs a CRC16 calculation on the real-time field data and the extended field data to obtain a second check code. 基于所述实时字段数据、扩展字段数据、第二校验码以及从站响应帧格式生成一数据应答,将所述数据应答基于RS485总线发送给主站,完成主站和从站的通信。A data response is generated based on the real-time field data, the extended field data, the second check code and the slave station response frame format, and the data response is sent to the master station based on the RS485 bus to complete the communication between the master station and the slave station. 2.一种基于RS485总线的主从通信系统,其特征在于:包括如下模块:2. A master-slave communication system based on RS485 bus, characterized by comprising the following modules: 格式设定模块,用于设定一主站请求帧格式、一从站响应帧格式以及一字段优先级;A format setting module, used to set a master station request frame format, a slave station response frame format and a field priority; 所述主站请求帧格式包括从站地址、功能码、第一数据以及第一校验码;所述从站响应帧格式包括从站地址、功能码、第二数据以及第二校验码;The master station request frame format includes the slave station address, function code, first data and first check code; the slave station response frame format includes the slave station address, function code, second data and second check code; 所述从站地址的长度为1字节;所述功能码的长度为1字节;所述第一数据以及第二数据的长度范围均为0至252字节;所述第一校验码以及第二校验码的长度均为2字节;所述第一数据包括实时字段起始地址、实时字段数量、扩展字段起始地址、扩展字段数量;所述实时字段数量大于扩展字段数量;所述第二数据包括实时字段读取字节数、实时字段取值、扩展字段读取字节数、扩展字段数取值;所述第一校验码以及第二校验码分别为第一数据和第二数据的CRC16计算值;The length of the slave address is 1 byte; the length of the function code is 1 byte; the length range of the first data and the second data are both 0 to 252 bytes; the length of the first check code and the second check code are both 2 bytes; the first data includes the real-time field starting address, the number of real-time fields, the extension field starting address, and the number of extension fields; the number of real-time fields is greater than the number of extension fields; the second data includes the number of bytes read in the real-time field, the value of the real-time field, the number of bytes read in the extension field, and the value of the number of extension fields; the first check code and the second check code are the CRC16 calculated values of the first data and the second data respectively; 所述字段优先级用区分字段类型为实时字段还是扩展字段;The field priority is used to distinguish whether the field type is a real-time field or an extended field; 数据获取请求发送模块,用于主站基于所述字段优先级区分待获取字段的字段类型,基于所述主站请求帧格式以及字段类型生成一数据获取请求,并将所述数据获取请求基于RS485总线发送给对应的从站;A data acquisition request sending module is used for the master station to distinguish the field type of the field to be acquired based on the field priority, generate a data acquisition request based on the master station request frame format and field type, and send the data acquisition request to the corresponding slave station based on the RS485 bus; 数据获取请求校验模块,用于从站接收并解析所述数据获取请求,获取包括实时字段起始地址和实时字段数量的实时字段参数、包括扩展字段起始地址和扩展字段数量的扩展字段参数以及第一校验码,对所述实时字段参数以及扩展字段参数进行CRC16计算得到第三校验码,判断所述第三校验码与第一校验码是否相等,若是,则校验成功,并进入数据应答发送模块;若否,则校验失败,并结束流程;a data acquisition request verification module, configured to receive and parse the data acquisition request from the station, obtain real-time field parameters including a real-time field starting address and a number of real-time fields, extended field parameters including an extended field starting address and a number of extended fields, and a first verification code, perform a CRC16 calculation on the real-time field parameters and the extended field parameters to obtain a third verification code, and determine whether the third verification code is equal to the first verification code; if so, the verification is successful, and the process enters the data response sending module; if not, the verification fails, and the process ends; 数据应答发送模块,用于从站基于所述实时字段参数以及扩展字段参数,生成包括实时字段读取字节数和实时字段取值的实时字段数据以及包括扩展字段读取字节数和扩展字段数取值的扩展字段数据,对所述实时字段数据以及扩展字段数据进行CRC16计算得到第二校验码;a data response sending module, configured to generate, from the station, real-time field data including the number of bytes read in the real-time field and the value of the real-time field, and extended field data including the number of bytes read in the extended field and the value of the extended field number, based on the real-time field parameters and the extended field parameters, and perform CRC16 calculation on the real-time field data and the extended field data to obtain a second check code; 基于所述实时字段数据、扩展字段数据、第二校验码以及从站响应帧格式生成一数据应答,将所述数据应答基于RS485总线发送给主站,完成主站和从站的通信。A data response is generated based on the real-time field data, the extended field data, the second check code and the slave station response frame format, and the data response is sent to the master station based on the RS485 bus to complete the communication between the master station and the slave station.
CN202211438451.4A 2022-11-16 2022-11-16 Master-slave communication method and system based on RS485 bus Active CN116016011B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211438451.4A CN116016011B (en) 2022-11-16 2022-11-16 Master-slave communication method and system based on RS485 bus

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211438451.4A CN116016011B (en) 2022-11-16 2022-11-16 Master-slave communication method and system based on RS485 bus

Publications (2)

Publication Number Publication Date
CN116016011A CN116016011A (en) 2023-04-25
CN116016011B true CN116016011B (en) 2025-09-16

Family

ID=86036081

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211438451.4A Active CN116016011B (en) 2022-11-16 2022-11-16 Master-slave communication method and system based on RS485 bus

Country Status (1)

Country Link
CN (1) CN116016011B (en)

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105404181A (en) * 2015-10-20 2016-03-16 卧龙电气集团股份有限公司 Multifunctional communication method for integrated variable frequency motor
CN111478838A (en) * 2020-04-08 2020-07-31 浙江中控研究院有限公司 High-efficiency high-bandwidth communication method and system

Family Cites Families (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103546467B (en) * 2013-10-23 2016-08-17 上海爱控自动化设备有限公司 The method applying Modbus rtu protocol on TCP/IP network
US11343117B2 (en) * 2017-08-08 2022-05-24 Volkswagen Aktiengesellschaft Method for transmitting data via a serial communication bus, correspondingly designed bus interface, and correspondingly designed computer program
CN110716977A (en) * 2019-10-17 2020-01-21 广东绿腾新能源服务有限公司 Energy data linking method and device based on block chain
CN112503717B (en) * 2020-11-02 2022-04-08 珠海格力电器股份有限公司 Control method and device of universal air conditioner controller and universal air conditioner controller

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105404181A (en) * 2015-10-20 2016-03-16 卧龙电气集团股份有限公司 Multifunctional communication method for integrated variable frequency motor
CN111478838A (en) * 2020-04-08 2020-07-31 浙江中控研究院有限公司 High-efficiency high-bandwidth communication method and system

Also Published As

Publication number Publication date
CN116016011A (en) 2023-04-25

Similar Documents

Publication Publication Date Title
CN108965484B (en) Internet of things data transmission method, system and terminal
CN110177013B (en) A Design and Implementation Method of EtherCAT Master-Slave Station Based on FPGA
CN112995133B (en) Analysis method, device and system based on modbus protocol
CA2362961C (en) Bridge for can to tcp/ip connection
EP2618544A1 (en) Ethernet communication system and method based on mmc/sd interface
CN113741360B (en) Industrial control gateway, system, control method and storage medium
EP4184831A1 (en) Data collection method and apparatus, modbus system, and storage medium
CN111464419A (en) A data transmission control method based on bus network communication
JP2019534609A (en) Network management information receiving / transmitting method, apparatus, transmitting apparatus, and receiving apparatus
EP3422655B1 (en) Method for managing address of meter device, and acquisition terminal and meter device
CN118158020A (en) Communication system based on Modbus protocol
CN112491620B (en) Multi-channel data transmission controller based on SRIO and adjusting method
CN117596313A (en) Multi-protocol acquisition method for Internet of things system
CN116016011B (en) Master-slave communication method and system based on RS485 bus
CN106953809B (en) 485 time-sharing communication-based equipment resource acquisition method
CN118413583B (en) IPMI protocol rapid transmission method and related equipment
CN118210751B (en) Communication method of BMS protocol based on two-in-one of CAN and RS485 bus
CN110768749A (en) Data transmission method and device and power peak shaving system
CN113938351A (en) Data acquisition method, system and computer readable storage medium
WO2012111229A1 (en) Communication system
CN103780412B (en) Realization method and apparatus for virtual southbound interface and internet of thing management platform
CN117424774A (en) Communication anti-interference method and device, electronic equipment and storage medium
CN120528733B (en) A method and apparatus for communication between a coupler and a host computer for hot parameter configuration.
US20180191562A1 (en) Parameter setting method for system bus and device thereof
CN115633102A (en) Multi-type protocol message processing system, method and readable storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant