Method, device, equipment and medium for controlling 485 receiving and transmitting direction switching

文档序号:115484 发布日期:2021-10-19 浏览:20次 中文

阅读说明:本技术 一种控制485收发方向切换的方法、装置、设备及介质 (Method, device, equipment and medium for controlling 485 receiving and transmitting direction switching ) 是由 张玉良 江燕良 徐富军 张功 于 2021-05-19 设计创作,主要内容包括:本申请公开了一种控制485收发方向切换的方法,包括接收收发方向切换的指令;根据指令,利用应用层中的管理函数调用串口的485配置函数;485配置函数在驱动层为串口注册完成;根据485配置函数,利用应用层中的电平参数处理函数控制串口的电平在发送数据起始时刻和接收数据结束时刻为第一电平,并控制串口的电平在发送数据结束时刻和接收数据起始时刻为第二电平;第一电平、第二电平为高电平、低电平中的一种,且第一电平、第二电平不同。由于485配置函数在驱动层中注册完成,在切换时可以精确估计系统延迟时间,使得收发状态可以及时完成切换,避免数据丢失,提升通信稳定性和可靠性。本申请还提供具有上述优点的装置、设备及介质。(The application discloses a method for controlling 485 receiving and transmitting direction switching, which comprises the steps of receiving an instruction of receiving and transmitting direction switching; calling a 485 configuration function of the serial port by using a management function in the application layer according to the instruction; the 485 configuration function is registered for the serial port in the driving layer; according to a 485 configuration function, a level parameter processing function in an application layer is utilized to control the level of a serial port to be a first level at the data sending starting time and the data receiving ending time, and control the level of the serial port to be a second level at the data sending ending time and the data receiving starting time; the first level and the second level are one of a high level and a low level, and the first level and the second level are different. Because the 485 configuration function is registered in the driving layer, the delay time of the system can be accurately estimated during switching, so that the receiving and transmitting states can be switched in time, data loss is avoided, and the communication stability and reliability are improved. The present application also provides devices, apparatuses, and media having the above advantages.)

1. A method for controlling 485 receiving and transmitting direction switching is characterized by comprising the following steps:

receiving an instruction of switching the receiving and sending directions;

calling a 485 configuration function of the serial port by using a management function in the application layer according to the instruction; the 485 configuration function completes registration of the serial port in a driving layer;

according to the 485 configuration function, a level parameter processing function in the application layer is utilized to control the level of the serial port to be a first level at the data sending starting time and the data receiving ending time, and control the level of the serial port to be a second level at the data sending ending time and the data receiving starting time;

wherein the first level and the second level are one of a high level and a low level, and the first level and the second level are different.

2. The method of claim 1, wherein before the step of calling the 485 configuration function of the serial port by using the management function in the application layer according to the instruction, the method further comprises:

initializing the serial port;

judging whether the serial port enables a 485 mode or not;

if the serial port enables the 485 mode, judging whether the serial port supports the 485 mode;

and if the serial port supports the 485 mode, calling a 485 configuration function of the serial port by using a management function in an application layer according to the instruction.

3. The method of claim 1, wherein before the step of calling the 485 configuration function of the serial port by using the management function in the application layer, the method further comprises:

and opening the serial port in the application layer.

4. The method of any of claims 1 to 3, wherein controlling the level of the serial port to be a second level at the end of transmitting data, wherein the second level being a low level comprises:

calling an interrupt processing function which is registered in the application layer;

and calling the level processing function according to the interrupt processing function so as to pull down the level of the serial port.

5. An apparatus for controlling 485 transceiving direction switching, comprising:

the receiving module is used for receiving a command of switching the receiving and sending directions;

the first calling module is used for calling a 485 configuration function of the serial port by using a management function in the application layer according to the instruction; the 485 configuration function completes registration of the serial port in a driving layer;

the adjusting module is used for controlling the level of the serial port to be a first level at the data sending starting time and the data receiving ending time by utilizing the level parameter processing function in the application layer according to the 485 configuration function, and controlling the level of the serial port to be a second level at the data sending ending time and the data receiving starting time;

wherein the first level and the second level are one of a high level and a low level, and the first level and the second level are different.

6. The apparatus for controlling switching of 485 transmit-receive directions according to claim 5, further comprising:

the initialization module is used for initializing the serial port;

the first judging module is used for judging whether the serial port enables the 485 mode or not;

the second judgment module is used for judging whether the serial port supports the 485 mode or not if the serial port enables the 485 mode;

and the switching module is used for entering the step of receiving the instruction of switching the receiving and sending directions if the serial port supports the 485 mode.

7. The apparatus for controlling switching of 485 transmit-receive directions according to claim 5, further comprising:

and the opening module is used for opening the serial port in the application layer.

8. The apparatus of any of claims 5 to 7, further comprising:

the second calling module is used for calling the interrupt processing function which is registered in the application layer;

and the third calling module is used for calling the level processing function according to the interrupt processing function so as to reduce the level of the serial port.

9. An apparatus for controlling 485 transceiving direction switching, comprising:

a memory for storing a computer program;

a processor for implementing the steps of the method of controlling 485 transmit-receive direction switching according to any of claims 1 to 4 when executing said computer program.

10. A computer-readable storage medium, having stored thereon a computer program which, when being executed by a processor, carries out the steps of the method of controlling 485 transmit-receive direction switching according to any one of claims 1 to 4.

Technical Field

The present application relates to the field of communications technologies, and in particular, to a method, an apparatus, a device, and a computer-readable storage medium for controlling 485 transmit/receive direction switching.

Background

485 communication belongs to half-duplex communication, data can be transmitted in two directions, but one channel only allows one-way transmission at the same time, namely, the receiving and transmitting processes cannot be carried out simultaneously. Usually, three PINs are led out from a Central Processing Unit (CPU), including two UART PINs (designated as PIN _ RX and PIN _ TX) and a 485 transmit-receive direction control PIN (designated as PIN _ DIR), and these three PINs are connected to an RS485 chip on a board, and two differential signal buses "D +, D-" are led out from the 485 chip. When the CPU controls the 485 chip to switch the receiving and transmitting directions based on the Linux system, when the data transmission is finished physically, the receiving and transmitting directions cannot be switched in time after the data transmission or the data reception is finished due to the delay of the Linux system, so that the data loss is directly caused, and the communication stability and reliability are influenced.

Therefore, how to solve the above technical problems should be a great concern to those skilled in the art.

Disclosure of Invention

The application aims to provide a method, a device, equipment and a computer readable storage medium for controlling 485 receiving and transmitting direction switching, and the receiving and transmitting directions are switched in time, so that data loss is avoided.

In order to solve the above technical problem, the present application provides a method for controlling 485 receive-transmit direction switching, including:

receiving an instruction of switching the receiving and sending directions;

calling a 485 configuration function of the serial port by using a management function in the application layer according to the instruction; the 485 configuration function completes registration of the serial port in a driving layer;

according to the 485 configuration function, a level parameter processing function in the application layer is utilized to control the level of the serial port to be a first level at the data sending starting time and the data receiving ending time, and control the level of the serial port to be a second level at the data sending ending time and the data receiving starting time;

wherein the first level and the second level are one of a high level and a low level, and the first level and the second level are different.

Optionally, before the calling the 485 configuration function of the serial port by using the management function in the application layer according to the instruction, the method further includes:

initializing the serial port;

judging whether the serial port enables a 485 mode or not;

if the serial port enables the 485 mode, judging whether the serial port supports the 485 mode;

and if the serial port supports the 485 mode, calling a 485 configuration function of the serial port by using a management function in an application layer according to the instruction.

Optionally, before the using the management function in the application layer to call the 485 configuration function of the serial port, the method further includes:

and opening the serial port in the application layer.

Optionally, controlling the level of the serial port to be a second level at the end of sending data, where the second level being a low level includes:

calling an interrupt processing function which is registered in the application layer;

and calling the level processing function according to the interrupt processing function so as to pull down the level of the serial port.

The application also provides a device for controlling the 485 receiving and dispatching direction switching, which comprises:

the receiving module is used for receiving a command of switching the receiving and sending directions;

the first calling module is used for calling a 485 configuration function of the serial port by using a management function in the application layer according to the instruction; the 485 configuration function completes registration of the serial port in a driving layer;

the adjusting module is used for controlling the level of the serial port to be a first level at the data sending starting time and the data receiving ending time by utilizing the level parameter processing function in the application layer according to the 485 configuration function, and controlling the level of the serial port to be a second level at the data sending ending time and the data receiving starting time;

wherein the first level and the second level are one of a high level and a low level, and the first level and the second level are different.

Optionally, the method further includes:

the initialization module is used for initializing the serial port;

the first judging module is used for judging whether the serial port enables the 485 mode or not;

the second judgment module is used for judging whether the serial port supports the 485 mode or not if the serial port enables the 485 mode;

and the switching module is used for entering the step of receiving the instruction of switching the receiving and sending directions if the serial port supports the 485 mode.

Optionally, the method further includes:

and the opening module is used for opening the serial port in the application layer.

Optionally, the method further includes:

the second calling module is used for calling the interrupt processing function which is registered in the application layer;

and the third calling module is used for calling the level processing function according to the interrupt processing function so as to reduce the level of the serial port.

The application also provides a device for controlling the 485 receiving and dispatching direction switching, which comprises:

a memory for storing a computer program;

and the processor is used for realizing the steps of any one of the methods for controlling the 485 receiving and transmitting direction switching when executing the computer program.

The present application further provides a computer-readable storage medium, wherein a computer program is stored on the computer-readable storage medium, and when being executed by a processor, the computer program implements any of the above-mentioned steps of the method for controlling 485 transceiver direction switching.

The application provides a method for controlling 485 receiving and dispatching direction switching, which comprises the following steps: receiving an instruction of switching the receiving and sending directions; calling a 485 configuration function of the serial port by using a management function in the application layer according to the instruction; the 485 configuration function completes registration of the serial port in a driving layer; according to the 485 configuration function, a level parameter processing function in the application layer is utilized to control the level of the serial port to be a first level at the data sending starting time and the data receiving ending time, and control the level of the serial port to be a second level at the data sending ending time and the data receiving starting time; wherein the first level and the second level are one of a high level and a low level, and the first level and the second level are different.

It can be seen that, in the present application, when the transceiving direction needs to be switched, the 485 configuration function of the serial port is called through the management function in the application layer, and then the level parameter processing function located in the application layer is called according to the 485 configuration function, the level parameter processing function is used to adjust the serial port to be the first level at the data sending start time and the data receiving end time, and to be the second level at the data sending end time and the data receiving start time, one of the first level and the second level is the high level, and the other is the low level, so as to complete the switching of the receiving and sending directions of the serial port, because the 485 configuration function is registered in the driving layer, the system delay time can be accurately estimated during switching, so that the transceiving state can be timely switched, data loss is avoided, and communication stability and reliability are improved.

In addition, the application also provides a device, equipment and a computer readable storage medium with the advantages.

Drawings

For a clearer explanation of the embodiments or technical solutions of the prior art of the present application, the drawings needed for the description of the embodiments or prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments of the present application, and for those skilled in the art, other drawings can be obtained according to these drawings without creative efforts.

Fig. 1 is a flowchart of a method for controlling 485 transceiving direction switching according to an embodiment of the present disclosure;

fig. 2 is a flowchart of another method for controlling 485 transceiving direction switching according to an embodiment of the present disclosure;

fig. 3 is a block diagram of a structure of a 485 transmit-receive direction switching device according to an embodiment of the present disclosure;

fig. 4 is a block diagram of a structure of a device for controlling 485 transceiving direction switching according to an embodiment of the present application.

Detailed Description

In order that those skilled in the art will better understand the disclosure, the following detailed description will be given with reference to the accompanying drawings. It is to be understood that the embodiments described are only a few embodiments of the present application and not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.

As described in the background section, currently, when the CPU controls the 485 chip to switch the transceiving directions based on the Linux system, and when the data transmission is physically completed, the transceiving directions cannot be switched in time after the data transmission or reception is completed due to the delay of the Linux system itself, which directly causes data loss, and affects the communication stability and reliability.

In view of this, the present application provides a method for controlling 485 receiving and transmitting direction switching, please refer to fig. 1, where fig. 1 is a flowchart of a method for controlling 485 receiving and transmitting direction switching according to an embodiment of the present application, where the method includes:

step S101: and receiving an instruction of switching the transmitting and receiving directions.

Step S102: calling a 485 configuration function of the serial port by using a management function in the application layer according to the instruction; and the 485 configuration function completes the registration of the serial port in the driving layer.

The management function is at the application layer, and the management function may be an ioctl () function.

It should be noted that after receiving the instruction of switching the transmitting and receiving directions, before calling the 485 configuration function of the serial port by using the management function in the application layer, the serial port is already opened and in the 485 mode enabled state, and supports the 485 mode.

It should also be noted that the 485 configuration function is written and registered in the driver layer before the driver layer is registered, i.e. the 485 configuration function is written and registered in the driver layer.

The 485 configuration function includes two parameters, which are referred to as a first parameter and a second parameter for convenience of description. The first parameter is associated with specific serial equipment, for example, when the first parameter is A, the first parameter corresponds to the serial equipment A, and when the first parameter is B, the first parameter corresponds to the serial equipment B; and storing a second parameter RS485 in the first parameter up, wherein the second parameter represents the enabled/disabled state of the serial port, and judging whether the second parameter enables the 485 mode, for example, if the second parameter is enabled, setting the 485 direction as receiving or sending (normally, the serial port is set as receiving by default), and then registering and interrupting the level parameter processing function of the bottom half part, wherein the level parameter processing function can be a tasklet processing function.

Step S103: according to the 485 configuration function, a level parameter processing function in the application layer is utilized to control the level of the serial port to be a first level at the data sending starting time and the data receiving ending time, and control the level of the serial port to be a second level at the data sending ending time and the data receiving starting time; wherein the first level and the second level are one of a high level and a low level, and the first level and the second level are different.

The level parameter handling function in the application layer may be a tasklet handling function.

It should be noted that, when the control level is a high level or a low level, the specific process is to pull up or pull down the level parameter PIN _ DIR by using the level parameter processing function, so as to achieve the pull-up or pull-down of the level.

In general, the level of the serial port is low when receiving data, and is high when transmitting data, so in this case, the first level is high, and the second level is low. However, the present application is not limited to this, and when the level of the reception data is set to a high level and the level of the transmission data is set to a low level, the first level is a low level and the second level is a high level.

The default setting of the serial port is set as the receiving direction, the level of the serial port is low level when the serial port receives data, and the level of the serial port is high level when the serial port sends data, so that the switching condition of the serial port is explained. In the first situation, when the serial port is in the initial default receiving direction or in the receiving and sending process, the serial port is currently in the receiving direction, namely the level is in the low level, and the serial port is switched, the level is pulled up to the high level at the initial data sending moment, and the level is pulled down to the low level at the end data sending moment; in the second situation, the serial port is in the sending direction, that is, the level is at the high level, when switching is performed, the level is pulled down to the low level at the starting time of receiving data, and the level is pulled up to the high level at the ending time of receiving data.

When the receiving and sending directions need to be switched, the 485 configuration function of the serial port is called through the management function in the application layer, and then the level parameter processing function located in the application layer is called according to the 485 configuration function, the level parameter processing function is used for adjusting the serial port to be the first level at the data sending starting moment and the data receiving ending moment, the data sending ending moment and the data receiving starting moment are the second level, one of the first level and the second level is the high level, the other one is the low level, the receiving and sending directions of the serial port are switched, because the 485 configuration function is registered and completed in the driving layer, the system delay time can be accurately estimated when the serial port is switched, the receiving and sending states can be timely switched, data loss is avoided, and the communication stability and reliability are improved. The 485 communication receiving and transmitting signal waveform is tested by an oscilloscope, and the 485 receiving and transmitting direction is switched very accurately and only has microsecond-level delay.

On the basis of the foregoing embodiment, in an embodiment of the present application, before the invoking the 485 configuration function of the serial port by using the management function in the application layer according to the instruction, the method further includes:

initializing the serial port;

judging whether the serial port enables a 485 mode or not;

if the serial port enables the 485 mode, judging whether the serial port supports the 485 mode;

and if the serial port supports the 485 mode, calling a 485 configuration function of the serial port by using a management function in an application layer according to the instruction.

It should be noted that, when the determination result of determining whether the serial port enables the 485 mode is that the serial port disables the 485 mode, the step of initializing the serial port is returned.

And when the judging result of judging whether the serial port supports the 485 mode is that the serial port does not support the 485 mode, returning to the step of initializing the serial port.

Further, before the calling the 485 configuration function of the serial port by using the management function in the application layer, the method further includes:

and opening the serial port in the application layer.

Specifically, the open () function in the application layer is used to open the serial port.

On the basis of any of the foregoing embodiments, in an embodiment of the present application, when a serial port receives data, a level is a low level, and when data is transmitted, under a condition that the level is a high level, the controlling the level of the serial port to be a second level at a data transmission end time, where the second level being the low level includes:

calling an interrupt processing function which is registered in the application layer;

and calling the level processing function according to the interrupt processing function so as to pull down the level of the serial port.

The level handling function is nested in the interrupt handling function. Types of serial ports include, but are not limited to 8250, 16550.

In the embodiment, when the serial port finishes sending data, the data is sent and received in an interrupt mode, the finishing time of the serial port is judged in an interrupt processing program, the priority of the interrupt mode is high, additional cycles caused by other interrupt processing can be avoided, and the time is shortened.

The method for 485 transmit-receive direction switching in the present application is described in a specific case. Referring to fig. 2, fig. 2 is a flowchart of another method for controlling 485 transceiving direction switching according to an embodiment of the present application, where the method includes:

step 1, receiving a command of switching transmitting and receiving directions;

step 2, initializing a serial port; the serial port is set as a receiving direction by default;

step 3, judging whether the serial port enables a 485 mode or not;

step 4, if the 485 mode of the serial port is forbidden, returning to the step of initializing the serial port;

step 5, if the serial port enables the 485 mode, judging whether the serial port supports the 485 mode;

step 6, if the serial port does not support the 485 mode, returning to the step of initializing the serial port;

step 7, if the serial port supports the 485 mode, the driver layer compiles and registers a 485 configuration function;

step 8, opening a serial port by an open () function in the application layer;

step 9, when the ioctl () function in the application layer enables the serial port 485 mode, the ioctl () function calls a 485 configuration function;

step 10, a tasklet processing function in the application layer raises the PIN _ DIR at the initial time of the data sending process;

step 11, registering an interrupt processing function by an application layer;

step 12, calling an interrupt processing function;

step 13, calling a bottom half mechanism tasklet processing function in the serial8250_ tx _ chars ();

step 14, the tasklet handling function pulls down the PIN _ DIR at the end of the sending process.

In the following, the 485 control transceiving direction switching device provided in the embodiment of the present application is introduced, and the 485 control transceiving direction switching device described below and the 485 control transceiving direction switching method described above may be referred to correspondingly.

Fig. 3 is a block diagram of a structure of a 485 transceiver direction switching device according to an embodiment of the present disclosure, and referring to fig. 3, the 485 transceiver direction switching device may include:

a receiving module 100, configured to receive an instruction for switching a transceiving direction;

the first calling module 200 is configured to call a 485 configuration function of the serial port by using a management function in the application layer according to the instruction; the 485 configuration function completes registration of the serial port in a driving layer;

the adjusting module 300 is configured to control, according to the 485 configuration function, the level of the serial port to be a first level at a data sending start time and a data receiving end time by using a level parameter processing function in the application layer, and control the level of the serial port to be a second level at the data sending end time and the data receiving start time;

wherein the first level and the second level are one of a high level and a low level, and the first level and the second level are different.

The 485 control transceiving direction switching device of this embodiment is used to implement the 485 control transceiving direction switching method, and therefore, specific implementation of the 485 control transceiving direction switching device can be found in the foregoing embodiments of the 485 control transceiving direction switching method, for example, the receiving module 100, the first calling module 200, and the adjusting module 300 are respectively used to implement steps S101, S102, and S103 in the 485 control transceiving direction switching method, so that the specific implementation thereof may refer to descriptions of corresponding embodiments of each part, and will not be described again here.

Optionally, the device for controlling the 485 receiving and transmitting direction switching further includes:

the initialization module is used for initializing the serial port;

the first judging module is used for judging whether the serial port enables the 485 mode or not;

the second judgment module is used for judging whether the serial port supports the 485 mode or not if the serial port enables the 485 mode;

and the switching module is used for entering the step of receiving the instruction of switching the receiving and sending directions if the serial port supports the 485 mode.

Optionally, the device for controlling the 485 receiving and transmitting direction switching further includes:

and the opening module is used for opening the serial port in the application layer.

Optionally, the device for controlling the 485 receiving and transmitting direction switching further includes:

the second calling module is used for calling the interrupt processing function which is registered in the application layer;

and the third calling module is used for calling the level processing function according to the interrupt processing function so as to reduce the level of the serial port.

In the device for controlling the 485 receiving and sending direction switching, when the receiving and sending direction switching is needed, the 485 configuration function of the serial port is called through the management function in the application layer, and then the level parameter processing function located in the application layer is called according to the 485 configuration function, the level parameter processing function is utilized to adjust the serial port to be the first level at the data sending starting moment and the data receiving ending moment, the data sending ending moment and the data receiving starting moment are the second level, one of the first level and the second level is the high level, the other one is the low level, the receiving and sending direction switching of the serial port is completed, the 485 configuration function is registered and completed in the driving layer, the system delay time can be accurately estimated during the switching, the receiving and sending states can be timely completed, the data loss is avoided, and the communication stability and reliability are improved.

The 485 control transceiving direction switching device provided by the embodiment of the present application is introduced below, and the 485 control transceiving direction switching device described below and the 485 control transceiving direction switching method described above may be referred to in a corresponding manner.

Fig. 4 is a block diagram of a structure of a device for controlling switching of a 485 transceiving direction, provided in an embodiment of the present application, including:

a memory 11 for storing a computer program;

and the processor 12 is configured to implement the steps of the method for controlling 485 transceiving direction switching according to any one of the above embodiments when executing the computer program.

The following describes a computer-readable storage medium provided in an embodiment of the present application, and the computer-readable storage medium described below and the control 485 transceiving direction switching method described above may be referred to in correspondence.

A computer-readable storage medium, having a computer program stored thereon, where the computer program is executed by a processor to implement the steps of the method for controlling 485 transmit/receive direction switching according to any of the above embodiments.

The embodiments are described in a progressive manner, each embodiment focuses on differences from other embodiments, and the same or similar parts among the embodiments are referred to each other. The device disclosed by the embodiment corresponds to the method disclosed by the embodiment, so that the description is simple, and the relevant points can be referred to the method part for description.

Those of skill would further appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, computer software, or combinations of both, and that the various illustrative components and steps have been described above generally in terms of their functionality in order to clearly illustrate this interchangeability of hardware and software. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present application.

The steps of a method or algorithm described in connection with the embodiments disclosed herein may be embodied directly in hardware, in a software module executed by a processor, or in a combination of the two. A software module may reside in Random Access Memory (RAM), memory, Read Only Memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.

The method, apparatus, device and computer-readable storage medium for controlling 485 transceiving direction switching provided by the present application are described in detail above. The principles and embodiments of the present application are explained herein using specific examples, which are provided only to help understand the method and the core idea of the present application. It should be noted that, for those skilled in the art, it is possible to make several improvements and modifications to the present application without departing from the principle of the present application, and such improvements and modifications also fall within the scope of the claims of the present application.

13页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:工业现场的人机交互终端及控制系统

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!