CN106326157A - Method and device for receiving variable-length data of serial port - Google Patents

Method and device for receiving variable-length data of serial port Download PDF

Info

Publication number
CN106326157A
CN106326157A CN201510373142.7A CN201510373142A CN106326157A CN 106326157 A CN106326157 A CN 106326157A CN 201510373142 A CN201510373142 A CN 201510373142A CN 106326157 A CN106326157 A CN 106326157A
Authority
CN
China
Prior art keywords
data
initial value
dma
serial ports
count value
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.)
Pending
Application number
CN201510373142.7A
Other languages
Chinese (zh)
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.)
Shenzhen Clou Electronics Co Ltd
Original Assignee
Shenzhen Clou 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 Shenzhen Clou Electronics Co Ltd filed Critical Shenzhen Clou Electronics Co Ltd
Priority to CN201510373142.7A priority Critical patent/CN106326157A/en
Publication of CN106326157A publication Critical patent/CN106326157A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F13/00Interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
    • G06F13/14Handling requests for interconnection or transfer
    • G06F13/20Handling requests for interconnection or transfer for access to input/output bus
    • G06F13/28Handling requests for interconnection or transfer for access to input/output bus using burst mode transfer, e.g. direct memory access DMA, cycle steal
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F13/00Interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
    • G06F13/38Information transfer, e.g. on bus
    • G06F13/42Bus transfer protocol, e.g. handshake; Synchronisation
    • G06F13/4282Bus transfer protocol, e.g. handshake; Synchronisation on a serial bus, e.g. I2C bus, SPI bus
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2213/00Indexing scheme relating to interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
    • G06F2213/0002Serial port, e.g. RS232C
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2213/00Indexing scheme relating to interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
    • G06F2213/28DMA
    • G06F2213/2806Space or buffer allocation for DMA transfers

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Communication Control (AREA)

Abstract

The invention relates to a method and a device for receiving the variable-length data of a serial port. The method comprises the following steps of: setting an initial value, wherein the initial value is DMA (Direct Memory Access) pre-receiving data length; inquiring a count value for one time at an interval of the same preset time period, wherein the count value is the data length obtained in a way that the initial value is taken as a starting value and the initial value reduces one byte one by one when every piece of byte data is received; comparing the count values inquired for successive two times; and if the count values inquired for successive two times are equal and are different from the initial value, determining that data finishes being received. By use of the method, a DMA way is adopted for receiving, the data of any length can be received, CPU (Central Processing Unit) resources are greatly saved, and communication response speed and communication reliability are improved.

Description

A kind of method and device receiving serial ports random length data
Technical field
The present invention relates to communication technical field, particularly relate to a kind of for direct memory access (DMA, Direct Memory Access) data receiver method.
Background technology
In serial communication, when required communication rate is higher, carry out data receiver, meeting according to interrupt mode Cause system to frequently enter serial ports transmitting-receiving to interrupt, perform serial ports interrupt service routine, take the most in a large number CPU processes the time, and easily causes the loss of data, and DMA (Direct Memory Access), It is a kind of direct from the data exchange mode of memory access data without CPU.In dma mode, CPU directly controls dma controller by instruction, allows dma controller to process the transmission of data, counts Again information is fed back to CPU according to transmission is complete, the most largely alleviates cpu resource occupation rate, System resource can be greatly saved, therefore, during carrying out data receiver, compared with interrupt mode, tool Have the advantage that first, when using serial ports interrupt mode, often receive 1 byte data and will produce once Serial ports receives and interrupts, and performs once to receive interrupt service routine, and under dma mode, it is desirable to the number of transmission All transmit end just generation once to interrupt according to block, decrease CPU and carry out the number of times of interrupt processing;Secondly, The data transmission using serial ports interrupt mode has been controlled by CPU when interrupt processing, and DMA It is to have been controlled by dma controller, this eliminates CPU and too much have little time because of LPT device to process And cause the phenomenons such as loss of data because speed is not mated.But, use dma mode to carry out data and connect In the time receiving, also there will be some problems, during DMA data transfer operation, indefinite transfer, i.e. sender will send Data case sender and the regular length appointed of recipient split, it is invalid that not enough regular length is filled with Data, to regular length, when using dma mode to carry out data receiver, need first to dma controller system Surely need to receive byte number, and when reality is applied, the byte number of communications reception data at random may be used often Become, it is impossible to determine, therefore cannot directly use DMA to carry out data receiver, how to utilize DMA side Formula receives random length data becomes problem demanding prompt solution.
Summary of the invention
The purpose of the present invention is contemplated to solve DMA in prior art cannot receive random length data Technical problem, the present invention is provided a kind of conveying length being inquired about DMA by timing, and then judges that data connect Receive and whether complete.The concrete technical scheme of the present invention is as follows:
A kind of method receiving serial ports random length data, described method comprises the steps:
Arranging initial value, described initial value is DMA pre-receiving data length;
At interval of an identical count value of preset time period inquiry;Described count value is: with described initial value be Initial value, often receives a byte data, and described initial value successively decreases the data length after a byte one by one;
The relatively size of the count value of double inquiry;
If the count value of double inquiry is equal, and with initial value, it is determined that data receiver completes.
Further, described initial value is set after, described method also includes:
The reception mark that trigger source is corresponding serial ports of DMA is set;
After receiving a data word, the reception mark set of described serial ports, triggers DMA transfer.
Further, after described judgement data receiver completes, described method includes:
Calculating the data length that DMA receives, the data length that described DMA receives is described initial value and end The difference of value, the count value of this inquiry is identical with the count value that last time inquires about, then the count value of this inquiry It is worth for end.
Further, described initial value is set before, described method also includes:
The reception cache register that source address is corresponding serial ports of DMA is set;
The destination address arranging DMA is data receiver buffer area first address.
Further, after described triggering DMA transfer, described method also includes:
The source address of described DMA keeps constant, and the destination address of described DMA adds 1 automatically;
The data received receive cache register from described serial ports and transfer to described data receiver buffer area.
Further, after described triggering DMA transfer, described method also includes:
Closedown serial ports interrupts, and opens DMA and interrupts.
Further, described method includes:
Described initial value is identical with the size of data receiver buffer area.
Further, described method also includes:
Described preset time period is more than the time received needed for 1 byte data.
A kind of device receiving serial ports random length data, including:
First module, is used for arranging initial value, and described initial value is DMA pre-receiving data length;
Counting unit, for recording gauge numerical value, described count value is for often to receive a byte data, with institute Stating initial value is initial value, the data length after 1 byte of successively decreasing one by one;
Query unit, at interval of an identical count value of preset time period inquiry;
Comparing unit, for the size of the relatively count value of double inquiry;
Determine unit, be used for determining whether data receiver completes, if the count value of double inquiry is equal, And with initial value, it is determined that data receiver completes.
Compared to prior art, the present invention provides the most useful of a kind of method receiving serial ports random length data Effect is: of the present invention for serial communication, when serial communication baud rate is higher, uses DMA Mode receives data, sets the initial value of DMA pre-receiving data length, often receives the data of a byte, Described initial value is that initial value successively decreases 1 one by one, at interval of the count value of preset time period inquiry DMA, if The count value of double inquiry is identical, and different from described initial value, determines that described DMA data receives Complete, invention achieves and can receive random length data by DMA, be greatly saved cpu resource, Improve communication corresponding speed and communication reliability.
Accompanying drawing explanation
Fig. 1 is the method step flow chart that the embodiment of the present invention 1 one kinds receives serial ports random length data.
Fig. 2 is the method step flow chart that the embodiment of the present invention 2 one kinds receives serial ports random length data.
Fig. 3 is the device frame structure schematic diagram that the embodiment of the present invention 3 one kinds receives serial ports random length data.
Detailed description of the invention
In order to make the purpose of the present invention, technical scheme and advantage clearer, below in conjunction with accompanying drawing and reality Execute example, the present invention is further elaborated.Only should be appreciated that specific embodiment described herein In order to explain the present invention, it is not used to limit the present invention.
Embodiment 1
Refer to shown in Fig. 1.
The present invention provides a kind of method receiving serial ports random length data, and described method comprises the steps:
Sa: arrange initial value, described initial value is DMA pre-receiving data length;DMA pre-receiving number is set Identical with data receiver buffer size according to length, it should be noted that for ensureing greatest length Frame Normally receive, receive data buffer area size and have to be larger than the length value equal to greatest length Frame, by institute State initial value and be set to identical with the size of data receiver buffer area, can reach to reduce loss of data as far as possible Advantageous Effects.
Sb: at interval of an identical count value of preset time period inquiry;Described count value is: at the beginning of described Value for initial value, often receives a byte data, and described initial value data after a byte of successively decreasing one by one are long Degree.
Described preset time period is more than the time received needed for 1 byte data.
It is understood that described initial value is m, clocking value is k, and the time is t;
t0In the moment, the initial value of DMA is m;
t1Moment, k1=m-1;
t2Moment, k2=k1-1;
t3Moment, k3=k2-1;Wherein, t0, t1, t2…tnBetween interlude section identical.
The like, if tnMoment, kn=kn-1-1。
Sc: the relatively size of the count value of double inquiry;Relatively kn-1And knSize.
Sd: determine whether data receiver completes.
Sd1: if the count value of double inquiry is equal, and with initial value, it is determined that data receiver Complete.
Sd2: if the count value of double inquiry is unequal, then return step Sc.
The count value of this inquiry is kn, the count value of last inquiry is kn-1If, kn=kn-1, and kn With kn-1All unequal with initial value m, it is determined that data receiver completes.
Embodiment 2
Refer to shown in Fig. 2.
The present invention provides a kind of present invention to provide a kind of method receiving serial ports random length data, described method bag Include following steps:
Sa: the reception cache register that the source address of DMA is corresponding serial ports is set, such as, UCAxRXBUF, the destination address arranging DMA is data receiver buffer area first address.
Sb: arrange initial value, described initial value is DMA pre-receiving data length;DMA pre-receiving number is set Identical with data receiver buffer size according to length, it should be noted that for ensureing greatest length Frame Normally receive, receive buffer size and have to be larger than the length value equal to greatest length Frame, initial value is set It is set to identical with the size of data receiver buffer area, can reach to reduce as far as possible the Advantageous of loss of data Effect.
Sc: the reception mark that trigger source is corresponding serial ports of DMA, such as, UCAxRXIFG are set, Triggering mode selects rising edge to trigger.
After receiving a data word, the reception mark set of described serial ports, triggers DMA transfer, and connects The data received receive cache register from described serial ports and transfer to described data receiver buffer area.
In data transmission procedure, the source address of described DMA keeps constant, the destination address of described DMA Automatically add 1;
Sd: close serial ports and interrupt, opens DMA and interrupts.If receive longer abnormal data frame, enter Enter DMA to interrupt, for abnormal fault-tolerant processing, entering DMA interrupt processing.
Se: at interval of an identical count value of preset time period inquiry;Described count value is: at the beginning of described Value for initial value, often receives a byte data, and described initial value data after a byte of successively decreasing one by one are long Degree.
Described preset time period is more than the time received needed for 1 byte data.
It is understood that described initial value is m, clocking value is k, and the time is t;
t0In the moment, the initial value of DMA is m;
t1Moment, k1=m-1;
t2Moment, k2=k1-1;
t3Moment, k3=k2-1;
The like, if tnMoment, kn=kn-1-1。
Sf: the relatively size of the count value of double inquiry;Relatively kn-1And knSize.
Sg: determine whether data receiver completes.
Sg1: if the count value of double inquiry is equal, and with initial value, it is determined that data receiver is complete Become.The count value of this inquiry is kn, the count value of last inquiry is kn-1If, kn=kn-1, and kn With kn-1All unequal with initial value m, it is determined that data receiver completes.
Sg2: if the count value of double inquiry is unequal, then return step Sf.
It should be noted that in prior art, it is identical that DMA receives data length, DMA interrupts only existing Just can produce DMA when the transmission number of DMA is decremented to 0 to interrupt, corresponding reception length is data Receive buffer size.Data length owing to receiving every time is indefinite, so DMA can not be utilized to interrupt coming Judgement finishes receiving.In the application, by the size of timing query counts value, if the meter of double inquiry Numerical value is identical, is determined as finishing receiving, and has reached to be received the Advantageous of random length data by DMA Effect.
Sh: data process, and calculate the data length that DMA receives, the data length that described DMA receives For the difference of described initial value with end value, the count value of this inquiry is identical with the count value that last time inquires about, then originally The count value of secondary inquiry is end value.
Data length=m-k that DMA receivesn
Reset destination address and the transmission data length of DMA, again make DMA carry out next frame number According to reception.
Embodiment 3
Refer to shown in Fig. 3.
On the basis of embodiment 2, the present invention provides a kind of device receiving serial ports random length data, including:
First module, is used for arranging initial value, and described initial value is DMA pre-receiving data length.
Second unit, for arranging source address and the destination address of DMA.
Unit the 3rd, for arranging the trigger source of DMA.
Receive unit, be used for receiving data, often receive a byte data, by described data from source address Transfer to destination address.
Counting unit, for recording gauge numerical value, described reception unit often receives a byte data, described counting Value is the data length with described initial value as initial value, after 1 byte of successively decreasing one by one.
Clock unit, is used for providing temporal information.
Query unit, for the temporal information provided according to clock unit, at interval of identical preset time period Inquire about a count value.
Comparing unit, for the size of the relatively count value of double inquiry.
Determine unit, be used for determining whether data receiver completes, if the count value of double inquiry is equal, And with initial value, it is determined that data receiver completes.
It should be noted that above example only in order to technical scheme to be described and unrestricted, although ginseng According to preferred embodiment, the present invention is described in detail, it will be understood by those within the art that, can Technical scheme is modified or equivalent, without deviating from the essence of technical solution of the present invention God and scope, it all should be contained in the middle of scope of the presently claimed invention.

Claims (9)

1. the method receiving serial ports random length data, it is characterised in that described method comprises the steps:
Arranging initial value, described initial value is DMA pre-receiving data length;
At interval of an identical count value of preset time period inquiry;Described count value is: with described initial value be Initial value, often receives a byte data, and described initial value successively decreases the data length after a byte one by one;
The relatively size of the count value of double inquiry;
If the count value of double inquiry is equal, and with initial value, it is determined that data receiver completes.
2. the method receiving serial ports random length data, it is characterised in that described initial value is set after, institute Method of stating also includes:
The reception mark that trigger source is corresponding serial ports of DMA is set;
After receiving a data word, the reception mark set of described serial ports, triggers DMA transfer.
A kind of method receiving serial ports random length data the most according to claim 1, it is characterised in that After described judgement data receiver completes, described method includes:
Calculating the data length that DMA receives, the data length that described DMA receives is described initial value and end The difference of value, the count value of this inquiry is identical with the count value that last time inquires about, then the count value of this inquiry It is worth for end.
A kind of method receiving serial ports random length data the most according to claim 1, it is characterised in that Described initial value is set before, described method also includes:
The reception cache register that source address is corresponding serial ports of DMA is set;
The destination address arranging DMA is data receiver buffer area first address.
A kind of method receiving serial ports random length data the most according to claim 4, it is characterised in that After described triggering DMA transfer, described method also includes:
The source address of described DMA keeps constant, and the destination address of described DMA adds 1 automatically;
The data received receive cache register from described serial ports and transfer to described data receiver buffer area.
A kind of method receiving serial ports random length data the most according to claim 2, it is characterised in that After described triggering DMA transfer, described method also includes:
Closedown serial ports interrupts, and opens DMA and interrupts.
A kind of method receiving serial ports random length data the most according to claim 5, it is characterised in that Described method includes:
Described initial value is identical with the size of data receiver buffer area.
A kind of method receiving serial ports random length data the most according to claim 1, it is characterised in that Described method also includes:
Described preset time period is more than the time received needed for 1 byte data.
9. the device receiving serial ports random length data, it is characterised in that including:
First module, is used for arranging initial value, and described initial value is DMA pre-receiving data length;
Counting unit, for recording gauge numerical value, described count value is for often to receive a byte data, with institute Stating initial value is initial value, the data length after 1 byte of successively decreasing one by one;
Query unit, at interval of an identical count value of preset time period inquiry;
Comparing unit, for the size of the relatively count value of double inquiry;
Determine unit, be used for determining whether data receiver completes, if the count value of double inquiry is equal, And with initial value, it is determined that data receiver completes.
CN201510373142.7A 2015-06-30 2015-06-30 Method and device for receiving variable-length data of serial port Pending CN106326157A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201510373142.7A CN106326157A (en) 2015-06-30 2015-06-30 Method and device for receiving variable-length data of serial port

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201510373142.7A CN106326157A (en) 2015-06-30 2015-06-30 Method and device for receiving variable-length data of serial port

Publications (1)

Publication Number Publication Date
CN106326157A true CN106326157A (en) 2017-01-11

Family

ID=57723115

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201510373142.7A Pending CN106326157A (en) 2015-06-30 2015-06-30 Method and device for receiving variable-length data of serial port

Country Status (1)

Country Link
CN (1) CN106326157A (en)

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107423654A (en) * 2017-08-11 2017-12-01 四川长虹电器股份有限公司 A kind of method for receiving random length bar code
CN108304335A (en) * 2018-02-07 2018-07-20 南京南瑞继保电气有限公司 A method of the indefinite long message of serial ports is received by DMA
CN111026694A (en) * 2019-11-01 2020-04-17 珠海奔图电子有限公司 Data receiving method, data receiving apparatus, image forming system, and electronic apparatus
CN112256617A (en) * 2020-10-23 2021-01-22 福州富昌维控电子科技有限公司 Method and terminal for improving serial port communication efficiency of equipment
CN114741343A (en) * 2022-02-28 2022-07-12 惠州华阳通用电子有限公司 Controller port expansion device and method

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6728795B1 (en) * 2000-04-17 2004-04-27 Skyworks Solutions, Inc. DMA channel for high-speed asynchronous data transfer
CN101146090A (en) * 2007-10-16 2008-03-19 深圳国人通信有限公司 A method and device for transmitting and receiving data based on USART bus
CN101510182A (en) * 2009-04-03 2009-08-19 北京中星微电子有限公司 Low speed DMA interface chip system and internal memory access method
CN104052579A (en) * 2014-06-26 2014-09-17 湖南先步信息股份有限公司 Automatic MODBUS-RTU communication baud rate matching method

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6728795B1 (en) * 2000-04-17 2004-04-27 Skyworks Solutions, Inc. DMA channel for high-speed asynchronous data transfer
CN101146090A (en) * 2007-10-16 2008-03-19 深圳国人通信有限公司 A method and device for transmitting and receiving data based on USART bus
CN101510182A (en) * 2009-04-03 2009-08-19 北京中星微电子有限公司 Low speed DMA interface chip system and internal memory access method
CN104052579A (en) * 2014-06-26 2014-09-17 湖南先步信息股份有限公司 Automatic MODBUS-RTU communication baud rate matching method

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
472283272: "STM32实现USARTDMA接收未知长度的数据和发送", 《HTTPS://WENKU.BAIDU.COM/VIEW/E8837D10FAD6195F312BA684.HTML》 *
BOBBAT: "STM32F207运用串口空闲中断+DMA接收不定长数据", 《HTTPS://BLOG.CSDN.NET/BOBBAT/ARTICLE/DETAILS/40071583》 *

Cited By (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107423654A (en) * 2017-08-11 2017-12-01 四川长虹电器股份有限公司 A kind of method for receiving random length bar code
CN107423654B (en) * 2017-08-11 2020-06-09 四川长虹电器股份有限公司 Method for receiving bar code with indefinite length
CN108304335A (en) * 2018-02-07 2018-07-20 南京南瑞继保电气有限公司 A method of the indefinite long message of serial ports is received by DMA
CN111026694A (en) * 2019-11-01 2020-04-17 珠海奔图电子有限公司 Data receiving method, data receiving apparatus, image forming system, and electronic apparatus
CN111026694B (en) * 2019-11-01 2021-03-02 珠海奔图电子有限公司 Data receiving method, data receiving apparatus, image forming system, and electronic apparatus
CN112256617A (en) * 2020-10-23 2021-01-22 福州富昌维控电子科技有限公司 Method and terminal for improving serial port communication efficiency of equipment
CN112256617B (en) * 2020-10-23 2021-04-20 福州富昌维控电子科技有限公司 Method and terminal for improving serial port communication efficiency of equipment
CN114741343A (en) * 2022-02-28 2022-07-12 惠州华阳通用电子有限公司 Controller port expansion device and method

Similar Documents

Publication Publication Date Title
EP3255553B1 (en) Transmission control method and device for direct memory access
CN106326157A (en) Method and device for receiving variable-length data of serial port
US9634888B2 (en) Method and system for transmitting data in parallel via wireless link and wired link
US7826349B2 (en) Connection management mechanism
TWI283977B (en) Adaptive interrupts coalescing system with recognizing minimum delay packets
CN101556564B (en) Data receiving/sending method and device
US8214552B2 (en) Transmission apparatus, transmission method, communication apparatus, and program
US10135710B2 (en) Information processing apparatus
US10432496B2 (en) Apparatus and method for controlling message communications load
CN108304156A (en) Host device and data transmission rate control method
TW201631943A (en) Communication device, communication method and program
CN111149097B (en) Master chip, slave chip and DMA transmission system between chips
CN108628544A (en) Host device and data transmission rate control method
WO2016095323A1 (en) Data transmission method and apparatus based on serial server
CN106209209B (en) A kind of method and system of satellite Star Service and observing and controlling telemetry data transmission
JP2012089948A (en) Data transmission device and data transmission method
US20100030930A1 (en) Bandwidth conserving protocol for command-response bus system
US7426589B2 (en) Network interface card for reducing the number of interrupts and method of generating interrupts
US7724775B2 (en) Data transmission circuit and method for controlling the data transmission circuit
JP2004179983A (en) Network equipment control system
EP2726992B1 (en) Network control model driver
US9182941B2 (en) Flow control with buffer reclamation
EP2141606B1 (en) Apparatus and method for processing high speed data using hybrid DMA
US20230275716A1 (en) System and method for assisting data transmission over virtual channels
JP2006195607A (en) Method and circuit for determining bulk-out transfer end

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
WD01 Invention patent application deemed withdrawn after publication
WD01 Invention patent application deemed withdrawn after publication

Application publication date: 20170111