A kind of Internet of Things communication means based on WCF
Technical field
The invention belongs to fields of communication technology, are particularly suitable for oriented application technical communication field.
Background technique
By data application that existing program is obtained from equipment it is a kind of trend in internet by certain communication mechanism, realizes
There are many method of Internet of Things application communication, but the communication capture program based on windows platform exploitation directly turns internet communication
Method and few, wherein it is Microsoft that most of method, which is by MFC(Microsoft Foundation Classes,
The class libraries provided) the obtained data of capture program are first stored in database, then from Internet application website by user to
Database request data.The data acquisition module of terminal device is developed based on MFC in the present invention, passes through service-oriented communication equipment
System does not achieve the effect that real time communication directly by collected data-pushing to webpage client foreground by database.
Summary of the invention
The present invention proposes a set of Internet of Things communication party based on WCF(Windows Communication Foundation)
Method, this method realize that MFC data acquisition module is communicated with Website server module (website of C# or other language developments).We
Case MFC data acquisition module is based on completing port model (referred to as: IOCP), establishes telecommunication with terminal device to obtain number
According to, and give data-pushing to Website server module real-time display, local communication can also be established with standalone version and terminal device
Data are obtained, then give data-pushing to Website server module real-time display.
The present invention is by MFC data acquisition module, oriented application module (WCF) and Website server module three parts
Composition is built between MFC data acquisition module and Website server module by the function that oriented application module (WCF) is created
Vertical communication.
1, MFC data acquisition module
MFC data acquisition module includes three submodules: the Internet communication module, Local Communication Module and interprocess communication
Module.
Three submodules of MFC data acquisition module cannot be run simultaneously, such as enable one of submodule, then and other two
A submodule does not work.The Internet communication module is applied to communicate with terminal unit remote, and Local Communication Module is applied to and end
End equipment serial ports or CAN communication, interprocess communication module application are based on windows platform with other in MFC data acquisition module
Interapplication communications.
Remotely (internet) communicating requirement MFC data capture program is mounted on the computer (letter of a WINDOWS platform
Claim: PC) in, PC needs that there is public network IP address to connect with communication port for remote terminal and receives the number for sending remote terminal
According to local communication and interprocess communication only need MFC data capture program to be mounted on networking (Internet/Intratnet)
On PC.PC and the work of oriented application module and Website server module cooperative serve data to more users use.
To solve the problems, such as the virtual connections of TCP/IP communication and guaranteeing the validity and normalization of telecommunication, Netcom is interconnected
The application layer protocol that letter module uses is 794 standard agreement of JT/T, and traffic model uses IOCP model, and IOCP is based on
Socket programming, suitable for controlling the high load server concurrently executed.
Local Communication Module mainly divides serial communication and CAN (Controller Area NetWork) to communicate, serial communication
Based on windows32 systems, using two kinds of programming modes, first is that using ActiveX control, second is that calling the API of Windows
Function.The application layer protocol of Can communication is CAN Cailbration Protocol(abbreviation: CCP) agreement, turned based on CAN
The driver that Usb protocol converter provides develops CAN communication, which is a kind of dynamic link library.
Interprocess communication module handles other and is based on mainly using MFC data acquisition module as a transit module
The data of the application program of windows platform, and the data of acquisition are passed through into oriented application module and network server mould
Block Real-time Feedback gives multiple users.As transit module, user only needs to pay close attention to interprocess communication mode MFC data acquisition module
And the design of the business model of interface and service-oriented WCF module.
2, oriented application module (WCF)
Oriented application module (WCF) mainly contains communication module and service logic data-interface.Communication module includes two
A WCF program, this is defined herein as WCF1 and WCF2.The data of WCF1 processing parsing MFC data acquisition module transmitting, WCF2 are used for
Realize duplex communication (duplex communication: transmitted in both directions can be carried out in synchronization information by referring to) propelling data to Website server mould
Block.Service logic data-interface is power function in oriented application module (WCF), and wherein WCF1 includes 4 power functions:
General data processing function (GetData) handles function for receiving general data, structural body linked list data
(GetDataUsingDataContract) for receiving structured data.It opens database functions (openSql) and closes number
According to library function (CloseSql).WCF2 includes 3 power functions, one interface function: general data handles function (GetData)
For receiving common data, function (ResgiterListenter) is monitored in registration and function is monitored in anti-registration
(UnResgisterLister) it is registered for Web client to WCF2, the Web client after registration can receive WCF2 publication
Data.For callback interface function (ITesCallback) for creating Virtual Function (Refresh), Web client passes through instantiation
Refresh function realizes client refreshes effect.
WCF1 is consistent with the structural body chained list interface that MFC data acquisition module is established, by gSoap compilation tool
WSDL(preprocessor) header file that generates C/C++ style, it is automatically generated further according to header file and calls long-range SOAP service visitor
Family end code (referred to as counterfoil: Stub) and provide the skeleton code (referred to as frame: Skeleton) of SOAP service, structural body chained list
For the data of storage by calling WCF1 function passes, this function is exactly MFC data capture program and the interface of WCF1.In communication
For the binding protocol used for basicHttpbinding, basicHttpbinding uses Http/Https agreement, is mainly used for
It cooperates with old Web service.Its traffic model used is InstanceContextMode.Single mode.
InstanceContextMode.Single example behavior is similar to one-piece design mode, and the shared service of all clients is real
Example, this Service Instance is a global variable, makes each module that WCF1 function variable be called to become latest data.
But basicHttpbinding binding protocol does not support duplex communication, and in order to support duplex communication, the present invention is set again
Counted WCF2 program, needed to be pushed to the data of Website server for handling, WCF2 as service respectively with WCF1 and website
Server module establishes communication, and WCF1 is the host of WCF2, by the function passes real time data for calling WCF2.WCF1 and WCF2
It can be with distributed structure/architecture, to alleviate computer pressure.WCF2 mainly handles callback mechanism, is given to data-pushing after receiving data
The Website server module that it is subscribed to.The callback operation of WCF2 is the method for calling website service module by WCF2.Callback operation
Only readjustment just can be used in the binding protocol with two-way communications capabilities, uses netTcpbinding binding association in this method
View by the CallBackContract attribute in setting ServiceContract characteristic and creates callback contracts ITes
TCallback instantiates callback object and creates a context object InstanceContext by it again, then with acting on behalf of handle
WCF2 is passed in this readjustment reference back.
3, Website server module
Website server module includes Web server end and Web client, using MVC(Model-View-Control)
Framework, wherein Model and Control is mainly used for Web server end, and View is mainly used for Web client.Website server mould
Block is there are two types of communication pattern: WCF2 information being pushed to Web client and is shown and by Web client data back to MFC data
Acquisition module.
Website server module allow Web client and Web server end can mutual notification message and call method in real time,
It is also responsible for pushing data into the Web client page, while browser also has good compatibility.
Installing ASP.NET Signalr class libraries packet in Website server module and having registered a HUB(HUB is Signalr
The one mode of class libraries communication), it can share the information being sent on Web server with all Web clients, while Web takes
Business end can call client script.Client and server end uses JOSN format (JavaScript Object
Notation is a kind of data interchange format of lightweight) data are exchanged, it is relied in the HUB of server end statement .Net
Proxy generates proxy class, is then that JSON is converted into object inside Proxy, client can see object.
Web client calls Website server modularity function to communicate with the end WCF1, and Website server module is as WCF1's
Host can call directly the function of WCF1, the data feedback of the instruction and optimization that are formed by power function to WCF1, by
WCF1 notifies that application program will be in data back to terminal device.
WCF1 is unable to reverse calling MFC acquisition data module processing affairs, and WCF1 sends number to MFC acquisition data module
This problem is solved according to message queue (MSMQ) need to be used, WCF1 is converted to the service request of Website server specified
Message format is stored in message queue, and MFC capture program module is taken away message queue information using thread architecture, is passed through
Socket, serial ports or CAN are written back in terminal device.
Beneficial effects of the present invention are as follows:
1, professional platform independence is good: WCF is that Microsoft is the distributed communication programming frame that the service-oriented application of building provides
Frame is the important component of .NET Framework.Using the frame, developer can construct cross-platform, safe and reliable
With the enterprise-level interconnecting application solution for supporting issued transaction.
2, performance is good: the MFC interapplication communications based on completing port model.Completing port, which designs a model, to be only applicable to
2000 operating system of Windows NT and Windows, the complexity of design, need while managing in application program hundreds of be
When to thousands of a sockets, and it is desirable that increasing with the CPU quantity installed in system, the performance of application program can also be with
It is linear to be promoted.But its interface communication and professional platform independence are poor, can be connect it with Web client by this programme.
3, modularization is good: present solution provides the data of a whole set of MFC programmed acquisition to Web
The solution that client is shown.Package is made of three big modules, and each module can be disassembled, with other
Scheme is communicated by general-purpose interface.
4, display effect is good: can be adopted MFC based on the Signalr+WCF+JavaScript communication technology and developing thought
The real time data of collection is shown to page end in time, makes up the problem of webpage end data instant playback interactivity difference.
5, distributed structure/architecture is good: each module of this programme can be with framework in different computers, by oriented application
Module (WCF) is responsible for communication.
Detailed description of the invention
Internet of Things network communication system of the Fig. 1 based on WCF
Fig. 1 shows the building forms and connection signal of each module of this solution.Terminal device or other be based on WINDOWS
The application program of platform is communicated by certain way with the foundation of MFC data acquisition module, and MFC data acquisition module passes through communication again
Interface is communicated with oriented application module (WCF) foundation.Finally connect towards service application module (WCF) program further according to general
Mouth function and related callback mechanism are communicated with the foundation of website service module.
Communication mechanism of the Fig. 2 based on WCF programming model
Fig. 2 illustrates the specific communication protocol and communication mode of each module.Proposed in this solution remote terminal with
MFC data acquisition module is based on TCP/IP and puppy parc is communicated.Local terminal can also by using CCP agreement or
Custom protocol communication.To realize duplex communication, this programme oriented application module (WCF) designs two WCF programs to divide
Cloth communication.MFC acquires data module as host and WCF1 method is called to transmit data, and WCF1 is used as WCF2 host to lead to again
Letter, WCF2 and WCF1 and Website server are based on netTcpbingding protocol communication, and WCF2 utilizes duplex technology (readjustment machine
System is properly termed as subscribing to issue mechanism) to Website server module propelling data.Last Website server module receives WCF2 number
HUB is established in server end using Signalr class libraries technology according to rear, realizes Website server by establishing proxy class by client
It holds to client message push function, realizes that webpage client refreshes the good display effect of flicker free with JavaScript technology
Fruit.
In addition Website server module can be taken as WCF1 host calls the method for WCF1 to remote terminal write-back number
According to.WCF1 is write on instruction in message queue by way of message queue, then by MFC data capture program by reading message
Queue realizes calibrating function after taking data away.
Specific embodiment
The system of Internet of Things Network Communication of the Fig. 1 based on WCF
1, MFC data acquisition module
MFC data acquisition module is responsible for and terminal equipment in communication.There are two types of terminal device connection types: internet communication,
Local communication.There are also one is communicate with other applications.Do not worked together between each module, but can be by application demand at
Manage different types of affairs.Here internet communication is mainly specifically introduced.
MFC data acquisition module is built on the PC of an internet or application Cloud Server, provides the company of terminal device
Connect information: fixed domain name addresses, port numbers.Terminal can send message to MFC data acquisition module after establishing connection.MFC
Data acquisition module can receive the data of multiple terminal devices.MFC data acquisition module receives terminal device data can be with
It is interpreted as packs of arrays.Each array needs MFC data acquisition module to go to parse its meaning by business logic modules,
It finally places the data in a structural body chained list and passes to oriented application module (WCF) again and go to handle.
2, MFC data acquisition module is communicated with oriented application module (WCF)
MFC data acquisition module is by calling oriented application module (WCF) communication module function to establish communication.Towards
Service application module (WCF) can acquire data program with MFC and be mounted on same server, can also be with distributed structure/architecture.
Oriented application module (WCF) can receive and forward data to the application program of any platform, here we
The data of MFC data capture program are received, then data are carried out with the analysis of service logic, relays to our Website servers
Module and database.Business logic modules are mainly the data of optimization processing MFC acquisition, as we have 100 terminal devices, mesh
The preceding equipment for establishing communication has 20, and each equipment includes its ID, manufacturer, position, generation information etc., we are these
Information is stored in one structural body chained list of MFC data capture program, by calling oriented application module (WCF) to provide
Function passes data will need the information saved to be stored in database by oriented application module (WCF), and real time data is straight
It connects and is pushed to Web client user's webpage is allowed to check.
3, Website server module
The customer terminal webpage of Web obtains data by two ways, and one is client user's activly request, another kind is
Data-pushing is given to website service end by oriented application module (WCF), then Web client is pushed to by website service end.
Communication mechanism of the Fig. 2 based on WCF programming model.
1, terminal device and MFC data acquisition module internet communication
Terminal device active handshake request data, the passive connection reply terminal of MFC data capture program.Communication protocol uses
Communication mode should meet the relevant regulations in JT/T 794, communication protocol uses TCP or UDP, MFC data acquisition module
As server end, internet ECU(Electronic Control Unit) electronic control unit can be used as terminal.Agreement
Word and double word are transmitted using the network bytes sequence of big end mode (big-endian).Every message by marker, message header, disappear
Cease body and check code composition.Terminal heartbeat is the data information of periodicity sending, sends and defaults after being spaced each electrification reset
It is 60 seconds, it is ensured that remote statistics equipment on-line state.Terminal periodic sends terminal heartbeat to platform, and platform replys a platform
General response.Message id: 0x0002.MFC data acquisition module is as sent: 7e 0221 11 12 11 11 11 11 7
b3 b6 7e.By 794 agreement of JT/T, it can learn that the data that number is 111211111 send a burst of heartbeat.
IOCP model of the MFC data acquisition module based on WindowsSoket, IOCP is mainly for data throughout and company
It connects concurrency and designs.Suitable for can control the high load server technology concurrently executed, it is capable of the model of asynchronous I/O operation.
Create first completing port (:: CreatetoCompletioPort ()) then creates multiple threads again, and is assigned to
This completing port reads data up.Long-range connection socket words and expressions handle is associated with this completing port again.To IOCP model
Under the MFC data capture program exploitation aspect of design focal point two: (1) memory management design aspect update (2) communications protocol and
It can not packet splicing process.
2, the communication of MFC data capture program and oriented application module (WCF)
MFC acquisition data program, which establishes structural body chained list (List:m_iClient) each node and contains a terminal, to be set
The information such as standby SocketId, position, device id, position, alarm condition, key message.
WCF1 calls the GetData function passes data of WCF2 as host
WCF1 is as service, address of service and interface function of the MFC data acquisition module by WCF1 exposure
(GetAlliClent) it is communicated as the host of WCF1.
The data of acquisition are stored in database according to after protocol analysis by executing SQL by WCF1, the number of real-time partial
According to WCF2 processing is passed to, real-time partial data are primarily referred to as which module is online, their position is somewhere etc..It adopts
It is unique with InstanceContextMode.Single mode principal security customer's representative instantiation, guarantee what client obtained
Data are newest parts.WCF1 transmits data, the server address and interface function of WCF2 exposure as the host of WCF2
(GetData).Interface function is mainly responsible for WCF2 propelling data.
WCF2 designs callback mechanism, can push the webpage client for being given to its subscription at once after receiving data.
Refresh method in ITesCallback interface class is used for Client Design callback method.
3, website service module propelling data is to Web client
ASP.NET Signalr packet is installed at website service end, and Web server end has registered a HUB and defines
CallWCFAsyncMetho method, client call callWCFAsyncMethod method to register back to WCF2 after generating agency
It adjusts.Website server module is by inheriting MvcGPRSApp1._1.ServiceReference2.ITestServiceCallb
Ack rewrites refresh method again to client push data (this.clients.All.sendMessage (value)).
Web client can refresh main interface using jquery (Javascript frame) technology after being connected to data update notification, here
We pass through " ajax " attribute using newest 2.9 plug-in unit of DataTable: '/DataModule/
DataProvider' realizes natural refreshing of the webpage without splashette sense.
4, website service module sends data to the end WCF1
The end Web client and oriented application module (WCF1) communicates, host of the Website server as WCF1, can be with
Call directly the function of WCF1.Web client calls website service module mainly to realize calibration function, such as needs terminal device
Stop sending data, or specific data are sent to terminal unit remote, Web client user is formed according to 794 agreement of JT/T
Instruction and optimization data feedback to WCF1, message queue (MSMQ:Message Queue) is formed by WCF1.
5, WCF1 sends data to MFC data acquisition module
WCF1 be unable to reverse calling MFC data acquisition module processing affairs, at this moment we using message queue (MSMQ) come
Solve this problem.The service request of Website server is converted to specified message format and is stored in message queue by WCF1.
MFC capture program module is taken away message queue information using threaded design thinking, then is returned by Socket or serial ports or Can mouthfuls
It writes in terminal device.