Serial communication device and method based on LabVIEW
Technical field
Serial communication device and method the present invention relates to be based on LabVIEW, belongs to serial port communication technology field.
Background technology
Serial ports is a kind of very general equipment communication agreement on computer, is conventional computer and external series equipment
Between data transmission channel, the composition that virtual instrument system is current virtual instrument is constituted by serial bus and PC computers
One of mode, with interface it is simple, it is easy to use the characteristics of.
In virtual instrument field, the LabVIEW of NI companies is more next as a programming language for aiming at test measuring and design
More noticeable, it overcomes, and the tradition high level language program code such as VC, VB, C#, Java is tediously long, inconvenient carry out functions expanding etc.
Shortcoming, with ease in use, intuitively graphic programming mode, its human-computer interaction interface is friendly, data visualization analysis, equipment
The advantages of control ability is strong so that have great advantage in terms of data acquisition and instrument controlling, be more and more used for serial ports
The communications field.
But, in the prior art, the serial communication based on LabVIEW programmings occurs that data receiver is not complete or is
The problems such as empty, parsing data frame program is lengthy and jumbled time-consuming more, this has a strong impact on the communication quality of data.
The content of the invention
There is loss of data and parsing number the invention aims to solve the existing serial communication based on LabVIEW programmings
Many problems are taken according to frame program is lengthy and jumbled, there is provided a kind of serial communication device and method based on LabVIEW.
Serial communication device based on LabVIEW of the present invention, the communicator is realized and bottom as host computer
The communication of machine, it includes:
Action for perceiving human-computer interaction interface control, produce event and as the producer will produce event with character
String form sends into the UI event generation modules of event message queue;
For receiving the character string in event message queue as consumer, and the corresponding event representated by character string is entered
Row framing produces data frame, is re-used as the UI event processing modules of the data frame feeding instruction queue that the producer will produce;
Data frame for receiving instruction queue as consumer, the data frame serial ports is sent to slave computer;Connect simultaneously
The serial data of slave computer transmission is received, and the data is activation of data receiver queue is sent into as the serial data that the producer will receive
With receiver module;
For receiving the data in data receiver queue as consumer and using analysis state machine to data receiving queue
In data parsed, and as the producer will parse obtain data feeding parsing frame queue data resolution module;
For receiving the data in parsing frame queue and the data disaply moudle for being shown as the producer.
The UI events generation module is specifically included:
Action for perceiving human-computer interaction interface control, produces event, and the event is identified by being worth change
Application affairs structure function;
For using the action of each control as an event branch, by each corresponding event command of event branch with word
The element that symbol string form is stored in event message queue is joined the team array function.
The UI event processing modules are specifically included:
Element dequeue function for obtaining event message queue;
For event branch is corresponding with conditional branching, and framing is carried out according to protocol frame requirement, after CRC16 is verified
As data frame to be sent, the element enqueue of instruction queue will be sent into after data frame to be sent and command string binding
Function.
The data is activation is specifically included with receiver module:
For carry out VISA serial ports parameter, baud rate parameter, data bit parameter, stop position parameter, check bit parameter and
The initial configuration of Row control parameter simultaneously disables respective attributes node in serial ports transceiving data to prevent the configuration change
Serial ports configuration module;
For obtaining the data frame of instruction queue and the command string of binding by element dequeue function, by command word
Symbol string judges branch for construction of condition, and data frame is write into serial ports by VISA sends to the serial ports sending module of slave computer;
Serial data for reading slave computer by VISA, and the slave computer data that will be read send into data receiver queue
Serial ports receiver module;
Serial ports is closed for closing function by VISA, to recover the string of during initial configuration disabled attribute node
Mouth closedown module.
The data resolution module is specifically included:
Element dequeue function for obtaining the data in data receiver queue;
For the analysis state machine parsed to the data in data receiving queue;
Sent into command string binding cluster after for the data that the parsing of analysis state machine is obtained to be converted into character string
Parse the function of frame queue;
The data disaply moudle is specifically included:
Element dequeue function for obtaining the data in parsing frame queue;
For carrying out releasing binding to the data in parsing frame queue, command character is series-connected to construction of condition function
Branch selector, after data character string is converted into byte arrays, the function for being shown.
A kind of serial port communication method based on LabVIEW, the communication means is used to realize that host computer is logical with slave computer
Letter, it includes:
Action for perceiving human-computer interaction interface control, produce event and as the producer will produce event with character
The UI events of string form feeding event message queue produce step;
For receiving the character string in event message queue as consumer, and the corresponding event representated by character string is entered
Row framing produces data frame, is re-used as the UI event processing steps of the data frame feeding instruction queue that the producer will produce;
Data frame for receiving instruction queue as consumer, the data frame serial ports is sent to slave computer;Connect simultaneously
The serial data of slave computer transmission is received, and the data is activation of data receiver queue is sent into as the serial data that the producer will receive
With receiving step;
For receiving the data in data receiver queue as consumer and using analysis state machine to data receiving queue
In data parsed, and as the producer will parse obtain data feeding parsing frame queue data analyzing step;
For receiving the data in parsing frame queue and the data displaying for being shown as the producer.
The UI events produce step to specifically include:
Action for perceiving human-computer interaction interface control by application affairs structure function, produces event, and to the thing
The step of part is identified by being worth change;
For joining the team array function using the action of each control as an event branch by element, by each event branch
The step of corresponding event command is stored in event message queue with character string forms.
The UI event processing steps are specifically included:
The step of for obtaining event message queue by element dequeue function;
For by element array function of joining the team that event branch is corresponding with conditional branching, and group is carried out according to protocol frame requirement
Frame, data frame to be sent is turned into after CRC16 is verified, and is sent into after data frame to be sent is bundled with command string and referred to
The step of making queue.
The data is activation is specifically included with receiving step:
Join for carrying out VISA serial ports parameter, baud rate parameter, data bit parameter, stop position by serial ports configuration module
Number, the initial configuration of check bit parameter and Row control parameter and disable respective attributes node in serial ports transceiving data in case
Only the step of configuration change;
For the data frame of instruction queue and the life of binding to be obtained by element dequeue function by serial ports sending module
Character string is made, command string is judged into branch for construction of condition, data frame is write into serial ports by VISA sends to bottom
The step of machine;
The serial data of slave computer is read for the VISA by serial ports receiver module, and the slave computer data of reading are sent
The step of entering data receiver queue;
Function is closed for the VISA by serial ports closedown module to close serial ports, to recover disabled during initial configuration
Attribute node the step of.
The data analyzing step is specifically included:
The step of for obtaining the data in data receiver queue by element dequeue function;
The step of for being parsed to the data in data receiving queue by analysis state machine;
Bundled with command string for being parsed analysis state machine after the data for obtaining are converted to character string by function
The step of cluster feeding parsing frame queue;
The data displaying is specifically included:
The step of for obtaining the data in parsing frame queue by element dequeue function;
Releasing binding is carried out for parsing the data in frame queue by function pair, command character is series-connected to condition knot
The branch selector of structure function, after data character string is converted into byte arrays, the step of display.
Advantages of the present invention:In device or method of the present invention, UI events generation module leads to UI event processing modules
Event message queue communication is crossed, UI event processing modules are communicated with data is activation with receiver module by instruction queue, data hair
Send with receiver module and data resolution module by data receiver queue communication, data resolution module passes through with data disaply moudle
Parsing frame queue communication, forms four layers of producer consumer framework and the execution of each modular concurrent, data resolution module or step
The data received to serial ports using the method for analysis state machine are parsed according to parsing Frame Protocol and obtain complete parsing data frame, are protected
The integrality of communication data transfer is demonstrate,proved, program simple and clear, execution speed are fast and stable.
The present invention realizes that serial ports is received, sent and parsing display using producer consumer framework and state machine, it is ensured that
Ensure that program is succinct while data are from losing, operation is time-consuming short.On the one hand it use four layers between five functional modules
Producer consumer framework, program orderliness is clear and takes full advantage of the advantage of LabVIEW executed in parallel, improves operation speed
Rate;On the other hand data are parsed using analysis state machine, solves the reception data occurred often in conventional program
It is incomplete or for empty and program is lengthy and jumbled time-consuming the problems such as, high degree optimizes serial communication program.
Brief description of the drawings
Fig. 1 is the Organization Chart of the serial communication based on LabVIEW of the present invention;
Fig. 2 is the stopping mode schematic diagram of the program of the serial communication based on LabVIEW of the present invention;
Fig. 3 is the schematic diagram of data is activation and receiver module.
Specific embodiment
Specific embodiment one:With reference to Fig. 1 and Fig. 2 explanation present embodiments, it is based on described in present embodiment
The serial communication device of LabVIEW, the communicator realizes the communication with slave computer as host computer, and it includes:
Action for perceiving human-computer interaction interface control, produce event and as the producer will produce event with character
String form sends into the UI events generation module 1 of event message queue;
For receiving the character string in event message queue as consumer, and the corresponding event representated by character string is entered
Row framing produces data frame, is re-used as the UI event processing modules 2 of the data frame feeding instruction queue that the producer will produce;
Data frame for receiving instruction queue as consumer, the data frame serial ports is sent to slave computer;Connect simultaneously
The serial data of slave computer transmission is received, and the data is activation of data receiver queue is sent into as the serial data that the producer will receive
With receiver module 3;
For receiving the data in data receiver queue as consumer and using analysis state machine to data receiving queue
In data parsed, and as the producer will parse obtain data feeding parsing frame queue data resolution module 4;
For receiving the data in parsing frame queue and the data disaply moudle 5 for being shown as the producer.
Specific embodiment two:With reference to Fig. 1 and Fig. 2 explanation present embodiments, present embodiment is to implementation method one
It is described further, the UI events generation module 1 is specifically included:
Action for perceiving human-computer interaction interface control, produces event, and the event is identified by being worth change
Application affairs structure function;
For using the action of each control as an event branch, by each corresponding event command of event branch with word
The element that symbol string form is stored in event message queue is joined the team array function.
In present embodiment, the application affairs structure function of UI events generation module 1 on human-computer interaction interface panel to respectively controlling
The action of part is identified by being worth change event, and the action of each control is used as an event branch, and it is right that each branch produces
The event command answered simultaneously is stored in event message queue now with character string forms by the element that LabVIEW is carried array function of joining the team
UI events generation module 1 is used as the producer.
Specific embodiment three:With reference to Fig. 1 and Fig. 2 explanation present embodiments, present embodiment is to implementation method one
Or two be described further, the UI event processing modules 2 are specifically included:
Element dequeue function for obtaining event message queue;
For event branch is corresponding with conditional branching, and framing is carried out according to protocol frame requirement, after CRC16 is verified
As data frame to be sent, the element enqueue of instruction queue will be sent into after data frame to be sent and command string binding
Function.
In present embodiment, UI event processing modules 2 obtain event message by the element dequeue function in LabVIEW
The data of queue, now UI event processing modules 2 are used as consumer, and application conditions structure function is only to realize each event correspondence
A conditional branching is found, framing is carried out according to protocol frame requirement according to event, the data frame for obtaining turns into after CRC16 is verified
Data frame to be sent, using showing that control is shown frame to be sent with hexadecimal string in man-machine interface panel
Show, while being joined the team after array function bundlees data frame with command string by the element that LabVIEW is carried sends into instruction queue,
Now UI event processing modules 2 are used as the producer.
Specific embodiment four:Present embodiment is illustrated with reference to Fig. 1 to Fig. 3, present embodiment is to implementation method three
It is described further, the data is activation is specifically included with receiver module 3:
For carry out VISA serial ports parameter, baud rate parameter, data bit parameter, stop position parameter, check bit parameter and
The initial configuration of Row control parameter simultaneously disables respective attributes node in serial ports transceiving data to prevent the configuration change
Serial ports configuration module;
For obtaining the data frame of instruction queue and the command string of binding by element dequeue function, by command word
Symbol string judges branch for construction of condition, and data frame is write into serial ports by VISA sends to the serial ports sending module of slave computer;
Serial data for reading slave computer by VISA, and the slave computer data that will be read send into data receiver queue
Serial ports receiver module;
Serial ports is closed for closing function by VISA, to recover the string of during initial configuration disabled attribute node
Mouth closedown module.
Present embodiment includes serial ports configuration module, serial ports sending module, serial ports receiver module and serial ports closedown module,
Serial ports configuration module carries out VISA serial ports, baud rate, data bit, stop position, check bit sum Row control these parameters
Initial configuration and these configurations when serial ports is received and dispatched are forbidden to be changed by disabling the method for respective attributes node;Serial ports is sent out
Module is sent to pass through the data that the element dequeue function in LabVIEW obtains instruction queue, now data is activation and receiver module 3
After as consumer, data solution is bundled, command string judges branch for construction of condition, and data character string is used to be sent to
Slave computer, the serial ports function VISA that is carried using LabVIEW carries out transceiving data, is write by VISA and sent the data to down
Position machine;Serial ports receiver module is read in real time to read back slave computer data by VISA and is shown to man-machine interface panel and sends into data
Receiving queue, now data is activation and receiver module 3 are used as the producer;Serial port module is closed to close function by VISA to close
Serial ports, while recovering each parameter attribute node disabled during initialization.
Specific embodiment five:Present embodiment is illustrated with reference to Fig. 1 to Fig. 3, present embodiment is to implementation method four
It is described further, the data resolution module 4 is specifically included:
Element dequeue function for obtaining the data in data receiver queue;
For the analysis state machine parsed to the data in data receiving queue;
Sent into command string binding cluster after for the data that the parsing of analysis state machine is obtained to be converted into character string
Parse the function of frame queue;
The data disaply moudle 5 is specifically included:
Element dequeue function for obtaining the data in parsing frame queue;
For carrying out releasing binding to the data in parsing frame queue, command character is series-connected to construction of condition function
Branch selector, after data character string is converted into byte arrays, the function for being shown.
The branch selector is on construction of condition function.
In present embodiment, shown in Fig. 3, data resolution module 4 obtains string by the element dequeue function in LabVIEW
The data that mouth reads back, are parsed by protocol analysis state machine, and now data resolution module 4 is used as consumer, data parsing
The outermost layer of module 4 is that while is circulated to maintain the operation of state machine, and secondary outer layer is a construction of condition, for judging whether to surpass
When, next layer is that for is circulated so that the data in obtaining the data receiver queue that data resolution module 4 is received are in the form of byte
Parsed into circulation one by one, be construction of condition in for circulation internal layers, sentenced for the state different to each
Disconnected, data have been processed under each state can indicate which state is next step enter, here it is the function of shift register, will
Next state is delivered in recurrent state judgement next time;Data resolution module 4 obtains the data of data receiver queue, passes through
Character string to byte arrays transfer function converts data to byte arrays, and then the feeding for circulations of byte one by one, this
When data resolution module as consumer;The data that serial ports reads back parse frame by being obtained after state machine, and parsing frame number group turns
Frame queue is parsed with command string binding cluster feeding after being changed to character string, now data resolution module 4 is used as the producer.
The outer layer of data disaply moudle 5 is that while circulates to ensure the operation of display module, and internal layer is construction of condition, data
Display module 5 obtains the data in parsing frame queue by the element dequeue function in LabVIEW, is carried out after dequeuing data row
Binding is released, command character therein is series-connected to the branch selector of construction of condition, and data character string is converted into byte arrays
It is used to show afterwards, now data disaply moudle 5 is used as consumer.
In applying the invention to Practical Project, the communication of slave computer and host computer is realized, and then by before in host computer
The operation of panel carrys out controlled motor operation:
Using the serial communication framework shown in Fig. 1, form four layers of producer consumer framework and each modular concurrent is performed, UI
Event generation module 1 perceives the action of front panel control, produces event and event is sent into character string forms as the producer
Event message queue, UI event processing modules 2 receive character string and relatively each thing in event message queue as consumer
Part carries out framing, and this is ground floor producer consumer framework, and UI event processing modules 2 are re-used as the data that the producer will produce
Frame sends into instruction queue, and data is activation receives the data in instruction queue and carries out serial ports transmission with receiver module 3 as consumer
And serial data is received, this is second layer producer consumer framework, and data is activation is re-used as the producer with receiver module 3 will be connect
Receive data feeding data receiver queue, data resolution module 4 as consumer receive data receiver queue in data simultaneously
Parsed, this is third layer producer consumer framework, data resolution module 4 is re-used as the producer and will parse the data for obtaining
In feeding parsing frame queue, data disaply moudle 5 receives the data in parsing frame queue and is shown as the producer, and this is
4th layer of producer consumer framework.
The outer layer of UI events generation module 1 is that while is circulated to maintain the operation of event generation module, is adopted in while circulations
Event structure function is used, the corresponding control of front panel is added to correspondence by right button selection addition event branch on time structure block diagram
Event branch, produce and event message is sent to by element array function of joining the team after corresponding event message and the binding of variant constant
In queue, now UI events generation module 1 is used as the producer.
As shown in Fig. 2 the serial communication framework of LabVIEW has multithreading, so the mode of stopping is using circulation expiry notification
The mode of device, i.e., use " obtain notifying device quote " function outside each circulation, UI event generation modules " ' stopping ' value changes
Becoming " branch uses " send notify " function to send the notice that stop button is pressed to give expiry notification device, and will by expiry notification device
This notice is delivered to the outermost layer while circulations of each thread, will notify to be assigned to while by " wait and notifying " function
" cycling condition " button of circulation, the operation of whole program is terminated with this.
The outermost layer of UI event processing modules 2 is that while is circulated to ensure the operation of this module, in reception event message queue
Data command character is series-connected to the branch selector of construction of condition after releasing binding, now UI event processing modules
2, as consumer, framing are carried out in corresponding conditional branching according to protocol frame standard.Using array function by frame to be sent
One byte arrays of composition simultaneously add CRC check, and present case is verified using CRC16, and core concept is to utilize " to call built-in function section
Point " generates the check bit of 16bit come the .dll files for calling the CRC16 for having generated to verify.The data obtained after the completion of framing
On the one hand frame number group cycles through " numerical value to hexadecimal string is changed " function and enters back into next for circulations into for,
The hexadecimal transmission frame character string for display is obtained by " connection string " function, by showing control in front panel
Display;On the other hand instruction queue is sent into command string binding after being converted into character string, now UI event processing modules 2 are made
It is the producer.
As shown in figure 3, serial ports sending module and serial ports receiver module are in a while circulation, while circulations ensure to receive
The continuity of hair.
The outermost layer of data resolution module 4 maintains the operation of analysis state machine with while circulations, outer right in while circulations
State and data used are initialized, and are successively respectively construction of condition from outside to inside in while circulations and are judged that element goes out
Whether queue is overtime, for circulations make the data for receiving be processed one by one with bytewise, construction of condition selection state
And the treatment of data and the selection to NextState under each state, state and data flow are followed flowing in and out while
Ring and for use shift register when circulating, this ensure that under indicating next step to enter after data have been processed under each state
One designated state.In outer layer while circulations, data resolution module read data words symbol string from data receiver queue turns
For circulations are sent into one by one after changing byte arrays into, and now used as consumer, byte is circulated data resolution module 4 into for
Enter first branches of states of acquiescence afterwards, judged according to parsing Frame Protocol, it is first determined whether be frame head, according to result
Enter which state below determination in innermost layer construction of condition and choose whether for this secondary data to be added to parsing frame number group,
In the condition adjudgement that this state passes through the incoming alternative condition structure of recurrent state next time of shift register, such state machine can
Freely changed between each state, finally give the parsing data frame array according to protocol frame requirement and array is converted into word
Parsing frame queue is sent into after symbol string and command string binding, now data resolution module 4 is used as the producer.
Data disaply moudle 5 is made up of the while circulations of outer layer and the construction of condition of internal layer, each serves as maintenance display mould
Block runs and selects the effect of display content.Data disaply moudle 5 is solved after obtaining the data in parsing frame queue to data
Bundling operation, command string feeding condition selector, data character string is converted into byte arrays latter aspect and is circulated into for
By " numerical value to hexadecimal string change " function enter back into next for cycle through " connection string " function come
To the hexadecimal parsing frame character string for display, and by showing that control shows in front panel, on the other hand according to condition
Branch's content to display and parsing Frame Protocol are operated to parsing array of data, mainly using character string function " interception
The LabVIEW such as character string ", dialog box function " single button dialog box " carry out requiring the whole of display content to different from tape function
Close, now data disaply moudle 5 is used as consumer.
Specific embodiment six:With reference to Fig. 1 and Fig. 2 explanation present embodiments, it is based on described in present embodiment
The serial port communication method of LabVIEW, the communication means is used to realize the communication of host computer and slave computer, and it includes:
Action for perceiving human-computer interaction interface control, produce event and as the producer will produce event with character
The UI events of string form feeding event message queue produce step;
For receiving the character string in event message queue as consumer, and the corresponding event representated by character string is entered
Row framing produces data frame, is re-used as the UI event processing steps of the data frame feeding instruction queue that the producer will produce;
Data frame for receiving instruction queue as consumer, the data frame serial ports is sent to slave computer;Connect simultaneously
The serial data of slave computer transmission is received, and the data is activation of data receiver queue is sent into as the serial data that the producer will receive
With receiving step;
For receiving the data in data receiver queue as consumer and using analysis state machine to data receiving queue
In data parsed, and as the producer will parse obtain data feeding parsing frame queue data analyzing step;
For receiving the data in parsing frame queue and the data displaying for being shown as the producer.
Specific embodiment seven:With reference to Fig. 1 and Fig. 2 explanation present embodiments, present embodiment is to implementation method six
It is described further, the UI events produce step to specifically include:
Action for perceiving human-computer interaction interface control by application affairs structure function, produces event, and to the thing
The step of part is identified by being worth change;
For joining the team array function using the action of each control as an event branch by element, by each event branch
The step of corresponding event command is stored in event message queue with character string forms.
Specific embodiment eight:With reference to Fig. 1 and Fig. 2 explanation present embodiments, present embodiment is to implementation method six
Or seven be described further, the UI event processing steps are specifically included:
The step of for obtaining event message queue by element dequeue function;
For by element array function of joining the team that event branch is corresponding with conditional branching, and group is carried out according to protocol frame requirement
Frame, data frame to be sent is turned into after CRC16 is verified, and is sent into after data frame to be sent is bundled with command string and referred to
The step of making queue.
Specific embodiment nine:Present embodiment is illustrated with reference to Fig. 1 to Fig. 3, present embodiment is to implementation method eight
It is described further, the data is activation is specifically included with receiving step:
Join for carrying out VISA serial ports parameter, baud rate parameter, data bit parameter, stop position by serial ports configuration module
Number, the initial configuration of check bit parameter and Row control parameter and disable respective attributes node in serial ports transceiving data in case
Only the step of configuration change;
For the data frame of instruction queue and the life of binding to be obtained by element dequeue function by serial ports sending module
Character string is made, command string is judged into branch for construction of condition, data frame is write into serial ports by VISA sends to bottom
The step of machine;
The serial data of slave computer is read for the VISA by serial ports receiver module, and the slave computer data of reading are sent
The step of entering data receiver queue;
Function is closed for the VISA by serial ports closedown module to close serial ports, to recover disabled during initial configuration
Attribute node the step of.
Specific embodiment ten:Present embodiment is illustrated with reference to Fig. 1, present embodiment is made into one to implementation method nine
Step explanation, the data analyzing step is specifically included:
The step of for obtaining the data in data receiver queue by element dequeue function;
The step of for being parsed to the data in data receiving queue by analysis state machine;
Bundled with command string for being parsed analysis state machine after the data for obtaining are converted to character string by function
The step of cluster feeding parsing frame queue;
The data displaying is specifically included:
The step of for obtaining the data in parsing frame queue by element dequeue function;
Releasing binding is carried out for parsing the data in frame queue by function pair, command character is series-connected to condition knot
The branch selector of structure function, after data character string is converted into byte arrays, the step of display.
Serial port communication method based on LabVIEW is applied in Practical Project, realizes the communication of slave computer and host computer, is entered
And the process phase in specific method and the specific embodiment five run come controlled motor by the operation of front panel in host computer
Together.