Disclosure of Invention
In view of this, embodiments of the present invention provide a data transmission device and method to solve the problem of compatibility between serial port communication and network port communication in the same embedded system.
According to a first aspect, an embodiment of the present invention provides a data transmission apparatus, including:
the serial port communication unit is provided with at least one serial port;
a network port communication unit having at least one network port;
the control unit is respectively connected with the serial port communication unit and the network port communication unit; the control unit is used for converting first serial port data received by the at least one serial port into first internet access data and outputting the first internet access data from the at least one internet access; and the control unit is also used for converting the second network port data received by the at least one network port into second serial port data and outputting the second serial port data from the at least one serial port.
According to the data transmission device provided by the embodiment of the invention, the serial port communication unit and the network port communication unit are integrated in the device and used for transmitting serial port data and network port data, and the control unit is used for converting the serial port data into the network port data and converting the network port data into the serial port data, so that the problem of transmission of the serial port data and the network port data can be realized through the device, when the data transmission device is subsequently applied to an embedded system, the original circuit and software system do not need to be changed, and the problem of compatibility of the serial port communication and the network port communication can be better solved by adopting a plug-and-play strategy.
With reference to the first aspect, in a first implementation manner of the first aspect, the serial port communication unit includes:
and the level conversion circuit is respectively connected with the at least one serial port and the control unit.
According to the data transmission device provided by the embodiment of the invention, the working voltages between the upper computer in communication with the serial port and the control unit are different, so that the level conversion circuit arranged in the serial port communication unit can meet the normal working requirement of the serial port communication unit.
With reference to the first aspect or the first implementation manner of the first aspect, in a second implementation manner of the first aspect, the portal communication unit includes:
the clock circuit is connected with the clock interface of the network card chip;
and the network card chip is connected with the control unit.
The Ethernet peripheral circuit is connected with the network card chip;
according to the data transmission device provided by the embodiment of the invention, the Ethernet peripheral circuit is adopted in the network port communication unit to transmit the network port data, namely, the data is transmitted in a direct memory access mode, so that the data is directly accessed from the memory without passing through the CPU in the data transmission, and the resource occupancy rate of the CPU is reduced.
With reference to the first aspect, in a third implementation manner of the first aspect, the control unit includes:
the power supply circuit is used for supplying working power to the controller;
and the controller is respectively connected with the serial port communication unit and the network port communication unit.
According to a second aspect, an embodiment of the present invention further provides a data transmission method, including:
acquiring first serial port data from at least one serial port of a serial port communication unit, and/or acquiring second network port data from at least one network port of a network port communication unit;
and converting the first serial port data into first internet port data and outputting the first internet port data from the at least one internet port, and/or converting the second internet port data into second serial port data and outputting the second serial port data from the at least one serial port.
The data transmission method provided by the embodiment of the invention can be applied to a data transmission device, and the serial port data is converted into the network port data and the network port data is converted into the serial port data, so that the transmission problem of the serial port data and the network port data can be realized through the device.
With reference to the second aspect, in a first embodiment of the second aspect, the converting the first serial data into first portal data and outputting the first portal data from the at least one portal includes:
receiving the first serial port data from the at least one serial port in an interrupt mode;
analyzing the first serial port data according to a first preset protocol to obtain first internet port data;
and outputting the first internet access data through the at least one internet access.
With reference to the first embodiment of the second aspect, in the second embodiment of the second aspect, the outputting the first portal data through the at least one portal includes:
and outputting the first internet access data through the at least one internet access by using a uIP protocol stack.
The data transmission method provided by the embodiment of the invention can be operated on a bare board without an operating system based on the operation of the uIP protocol stack.
With reference to the second aspect, or the first implementation manner of the second aspect, or the second implementation manner of the second aspect, in a third implementation manner of the second aspect, the converting the second internet access data into second serial data to be output from the at least one serial port includes:
receiving the second internet access data from the at least one internet access by adopting a query mode;
analyzing the second internet access data according to a second preset protocol to obtain second serial port data;
and outputting the second serial port data through the at least one serial port.
With reference to the third implementation manner of the second aspect, in a fourth implementation manner of the second aspect, the receiving, by using a query, the second portal data from the at least one portal includes:
and receiving the second internet access data from the at least one internet access by using a uIP protocol stack in a query mode.
With reference to the fourth embodiment of the second aspect, in a fifth embodiment of the second aspect, the outputting the second serial port data through the at least one serial port includes:
and outputting the second serial port data through the at least one serial port in a direct memory access mode.
According to the data transmission method provided by the embodiment of the invention, the second serial port data is output by adopting a direct memory access method, so that the data is directly accessed from the memory without passing through the CPU in the data transmission process, and the resource occupancy rate of the CPU is reduced.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. 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 invention.
It should be noted that the data communication device described in the embodiment of the present invention is used in a circuit system, and realizes compatibility between serial port communication and network port communication. The data communication device is provided with a serial port and a network port, so that the serial port of the data communication device can be connected with the serial port communication equipment in a circuit system which needs to solve the problem of compatibility between serial port communication and network port communication, the network port is connected with the network port communication equipment, the structure of the original circuit system does not need to be changed, and plug and play are realized.
An embodiment of the present invention provides a data communication device, as shown in fig. 1, the data communication device includes a serial port communication unit 10, a network port communication unit 20, and a control unit 30. The control unit 30 is connected to the serial port communication unit 10 and the network port communication unit 20, respectively, and is configured to implement interconversion between serial port data and network port data.
Specifically, the serial communication unit 10 has at least one serial port, and each serial port may be used to transmit serial data. For example, the number of serial ports may be 1, 2, 3, or 5, etc., and the number of serial ports of the serial port communication unit 10 is not limited at all, and may be set accordingly according to actual situations.
The serial port communication unit 10 may further include other auxiliary circuits, such as a level shift circuit, on the basis of having at least one serial port. The specific structure of the serial communication unit 10 will be described in detail below.
In this embodiment, for example, the serial communication unit 10 has 5 serial ports, and the control unit may attach a source of the serial data to the data packet when converting the serial data, for example, may add an identifier corresponding to the serial port one to one. When a serial port receives the first serial port data, the control unit 30 converts the first serial port data into first internet port data, and outputs the first internet port data through the internet port of the internet port communication unit 20. When the control unit 30 converts the serial port data into the network port data, the serial port data may be analyzed first and then converted into the network port data for output. The specific conversion process will be described in detail in the following data transmission method.
The internet access communication unit 20 has at least one internet access, and each internet access can be used for transmitting internet access data. For example, the number of the network ports may be 1, 2, or 3, and so on, and the number of the network ports of the network port communication unit 20 is not limited at all, and may be set accordingly according to actual situations.
The portal communication unit 20 may further include other auxiliary circuits such as a power circuit, an outlet circuit, and the like, in addition to the at least one portal. The specific structure of the portal communication unit 20 will be described in detail below.
In this embodiment, taking the example that the network port communication unit 20 has one network port, after the network port receives the second network port data, the control unit 30 converts the second network port data into second serial data, and outputs the second serial data by using the serial port of the serial communication unit 10. When the control unit 30 converts the second network port data into the second serial port data, the second network port data may be analyzed first and then converted into the second serial port data to be output. The specific conversion process will be described in detail in the following data transmission method.
The control unit 10 shown in fig. 1 is used to implement interconversion between serial port data and network port data, and may be implemented by a programmable logic control device, or by other controllers, where the specific form of the control unit 10 is not limited at all. A data conversion program is stored in a storage module of the controller, and the control unit 10 converts received serial port data or network port data based on the stored data conversion program. The specific conversion will be described in detail below.
The data transmission device that this embodiment provided, serial communication unit and net gape communication unit have integrateed in the device are used for transmitting serial data and net gape data, and utilize the control unit to carry out serial data to net gape data, and net gape data changes serial data, therefore, can realize the transmission problem of serial data and net gape data through the device, follow-up when using this data transmission device to embedded system in, need not change original circuit and software system, adopt the compatible problem of plug-and-play's strategy can better solution serial communication and net gape communication.
In some optional implementations of this embodiment, as shown in fig. 2, the serial port communication unit includes a level shifter circuit and a serial port. The level shift circuit in fig. 2 is MAX232 as an example, but the protection scope of the present invention is not limited thereto, and may be other types of level shift circuits, which are not limited herein, and only need to implement normal communication between the host computer in communication with the serial port and the control unit 10. For example, the typical working voltage of the upper computer communicating with the RS232 serial port is 3V to 12V, -3V to-12V, while the working voltage of the control unit 10 is TTL level, and for the convenience of subsequent user debugging, a level conversion circuit is connected with the serial port UART1 for communicating with the upper computer.
It should be noted that the type of the capacitor connected to the level shift circuit MAX232 in fig. 2 may be set according to actual conditions, and fig. 2 is only an alternative embodiment, but the scope of the present invention is not limited thereto.
In other optional implementations of this embodiment, the network port communication unit includes a clock circuit, a network card chip, and an ethernet peripheral circuit. The clock circuit is used for providing an external clock for the network card chip, and the Ethernet peripheral circuit is connected with the network card chip.
The clock circuit is used for providing a time sequence signal for the network card chip. The clock circuit can adopt a crystal oscillator circuit or other clock chips. In the present embodiment, a clock circuit is taken as an external crystal oscillator circuit as an example. As shown in fig. 3, a 25MHz crystal oscillator is used as the external clock.
The network card chip may be specifically selected according to the actual situation, for example, the DM9000A network card chip may be selected. The network card chip has the self-adaptive rate of 10M/1000M, the width of a data bus is set to be 8 bits, an interrupt processing mode is not adopted, and a polling mode is adopted. The circuit structure diagram of the ethernet peripheral device may adopt the connection structure shown in fig. 4. The portal communication unit 20 may be connected via RJ45 as an interface, using twisted pair wires or other network equipment.
In the network port communication unit 20, the ethernet peripheral circuit is used to transmit network port data, that is, the data is transmitted in a direct memory access manner, so that the data is directly accessed from the memory without passing through the CPU, and the CPU resource occupancy is reduced.
As an optional implementation manner of this embodiment, as shown in fig. 5, the control unit 10 may be implemented by a minimum system of a single chip, and the control unit includes a power circuit and a controller. As shown in fig. 5, the minimum system of the single chip has a working frequency of 72MHz, and contains 512K Flash and 5 UART interfaces.
The power circuit is used for providing working power for the controller. Fig. 6 shows a specific schematic block diagram of a power supply circuit for converting an external 5V voltage to a 3.3V voltage. In conjunction with the control unit shown in fig. 5, the operating voltage of the controller is 2V to 3.6V, and the external power supply voltage can be converted into the operating voltage of the controller by using the power supply circuit. The voltage regulator chip U2 in fig. 6 is used for voltage reduction, and the capacitors C1, C17, C2 and C18 are used for filtering and filtering the interference of the input voltage fluctuation to the controller.
The control unit in fig. 5 also comprises a reset circuit. Optionally, for convenience of debugging, the reset circuit used in this embodiment is, as shown in fig. 7, a combination of power-on automatic reset and manual reset.
The embodiment of the invention also provides a data transmission method which can be applied to the control unit of the data transmission device in the embodiment. Specifically, as shown in fig. 8, the data transmission method includes:
s11, obtaining the first serial port data from at least one serial port of the serial port communication unit, and/or obtaining the second network port data from at least one network port of the network port communication unit.
With reference to the data transmission device in the foregoing embodiment, the control unit obtains the first serial port data from at least one serial port of the serial port communication unit, and/or obtains the second internet access data from at least one internet access of the internet access communication unit.
S12, converting the first serial port data into first network port data and outputting the first network port data from at least one network port, and/or converting the second network port data into second serial port data and outputting the second serial port data from at least one serial port.
After receiving the first serial port data, the control unit analyzes the first serial port data, packages the first serial port data into first internet port data and outputs the first internet port data from at least one internet port; and/or after receiving the second network port data, the control unit analyzes the second network port data, packages the second network port data into second serial port data and outputs the second serial port data from at least one serial port.
The data transmission method provided by the embodiment can be applied to a data transmission device, and the serial port data is converted into the network port data and the network port data is converted into the serial port data, so that the transmission problem of the serial port data and the network port data can be realized through the device.
As an optional implementation manner of this embodiment, the step S11 includes the following steps:
(1) and receiving the first serial port data from at least one serial port in an interrupt mode.
For example, referring to fig. 9, as shown in fig. 9, the control unit receives the first serial port data in an interrupt manner.
(2) And analyzing the first serial port data according to a first preset protocol to obtain first internet port data.
After receiving the first serial port data, analyzing the first serial port data according to a first preset protocol, and sending the analyzed first serial port data to a scheduling and management module in the control unit. The first preset protocol is a communication protocol corresponding to the serial port, for example, when the serial port is RS232, the RS232 communication protocol is used to analyze the first serial port data.
(3) And outputting the first internet access data through at least one internet access.
The scheduling and management module processes the network data and sends the network data to the network port for sending.
Optionally, the control unit outputs the first portal data through at least one portal by using the upi protocol stack. Based on the running of the uIP protocol stack, the operating system can be separated from running on a bare board.
In other optional embodiments of this embodiment, the step S12 includes the following steps:
(1) and receiving second internet access data from at least one internet access by adopting an inquiry mode.
And the control unit receives the second internet access data from at least one internet access of the internet access communication unit in an inquiry mode, and optionally, receives the second internet access data from at least one internet access in an inquiry mode by using the uIP protocol stack.
(2) And analyzing the second network port data according to a second preset protocol to obtain second serial port data.
After receiving the second port data, the control unit analyzes the second port data according to a second preset protocol, and performs serial port data processing on the analyzed data by using the scheduling and management module shown in fig. 9.
(3) And outputting the second serial port data through at least one serial port.
And after the scheduling and management module processes the second network port data into second serial port data, the second serial port data is output through at least one serial port of the serial port communication unit.
Optionally, the control unit outputs the second serial port data through at least one serial port in a direct memory access manner. The second serial port data is output by adopting a direct memory access method, so that the data is directly accessed from the memory without being transmitted through the CPU, and the resource occupancy rate of the CPU is reduced.
Hereinafter, a data transmission method is described from the software implementation perspective. The software running in the control unit of the data transmission device is divided into three layers, namely a drive layer, a protocol layer and an application layer. The drive layer comprises transplantation of network uIP, interruption of five serial ports and DMA communication realization. The protocol layer contains the parsing, translation and packaging of data. The application layer mainly handles information and manages scheduling.
And the software transplantation uIP protocol is used for carrying out network communication. As shown in FIG. 9, FIG. 9 illustrates the overall logical framework of the software. Wherein, the application function portal: the data flow or program operation start is divided into main entry and interrupt entry, namely the so-called embedded software foreground and background architecture. The dispatching and management are periodically operated in the main, the network data are received, the serial port and the network data are sent, the data which are mainly processed and read by the serial port are interrupted, the receiving and sending conversion of the serial port data and the network data is comprehensively realized, namely, the controller reads the network data and then converts the data into the serial port data to be sent, and the data read from the serial port is sent through the network.
Specifically, the initialization includes startup, clock initialization, Flash storage initialization, DMA initialization, interrupt initialization, serial port initialization, network initialization, and the like. This part of the function is automatically completed by the program.
Configuration parameters can be filled in by a user through the operation of an upper computer network assistant interface, and include but are not limited to serial port numbers, baud rates, data bit lengths, stop bits, parity bits and data lengths of serial ports; network address, port number, etc. The filling mode and content are detailed in the protocol design part below, and the configuration can be completed.
(1) Network to serial port
Example (c):
com 1: the data represents data sent to com 1;
com 123456789: the data is sent to a com1 port without changing;
com 223456789: the data is sent to a com2 port without changing;
if the packet does not carry a header (com2, etc.), it is automatically sent from the com 1.
(2) Serial to network
And the data received by the serial port is directly sent to the network port and transmitted through. (it is proposed to have a header for data processing).
(3) Configuring serial port parameters
(3.1) opening configuration
Wherein, the upper computer issuing protocol is shown in table 1; the lower computer returns a protocol, and does not.
TABLE 1 upper computer issuing protocol
|
Frame header
|
Type (B)
|
Char
|
Length of
|
15 bytes
|
Content providing method and apparatus
|
usartConfigOpen |
Example (c): usartConfigOpen
setUsart,com1,115200,8,1,None,end;
setUsart,com2,,9600,8,0,0,end;
A stop position 012;
and a check bit 012.
(3.2) configuration parameters
Wherein, the upper computer issuing protocol is shown in table 2; the lower computer returns a protocol, and does not.
TABLE 2 upper computer issuing protocol
|
Frame header
|
Separator symbol
|
Data of
|
Type (B)
|
Char
|
Char
|
Char
|
Length of
|
4 bytes
|
1 byte
|
Become longer
|
Content providing method and apparatus
|
comX
|
,
|
Baud rate |
Example (c): com1, 115200, 8, 1, None, end.
(4) Network parameter configuration
(4.1) opening configuration
The upper computer issuing protocol is shown in table 3.
TABLE 3 upper computer issuing protocol
|
Frame header
|
Type (B)
|
Char
|
Length of
|
13 bytes
|
Content providing method and apparatus
|
netConfigOpen |
Example (c): netConfigOpen.
(4.2) configuration parameters
Wherein, the upper computer issuing protocol is shown in table 4; the lower computer returns a protocol, and does not.
TABLE 4 upper computer issuing protocol
|
Frame header
|
Separator symbol
|
Data of
|
Type (B)
|
Char
|
Char
|
Char
|
Length of
|
3 bytes
|
1 byte
|
Become longer
|
Content providing method and apparatus
|
net
|
,
|
Configuration information |
Example (c): net, 192.168.1.10, 1000; or: net, 192.168.1.100, 8080.
After configuration is complete, data communication can be achieved. Wherein, the network transmission is based on the open source small-sized uIP protocol stack, and the Ethernet data is processed by TCP at the network layer and the protocol layer by being transplanted to stm 32. In the physical layer and the data link layer, the ethernet controller employs DM9000A, which is driven by its 10M/100M adaptive PHY and MAC functions to design procedures including initialization, packet transmission, packet reception, and the like.
As shown in fig. 10, fig. 10 shows a flow of the portal data processing in the control loop. The process mainly calls uip _ process () function to process the ethernet event, so as to realize the analysis and processing of the ethernet packet, the program processes through polling main control loop, creates two timers as the clocks of TCP and ARP respectively, then reads the ethernet packet read from DM9000A, and calls uip _ process () function to analyze the received data. The interface function between the protocol stack and the application layer is UIP _ APPCALL (), which is a callback function and passes different states and parameters to the application layer. Specifically, the internet access data processing comprises the following steps:
(101) judging whether the first entry is made; when entering for the first time, executing (102); otherwise, executing (104);
(102) creating a timer of 0.5S;
(103) creating a timer of 10S;
(104) receiving a network data packet from the network communication unit;
(105) judging whether the length of the received network data packet is greater than 0; when the length is greater than 0, executing (106); otherwise, executing (112);
(106) judging whether the received network data packet is an IP packet or not; when the network data packet is an IP packet, executing (107); otherwise, executing (108);
(107) performing IP unpacking processing and executing (110);
(108) judging whether the received network data packet is an ARP packet; when the network data packet is an ARP packet, executing (109);
(109) performing ARP unpacking processing and executing (110);
(110) judging whether a packet needs to be sent; when the package sending is needed, executing (111); otherwise, executing (113);
(111) packaging the uIP protocol stack;
(112) the network card is sent out, and the execution is returned (101);
(113) judging whether the 0.5S timer is overtime or not; when the 0.5S timer times out, executing (114); otherwise, executing (101);
(114) resetting the 0.5S timer;
(115) polling each TCP connection;
(116) judging whether a packet needs to be sent; when the package sending is needed, executing (117); otherwise, executing (119);
(117) packaging by adopting a uIP protocol stack;
(118) adopting a network card to send out a packet;
(119) judging whether the timer is overtime or not; when the 10S timer times out, executing (120); otherwise, executing (101);
(120) resetting the 10S timer;
(121) and updating the ARP cache table and returning to the execution (101).
Alternatively, the processing flow shown in fig. 11 may be employed for the reception of portal data. Specifically, the method comprises the following steps:
(201) clearing the length of the received data;
(202) reading memory data;
(203) calculating the position of the memory data;
(204) reading the data state;
(205) judging whether data is received or not; when data is received, execution 206; otherwise, performing (215);
(206) detecting the width of a data bus;
(207) judging whether the width of the data bus is 8 bits or not; when the data bus width is 8 bits, execute (208); otherwise, executing (216);
(208) reading the state of the data packet;
(209) reading the length of the data packet;
(210) calculating the storage position of the next data packet;
(211) reading data packet data;
(212) the packet length is reported to a TCP/IP upper layer;
(213) accumulating the packet receiving times;
(214) returning the length of the data packet, and ending;
(215) DM9000A software reset;
(216) and returning to 0 and ending.
The serial port transmission adopts the design of interrupt reception and DMA transmission, and a total of five serial ports are available for use. The interrupt is distributed to the serial ports 1-5, and the priority level is gradually reduced. After triggering the interrupt, the frame header is judged first, and then the data is received to the buffer area. And sending and matching DMA, and reducing the occupancy rate of the CPU.
The control unit is responsible for scheduling the three parts and realizing the mutual transmission and conversion of the network data and the serial port data.
As a typical application scenario of the data transmission method of the embodiment, when an upper computer, i.e. a common PC terminal, is used for communicating with an embedded serial device, the use steps are as follows:
and step 1, connecting and operating. The upper computer is connected with the data transmission device through a network cable, and the data transmission device is connected with the embedded equipment through a serial port; then the module program is automatically operated after being electrified.
And step 2, the upper computer refers to a protocol and fills communication configuration parameters of the serial port and the network through network assistant software.
And 3, sending and receiving data according to the communication protocol.
And 4, checking the sent and returned data on the interface of the upper computer.
In order to meet the requirements of resources and efficiency of the embedded system, the data transmission method provided by the embodiment adopts a design principle of lightweight, general and easy use, especially considers that the data transmission method is used in different systems and software environments in the future, has clear external interfaces and clear internal modules, and is convenient to transplant to different embedded environments.
Although the embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the invention, and such modifications and variations fall within the scope defined by the appended claims.