Summary of the invention
The present invention seeks to have loss of data and the lengthy and jumbled many problems consuming time of resolution data frame program in order to solve the existing serial communication based on LabVIEW programming, a kind of serial communication device and method based on LabVIEW is provided.
Serial communication device based on LabVIEW of the present invention, described communicator is realized and the communicating by letter of slave computer as host computer, and it comprises:
For the action of perception human-computer interaction interface control, generation event is also sent into the event of generation the UI event generation module of event message queue with character string forms as the producer;
For receive the character string of event message queue as consumer, and the corresponding event of character string representative is carried out to framing produce Frame, be re-used as the producer Frame of generation is sent into the UI event processing module of instruction queue;
For receive the Frame of instruction queue as consumer, this Frame serial ports is sent to slave computer; Receive the serial data that slave computer transmits simultaneously, and as the producer, the serial data of reception is sent into the data send and receive module of data receiver queue;
For receiving the data of data receiver queue and utilize parse state machine to resolve the data in data receiving queue as consumer, and the data that parsing obtained as the producer are sent into the data resolution module of parse for frame queue;
For receiving the data of parse for frame queue the data disaply moudle showing as the producer.
Described UI event generation module specifically comprises:
For the action of perception human-computer interaction interface control, generation event, and this event is changed to the application affairs structure function of identifying by value;
For using the action of each control as an event branch, the element that deposits event command corresponding each event branch in event message queue with the character string forms array function of joining the team.
Described UI event processing module specifically comprises:
For obtaining the element dequeue function of event message queue;
For event branch is corresponding with conditional branching, and require to carry out framing according to protocol frame, by becoming Frame to be sent after CRC16 verification, the element of sending into instruction queue after Frame to be sent and command string the are bundled array function of joining the team.
Described data send and receive module specifically comprises:
For carrying out the initial configuration of VISA string slogan parameter, baud rate parameter, data bit parameter, position of rest parameter, check bit parameter and stream range control parameter and forbidding respective attributes node to prevent the serial ports configuration module of described configuration change in the time of serial ports transceiving data;
For obtaining the Frame of instruction queue and the command string of binding by element dequeue function, command string is used for to construction of condition and judges branch, Frame is write to serial ports and is sent to the serial ports sending module of slave computer by VISA;
For reading the serial data of slave computer by VISA, and the slave computer data that read are sent into the serial ports receiver module of data receiver queue;
Close serial ports for closing function by VISA, the serial ports closing module of disabled attribute node when recovering initial configuration.
Described data resolution module specifically comprises:
Be used for the element dequeue function of the data of obtaining data receiver queue;
For the parse state machine that the data of data receiving queue are resolved;
Be converted to for parse state machine being resolved to the data that obtain the function of sending into parse for frame queue after character string with command string binding cluster;
Described data disaply moudle specifically comprises:
Be used for the element dequeue function of the data of obtaining parse for frame queue;
For the data of parse for frame queue are removed to binding, command character is series-connected to the branching selection device of construction of condition function, data character string is converted after byte arrays to the function showing.
Based on a serial port communication method of LabVIEW, described communication means is for realizing communicating by letter of host computer and slave computer, and it comprises:
For the action of perception human-computer interaction interface control, generation event the UI event of the event of generation being sent into event message queue with character string forms as the producer produce step;
For receive the character string of event message queue as consumer, and the corresponding event of character string representative is carried out to framing produce Frame, be re-used as the producer Frame of generation is sent into the UI event handling step of instruction queue;
For receive the Frame of instruction queue as consumer, this Frame serial ports is sent to slave computer; Receive the serial data that slave computer transmits simultaneously, and as the producer, the serial data of reception is sent into the data send and receive step of data receiver queue;
For receiving the data of data receiver queue and utilize parse state machine to resolve the data in data receiving queue as consumer, and the data that parsing obtained as the producer are sent into the Data Analysis step of parse for frame queue;
For receiving the data of parse for frame queue the data displaying showing as the producer.
Described UI event produces step and specifically comprises:
For passing through the action of application affairs structure function perception human-computer interaction interface control, generation event, and this event is changed to the step of identifying by value;
For joining the team array function using the action of each control as an event branch by element, event command corresponding to each event branch deposited in to the step of event message queue with character string forms.
Described UI event handling step specifically comprises:
For obtain the step of event message queue by element dequeue function;
For the array function of joining the team by element, event branch is corresponding with conditional branching, and require to carry out framing according to protocol frame, by becoming Frame to be sent after CRC16 verification, the step of instruction queue will be sent into after Frame to be sent and command string binding.
Described data send and receive step specifically comprises:
For carrying out the initial configuration of VISA string slogan parameter, baud rate parameter, data bit parameter, position of rest parameter, check bit parameter and stream range control parameter by serial ports configuration module and forbidding respective attributes node to prevent the step of described configuration change when the serial ports transceiving data;
For obtaining the Frame of instruction queue and the command string of binding by serial ports sending module by element dequeue function, command string is used for to construction of condition and judges branch, Frame is write to serial ports and is sent to the step of slave computer by VISA;
For reading the serial data of slave computer by the VISA of serial ports receiver module, and the slave computer data that read are sent into the step of data receiver queue;
Close serial ports for closing function by the VISA of serial ports closing module, the step of disabled attribute node when recovering initial configuration.
Described Data Analysis step specifically comprises:
For obtain the step of the data of data receiver queue by element dequeue function;
For the step of the data of data receiving queue being resolved by parse state machine;
Be converted to for parse state machine being resolved to the data that obtain by function the step of sending into parse for frame queue after character string with command string binding cluster;
Described data displaying specifically comprises:
For obtain the step of the data of parse for frame queue by element dequeue function;
For the data of parse for frame queue being removed to binding by function, command character is series-connected to the branching selection device of construction of condition function, data character string is converted after byte arrays to the step showing.
Advantage of the present invention: in device of the present invention or method, UI event generation module is communicated by letter by event message queue with UI event processing module, UI event processing module is communicated by letter by instruction queue with data send and receive module, data send and receive module is communicated by letter by data receiver queue with data resolution module, data resolution module is communicated by letter by parse for frame queue with data disaply moudle, four layers of producer consumer framework and each module executed in parallel are formed, the data that data resolution module or step are used the method for parse state machine to receive serial ports obtain complete resolution data frame according to parse for frame protocol analysis, ensure the integrality of communication data transmission, program simple and clear, execution speed is fast and stable.
The present invention utilizes producer consumer framework and state machine to realize serial ports and receives, sends and resolve demonstration, guarantees that data avoid having ensured that program is succinct when loss, moves consuming time short.Its uses on the one hand four layers of producer consumer framework between five functional modules, and program orderliness is clear and take full advantage of the advantage of LabVIEW executed in parallel, has improved operating rate; With parse state machine, data are resolved on the other hand, solved the reception data that often occur in program in the past complete or be the problems such as sky and the lengthy and jumbled length consuming time of program, greatly degree has been optimized serial communication program.
Embodiment
Embodiment one: below in conjunction with Fig. 1 and Fig. 2, present embodiment is described, the serial communication device based on LabVIEW described in present embodiment, described communicator is realized and the communicating by letter of slave computer as host computer, and it comprises:
For the action of perception human-computer interaction interface control, generation event is also sent into the event of generation the UI event generation module 1 of event message queue with character string forms as the producer;
For receive the character string of event message queue as consumer, and the corresponding event of character string representative is carried out to framing produce Frame, be re-used as the producer Frame of generation is sent into the UI event processing module 2 of instruction queue;
For receive the Frame of instruction queue as consumer, this Frame serial ports is sent to slave computer; Receive the serial data that slave computer transmits simultaneously, and as the producer, the serial data of reception is sent into the data send and receive module 3 of data receiver queue;
For receiving the data of data receiver queue and utilize parse state machine to resolve the data in data receiving queue as consumer, and the data that parsing obtained as the producer are sent into the data resolution module 4 of parse for frame queue;
For the data disaply moudle 5 that receives the data of parse for frame queue and show as the producer.
Embodiment two: below in conjunction with Fig. 1 and Fig. 2, present embodiment is described, present embodiment is described further embodiment one, described UI event generation module 1 specifically comprises:
For the action of perception human-computer interaction interface control, generation event, and this event is changed to the application affairs structure function of identifying by value;
For using the action of each control as an event branch, the element that deposits event command corresponding each event branch in event message queue with the character string forms array function of joining the team.
In present embodiment, UI event generation module 1 application affairs structure function is identified by value change event the action of each control on human-computer interaction interface panel, the action of each control is as an event branch, array function deposits in event message queue that each branch produces corresponding event command and the element that carries by LabVIEW using character string forms is joined the team now UI event generation module 1 as the producer.
Embodiment three: below in conjunction with Fig. 1 and Fig. 2, present embodiment is described, present embodiment is described further embodiment one or two, described UI event processing module 2 specifically comprises:
For obtaining the element dequeue function of event message queue;
For event branch is corresponding with conditional branching, and require to carry out framing according to protocol frame, by becoming Frame to be sent after CRC16 verification, the element of sending into instruction queue after Frame to be sent and command string the are bundled array function of joining the team.
In present embodiment, UI event processing module 2 obtains the data of event message queue by the element dequeue function in LabVIEW, now UI event processing module 2 is as consumer, application conditions structure function is realized the corresponding independent conditional branching of each event, require to carry out framing according to event according to protocol frame, the Frame obtaining becomes Frame to be sent after by CRC16 verification, utilize and show that control shows with hexadecimal string frame to be sent at man-machine interface panel, the element simultaneously carrying by LabVIEW is joined the team array function bundlees Frame with command string after and is sent into instruction queue, now UI event processing module 2 is as the producer.
Embodiment four: below in conjunction with Fig. 1 to Fig. 3, present embodiment is described, present embodiment is described further embodiment three, described data send and receive module 3 specifically comprises:
For carrying out the initial configuration of VISA string slogan parameter, baud rate parameter, data bit parameter, position of rest parameter, check bit parameter and stream range control parameter and forbidding respective attributes node to prevent the serial ports configuration module of described configuration change in the time of serial ports transceiving data;
For obtaining the Frame of instruction queue and the command string of binding by element dequeue function, command string is used for to construction of condition and judges branch, Frame is write to serial ports and is sent to the serial ports sending module of slave computer by VISA;
For reading the serial data of slave computer by VISA, and the slave computer data that read are sent into the serial ports receiver module of data receiver queue;
Close serial ports for closing function by VISA, the serial ports closing module of disabled attribute node when recovering initial configuration.
Present embodiment comprises serial ports configuration module, serial ports sending module, serial ports receiver module and serial ports closing module, and serial ports configuration module carries out the initial configuration of VISA string slogan, baud rate, data bit, position of rest, these parameters of check bit sum flow process control and forbids that by the method for forbidding respective attributes node these configurations are changed in the time that serial ports is received and dispatched; Serial ports sending module obtains the data of instruction queue by the element dequeue function in LabVIEW, after now data send and receive module 3 bundlees data solution as consumer, command string judges branch for construction of condition, data character string is used for sending to slave computer, the serial ports function VISA carrying with LabVIEW carries out transceiving data, is write and is sent the data to slave computer by VISA; Serial ports receiver module is read slave computer data is read back in real time and are shown to man-machine interface panel and send into data receiver queue by VISA, and now data send and receive module 3 is as the producer; Close serial port module and close function by VISA and close serial ports, disabled each parameter attribute node while simultaneously recovering initialization.
Embodiment five: below in conjunction with Fig. 1 to Fig. 3, present embodiment is described, present embodiment is described further embodiment four, described data resolution module 4 specifically comprises:
Be used for the element dequeue function of the data of obtaining data receiver queue;
For the parse state machine that the data of data receiving queue are resolved;
Be converted to for parse state machine being resolved to the data that obtain the function of sending into parse for frame queue after character string with command string binding cluster;
Described data disaply moudle 5 specifically comprises:
Be used for the element dequeue function of the data of obtaining parse for frame queue;
For the data of parse for frame queue are removed to binding, command character is series-connected to the branching selection device of construction of condition function, data character string is converted after byte arrays to the function showing.
Described branching selection device is on construction of condition function.
In present embodiment, shown in Fig. 3, data resolution module 4 obtains by the element dequeue function in LabVIEW the data that serial ports reads back, resolve by protocol analysis state machine, now data resolution module 4 is as consumer, data resolution module 4 outermost layers are operations that while circulates to maintain state machine, inferior skin is a construction of condition, be used for judging whether overtime, lower one deck be for circulation so that the data in the data receiver queue that receives of data resolution module 4 enter one by one and loop parsing with the form of byte, construction of condition at for circulation internal layer, be used for each different state to judge, under each state, handling data can indicate next step to enter which state, the function of Here it is shift register, by next state transfer in the judgement of recurrent state next time, data resolution module 4 obtains the data of data receiver queue, by character string to byte arrays transfer function, data is converted to byte arrays, and then for that sends into of byte circulates one by one, and now data resolution module is as consumer, the data that serial ports reads back are through obtaining parse for frame after state machine, and parse for frame array is sent into parse for frame queue with command string binding cluster after being converted to character string, and now data resolution module 4 is as the producer.
Data disaply moudle 5 skins are operations that while circulates to ensure display module, internal layer is construction of condition, data disaply moudle 5 obtains the data in parse for frame queue by the element dequeue function in LabVIEW, after dequeuing data row, remove binding, command character is wherein series-connected to the branching selection device of construction of condition, data character string converts to after byte arrays for showing, now data disaply moudle 5 is as consumer.
The present invention is applied in Practical Project, realizes communicating by letter of slave computer and host computer, and then control motor operation by the operation of front panel in host computer:
Adopt the serial communication framework shown in Fig. 1, form four layers of producer consumer framework and each module executed in parallel, the action of UI event generation module 1 perception front panel control, generation event is also sent into event message queue by event with character string forms as the producer, character string relative each event that UI event processing module 2 receives in event message queue as consumer are carried out framing, this is ground floor producer consumer framework, UI event processing module 2 is re-used as the producer Frame of generation is sent into instruction queue, the data that data send and receive module 3 receives in instruction queue as consumer are carried out serial ports transmission and receive serial data, this is second layer producer consumer framework, data send and receive module 3 is re-used as the producer data that receive is sent into data receiver queue, data resolution module 4 receives the data in data receiver queue and resolves as consumer, this is the 3rd layer of producer consumer framework, data resolution module 4 is re-used as the data that the producer obtains parsing and sends in parse for frame queue, data disaply moudle 5 receives the data in parse for frame queue and shows as the producer, this is the 4th layer of producer consumer framework.
UI event generation module 1 skin is the operation that while circulates to maintain event generation module, in while circulation, adopt event structure function, on time structure block diagram, right button selects interpolation event branch that corresponding front panel control is joined to corresponding event branch, produce after the binding of corresponding event message and variant constant by the element array function of joining the team and send in event message queue, now UI event generation module 1 is as the producer.
As shown in Figure 2, the serial communication framework of LabVIEW has multithreading, so the mode of stopping adopts the mode of circulation expiry notification device, use " obtaining notifying device quotes " function outward in each circulation, use " sending notice " function to send notice that stop button is pressed to expiry notification device in " change of ' stopping ' value " branch of UI event generation module, the outermost layer while that this notice is delivered to each thread by expiry notification device circulates, " cycling condition " button notice assignment being circulated to while by " waiting wait order " function, finish the operation of whole program with this.
UI event processing module 2 outermost layers are operations that while circulates to ensure this module, data in reception event message queue are series-connected to command character the branching selection device of construction of condition after removing binding, now UI event processing module 2, as consumer, carries out framing according to protocol frame standard in corresponding conditional branching.Utilize array function by a frame byte arrays of composition to be sent and add CRC check, present case is used CRC16 verification, and core concept is the check bit that the .dll file that utilizes " call library function node " to call the CRC16 verification having generated generates 16bit.The Frame array that obtains after framing completes enters on the one hand and cycles through " numerical value to hexadecimal string is changed " function and enters next for circulation again, obtain for the hexadecimal transmission frame character string showing by " connection string " function, by showing that control shows at front panel; After changed string, send into instruction queue with command string binding on the other hand, now UI event processing module 2 is as the producer.
As shown in Figure 3, serial ports sending module and serial ports receiver module are in a while circulation, and while circulation ensures the continuity of transmitting-receiving.
Data resolution module 4 outermost layers circulate to maintain the operation of parse state machine with while, outside while circulation, state and data used are carried out to initialization, in while circulation, be successively respectively from outside to inside construction of condition and judge that whether element dequeue is overtime, for circulation makes the data that receive process one by one with byte form, the processing of data and the selection to NextState under construction of condition selection mode and each state, state and data stream are flowing into and outflow while circulation and for circulation time use shift register, after having ensured like this to handle data under each state, indicate next step to enter next designated state.In outer while circulation, data resolution module is reading out data character string from data receiver queue, after converting byte arrays to, send into one by one for circulation, now data resolution module 4 is as consumer, byte enters the branches of states that enters first acquiescence after for circulates, judge according to parse for frame agreement, first determine whether frame head, below determining in innermost layer construction of condition according to result, enter which state and select whether add this secondary data to parse for frame array, this state imports into by shift register in the state judgement of recurrent state alternative condition structure next time, state machine can freely be changed between each state like this, after finally obtaining being converted to character string and command string binding according to the resolution data frame number group of protocol frame requirement and by array, send into parse for frame queue, now data resolution module 4 is as the producer.
Data disaply moudle 5 is made up of the construction of condition of outer field while circulation and internal layer, plays respectively the effect that maintains display module operation and select displaying contents.Data disaply moudle 5 is separated bundling operation to data after obtaining the data in parse for frame queue, command string is sent into condition selector switch, data character string converts to and enters on the one hand after byte arrays and cycle through " numerical value to hexadecimal string is changed " function and enter next for again and cycle through " connection string " function and obtain for the hexadecimal parse for frame character string showing, and by showing that control shows at front panel, according to conditional branching content to display and parsing Frame Protocol, resolution data array is operated on the other hand, mainly utilize character string function " intercepting character string ", the LabVIEW such as dialog box function " button dialog box " carry out difference to require from tape function the integration of displaying contents, now data disaply moudle 5 is as consumer.
Embodiment six: below in conjunction with Fig. 1 and Fig. 2, present embodiment is described, the serial port communication method based on LabVIEW described in present embodiment, described communication means is for realizing communicating by letter of host computer and slave computer, and it comprises:
For the action of perception human-computer interaction interface control, generation event the UI event of the event of generation being sent into event message queue with character string forms as the producer produce step;
For receive the character string of event message queue as consumer, and the corresponding event of character string representative is carried out to framing produce Frame, be re-used as the producer Frame of generation is sent into the UI event handling step of instruction queue;
For receive the Frame of instruction queue as consumer, this Frame serial ports is sent to slave computer; Receive the serial data that slave computer transmits simultaneously, and as the producer, the serial data of reception is sent into the data send and receive step of data receiver queue;
For receiving the data of data receiver queue and utilize parse state machine to resolve the data in data receiving queue as consumer, and the data that parsing obtained as the producer are sent into the Data Analysis step of parse for frame queue;
For receiving the data of parse for frame queue the data displaying showing as the producer.
Embodiment seven: below in conjunction with Fig. 1 and Fig. 2, present embodiment is described, present embodiment is described further embodiment six, described UI event produces step and specifically comprises:
For passing through the action of application affairs structure function perception human-computer interaction interface control, generation event, and this event is changed to the step of identifying by value;
For joining the team array function using the action of each control as an event branch by element, event command corresponding to each event branch deposited in to the step of event message queue with character string forms.
Embodiment eight: below in conjunction with Fig. 1 and Fig. 2, present embodiment is described, present embodiment is described further embodiment six or seven, described UI event handling step specifically comprises:
For obtain the step of event message queue by element dequeue function;
For the array function of joining the team by element, event branch is corresponding with conditional branching, and require to carry out framing according to protocol frame, by becoming Frame to be sent after CRC16 verification, the step of instruction queue will be sent into after Frame to be sent and command string binding.
Embodiment nine: below in conjunction with Fig. 1 to Fig. 3, present embodiment is described, present embodiment is described further embodiment eight, described data send and receive step specifically comprises:
For carrying out the initial configuration of VISA string slogan parameter, baud rate parameter, data bit parameter, position of rest parameter, check bit parameter and stream range control parameter by serial ports configuration module and forbidding respective attributes node to prevent the step of described configuration change when the serial ports transceiving data;
For obtaining the Frame of instruction queue and the command string of binding by serial ports sending module by element dequeue function, command string is used for to construction of condition and judges branch, Frame is write to serial ports and is sent to the step of slave computer by VISA;
For reading the serial data of slave computer by the VISA of serial ports receiver module, and the slave computer data that read are sent into the step of data receiver queue;
Close serial ports for closing function by the VISA of serial ports closing module, the step of disabled attribute node when recovering initial configuration.
Embodiment ten: below in conjunction with Fig. 1, present embodiment is described, present embodiment is described further embodiment nine, described Data Analysis step specifically comprises:
For obtain the step of the data of data receiver queue by element dequeue function;
For the step of the data of data receiving queue being resolved by parse state machine;
Be converted to for parse state machine being resolved to the data that obtain by function the step of sending into parse for frame queue after character string with command string binding cluster;
Described data displaying specifically comprises:
For obtain the step of the data of parse for frame queue by element dequeue function;
For the data of parse for frame queue being removed to binding by function, command character is series-connected to the branching selection device of construction of condition function, data character string is converted after byte arrays to the step showing.
Serial port communication method based on LabVIEW is applied in Practical Project, realizes communicating by letter of slave computer and host computer, and then it is identical with the process in embodiment five to control the concrete grammar of motor operation by the operation of front panel in host computer.