The personal computer below WINDOWS95 and NT and intelligence change the serial communication design of appearance

  • Time:
  • Click:141
  • source:NEWRGY CNC Machining
Summary: The article was discussed how to use Delphi3.

The object-oriented serial communication component below 0 development WINDOWS95 and NT devises a method, offerred source program, make apply a program to become very convenient about serial communication with Delphi development, can see the method that expand ceaselessly and abounds Delphi component from this. Use the serial communication component part that give out, can realize the computer below WINDOWS95 and NT environment and sheet easily piece the information switch that the intelligence such as machine turns ceremony, be in analyzer of ultrasonic liquid component, microcomputer man-made flix debugs a success in jacquard weave system, change the domain such as instrument appearance, CAD, CAM and computer control system to have certain real sense to intelligence. Keyword: Communication; Serial; Process designing; Component part; Object-oriented in the graph classifies date: TP313Designing Serial Communication Between The Microcomputer And The Intelligentized Instrument Under Windows95 And NT By OOPJiang Zetao(Dept.

Of Electronics Engineering, nanchang Institute Of Aeronautical Technology)Duan Longzhen(Computer Department Of Nachang University)Abstract: This Paper Discusses How To Develop The Method Of The OOP Serial Communication Component Under Windows95 And NT By Delphi3.

0. It Supplies Original Program And Makes It Quite Convenient To Program The Software Of Serial Communication Of Control In Delphi.

We Can Educe The Method Enhancing The Ability Of Developing The Program By Expanding Component Of Delphi.

It Is Easily Realized That The Information Exchanges Between The Microcomputer And The Intelligentized Instrument Under Windows95 And NT By The Component.

This Exchange Technology Was Successfully Used In The Ultrasonic Liquid Member Analyse Instrument And The Microcomputer Artificial Fur Weave Flower System.

It Has Some Practical Significance To Intelligence Instrument, CAD, CAM And The Computer Controlled Systems.

Key Words:COmmunication, serial, programme, component, OOP is introductive as computer soft hardware is beneficial development, the software that is based on Windows95 and NT platform is increasing, electronic appearance and computer control system are changed in intelligence Zhongdoushe is reached undertake information is exchanged between the computer and intelligent apparatus or computer, and serial communication is the computer between and the computer and odd a kind of when the digitlization instrument such as a machine corresponds important step, it is a kind of when realize industrial monitoring main way, because it is efficient reliable, low-cost, follow uniform standard, get applied extensively consequently. Develop ceaselessly as computer technology, process designing method also rises ceaselessly, wait for the method that uses object-oriented component part like Visual Basic, Delphi, Visual C++ and C++ Builder, make the applied program that writes Windows to fall becomes rapid it is easy to mix, among them Delphi function is powerful, code efficiency is tall, develop personnel eyeball to look at by software, but Delphi all did not supply communication component part euqally with Visual C++ and C++ Builder, the communication component part of API function or Visual Basic has to use when developing communication application software with Delphi for this, API function has certain difficulty to developing personnel commonly and not quite convenient, and the applied program that develops with the communication component of VB needs to be in WINDOWS95 or NT install and note cutout corresponding dynamic library ability moves, this is very no-go to applied user. For this article introduction uses API function and technology of multi-line Cheng process designing to be in Delphi3.

0 designs out already communication component, offerred entire source program, use Delphi to install new member method to install its from already in compiling a system, OK very develop communication program conveniently, this component in intelligence analyzer of supersonic liquid component and network of distributing center type measure those who control heat treatment system to be applied by successful ground. Can see the method that uses the content that Delphi weaves component part abounds Delphi ceaselessly from which. When the programmer that component of 1 serial communication designs a thought to be based on DOS process designing commonly is writing serial communication, often be to write to suspend service program, once serial mouth know exactly about sth occupies it to be able to be given out to CPU,suspend a request, the meeting after CPU is answering this to interrupt carries out those who string together a mouth to suspend service program, finish booked job thereby. Below Windows operating system, because Windows prohibits applying a program to be mixed directly hardware contact with, so programmer can use the standard function process designing that Windows offers only. Although because need not be opposite,hardware process designing goes to the lavatory to concerning hardware to debug, but Windows itself is far more complex than DOS, understand and use what carry parameter to these standard function and them so far also more difficult than DOS, in Windows3.

In X, when when communication equipment is opened and allowing deferent WM-COMMNOTIFY information, want this communication equipment to receive data only, the operating system can enter WM-COMMNOTIFY message in the buy in message alignment, the WM-COMMNOTIFY word that applied process can utter through intercepting and capture an operating system will undertake operating to the communication facility that already opened. In Windows95 and NT, revised Windows3.

The standard function that X handles to serial mouth, undertook more unified standardization, cancelled WM-COMMNOTIFY message and OpenComm, closeComm, readComm, writeComm, the function such as FlushComm, treat serial mouth to operate as the file same, its are serial of equipment open and shut an operation to use open with the file with shut the function with same operation, be like CreatFile, closeFile, readFile, writeFile, purgeComm, because what the user defines size allow to read in Windows95 and NT,write buffer, such data lose possibility very small, make at the same time read keep rate very fast. In technology of Windows95 and process designing of Cheng of multi-line of the support in NT, and Delphi3.

0 provided support for multi-line Cheng process designing and staff member, can weave so serial communication component, build namely new " .

Pak " the file went. Considering length, offer only in this component necessary and a few quite general and commonly used attribute and the event that produce according to from time to time when input amortize know exactly about sth, attribute can be inspected to be baud rate, data in these attribute, desired effect, stop, size of amortize of serial mouth name, input (read amortize namely) , output amortize size (keep cushion namely) , spark incident means; Moving attribute has the facility that string together a mouth sentence window of handle, message sentence handle, incident sentence handle; The method in moving has port to open shut function with port. The design thought of component part is: The data in can inspecting property, desired effect, stop, spark incident means is defined with Mei Ju type, the value that process designing personnel will choose to need conveniently went, size of amortize of name of rate of the baud in can inspecting property, serial mouth, input, output amortize size inputs set by process designing personnel; Spark incident means has every to close one character sparks and one alignment receives post-trigger. In component part found process lieutenant general to be able to inspect attribute to tax default value, the port that runs component part when the program opens function (ComPortOpen) when, will string together a mouth to be able to inspect attribute set value to undertake port initialization is reached by component part found surveillance to string together buccal line Cheng to return port sentence handle (HCommFile) ; The action of surveillant line Cheng is, by spark incident means surveillance strings together a mouth, with respect to Xiang Chuanghan when the know exactly about sth that string together a mouth is occupied number utters the WM_COMMNOTIFY word that defines oneself, window function gets event of OnComm of WM_COMMNOTIFY message post-trigger; Shut function when executive port (ComPortClose) when, this function shuts port and Cheng of cancel inspect line of sight. Program flow chart is a graph 1. Graph 12 application specification should execute ComPortOpen function (namely method) when, open serial mouth with CreatFile() , right now FdwShareMode, parameter must be 0, open the resource of exclusive visit. FdwCreate parameter must be designation OPEN_EXISTING mark, HTemplateFile parameter must be Nil, set communication parameter with GetCommState, establish incident target with CreateEvent() , get with AllocateHWnd() the window enumerates compose power; Use Delphi3.

The 0 object TmyCommWacth that found multi-line tool to build Cheng of a surveillant line; Event is set with ResetEVent() in surveillant line Cheng sentence handle, appoint an object to be in signal with WaitForSingleObject() or return when overtime condition, with PostMessage() to the mistake keeps clear of with ClearCommError() after appointing a window to send function of message; window to receive an information, use from decided process OnCommData(Pcha(msg.

LParam) , msg.

WParam) sparks incident OnComm, when executive port shuts function ComPortClose, with Cheng of line of sight of CloseMyComThread cancel inspect, release message window with DeallocateHWnd() sentence handle, shut with CloseHandle() incident uses RegisterComponents to undertake registered to component part with the; that string together a mouth. Did not offer considering space source program read write amortize data program, receive data to be able to be read with ReadFile() in OnComm incident actually, its file sentence the facility stringing together a mouth that the handle returns for ComPortOpen sentence handle HCommFile; Write data to be able to make up one process or function to use WriteFile() , its file sentence the handle reads a handle together, read keep data simpler. Graph what the 2 component after be being installed to compile show shown attribute and episode below Object Inspector. Graph Windows of   of Unit Comm32;interfaceuses of 23 component source program, messages, sysUtils, classes, graphics, controls, forms, dialogs;const   WMCOMMNOTIFY = WMUSER + 1;Type{defines attribute to lift TParity = (None of   of type}   with the plum, odd, even, mark, TStopBits = (1 of   of Space);   , 15, toncommMode = (evchar of   of 2);   , TComPorts=(Com1 of Evflag);     , com2, com3, toncommEvent = Procedure(Sender of   of Com4);   ECommsError = Class(Exception); : TObject;Buffer:POinter;BufferLength: Word) Of Object;{sparks incident founds surveillant line Cheng to resembling}   Type{kind PostEvent of   of   of Private of   of}   TMyCommWacth = Class(TThread) : Public HCommFile of   of Procedure Execute; Override; of   of   of Protected of   of } of Private Declarations of { of Integer;     : THandle; { strings together a mouth sentence handle } HCloseEvent: Incident of THandle; { sentence handle } HComm32Window:TWindow of Handle; { message sentence Lpoverlapped:T of   of handle }  End;type { of   of } of tectonic function of { of ConStructor Create; of OVERLAPPED;     establishs property of Private{definition of   of Tcomm32 = Class(TComponent) of   of } of member boy or girl friend demesne MyComThread of variable}   : TMyCommWacth;   BaudRates: ? ComName of  Nteger;   : TComPorts;   Parity: Stopbits of TParity;   : DataBits of TStopBits;   : Byte;   InPutbuffers: Integer;   OutPutbuffers: Integer;commMode: TOncommMode;OnCommMsg: TOnCommEvent;procedure CommWndProc(Var Msg: Procedure OnCommData(Buffer of   of Protected of   of } of Private Declarations of { of   of TMessage);message WMCOMMNOTIFY; : PChar; BufferLength: Does Public { of   of } of Protected Declarations of { of Word);     run HCommFile: of   of attribute }   ?  Handle;hCloseEvent: HComm32Window:T of THandle;    Handle;Function ComPortOpen: Thandle;Function ComPortClose: Constructor Create(Aowner:T of   of Boolean;procedure CloseMyComThread;  Published{of } of Public Declarations of { of   of   of Destructor Destroy; Override; of Component);override;     can inspect attribute and incident}property ComParity: TParity Read Parity Write Parity Default None;property ComPortName:TComPorts Read ComName Write ComName Default Com2;property BaudRate:INteger Read BaudRates Write BaudRates Default 9600;property Stopbit:TStopBits Read Stopbits Write Stopbits Default1;property ByteDataBit:BYte Read DataBits Write DataBits Default 8;property InBuffersize: Integer Read InPutbuffers Write InPutbuffers Default 1024;property OutBuffersize:INteger Read OutPutbuffers Write OutPutbuffers Default 1024;property SetComMode:TOncommMode Read CommMode Write CommMode Default EvChar;property OnComm:TOnCommEvent Read OnCommMsg Write OnCommMsg;end;procedure Register;implementationTMyCommWacth.

Cheng of Create();{inspect line of sight founds FreeOnTerminate:= of   of   of Inherited Create(False); of}begin    Cheng of line of sight of True;end; { inspect carries out } Procedure TMyCommWacth.

Execute;Var DwTransfer, dwEvtMask:INteger;begin   If Comm32.

If Not SetCommMask(hCommFile of   of   of Begin of   of SetComMode = Evchar Then, dwEvtMask:= of   of   of   of Begin of   of   of While(True) Do of   of   of EVRXCHAR) Then Exit;WaitCommEvent(hCommFile of   of 0;     , dwEvtMask, waitForSingleObject(PostEvent of   of   of   of Begin of   of =EVRXCHAR) Then of   of   of   of If((DwEvtMaskandEVRXCHAR) of   of @Lpoverlapped);     , postMessage(hComm32Window of   of   of ResetEVent(PostEvent); of 1000000);     , WMCOMMNOTIFY, hcommfile, if Not SetCommMask(hCommFile of   of   of Begin of End Else   of   of End; of   of   of End; of   of 0);     , dwEvtMask:= of   of   of   of Begin of   of   of While(True) Do of   of   of EVRXFLAG) Then Exit;WaitCommEvent(hCommFile of   of 0;     , dwEvtMask, @comm32.

WaitForSingleObject(comm32 of   of   of   of   of Begin of   of   of   of =EVRXFLAG) Then of   of   of   of If ((DwEvtMask And EVRXFLAG) of   of Lpoverlapped);     .

PostEvent, resetEVent(comm32 of   of   of 1000000);   .

PostMessage(hComm32Window of PostEvent);     , WMCOMMNOTIFY, HCommFile, { of End; End;end; of   of   of   of End; of NULL);     monitors line Cheng ends } { to build Tcomm32 of communication component } .

Create(Aowner:TMyComThread:= of   of Component);begin   Inherited Create(aOwner);Nil;   HCommFile:=0;   HCloseEvent:=0;   Parity:=None;   ComName:=Com2;   BaudRates:=9600;   Stopbits:=1;   DataBits:=8;   InPutBuffers:=1024;   OutPutBuffers:=1024;   CommMode:=Evchar;end;destructor TComm32.

Component of Inherited Destroy;end;procedure Register;{of   of DeallocateHWnd(hComm32Window); of   of   If Not(csDesigning In ComponentState)then of}begin of function of form of Destroy;{component analyse registers}begin   RegisterComponents(' Sample ' , [Tcomm32] ) ;end;procedure TComm32.

OnCommData(Buffer: PChar; BufferLength: OnCommMsg(Self of   of   of   of   of Word);begin   If Assigned(OnCommMsg) Then, buffer, port of BufferLength);end; { component opens methodological } Function TComm32.

ComPortOpen: Thandle;var DcbPort:TDCB;   ComBuff:BOOlean;   StrCom:sTring;begin   StrCom:=' Com ' + IntToStr(or(comName)+1);   HCommFile:=CreateFile(Pcha(StrCom) , GENERICREAD Or GENERICWRITE, 0, nil, OPENEXISTING, FILEATTRIBUTENORMAL Or FILEFLAGOVERLAPPED, longInt(0));   If (hCommFile <>If GetCommState(hCommFile of   of Begin of INVALIDHANDLevalUE) Then   , dcbPort of   of   of   of   of DcbPort) Then Begin.

BaudRate:=DcbPort of   of   of BaudRate;     .

ByteSize:=DcbPort of   of   of DataBits;     .

Parity:=DcbPort of   of   of Or(parity);     .

StopBits:=DcbPort of   of   of Or(Stopbit);     .

Flags:=SetCommState(hCommFile of   of   of 0;     , application of   of   of Begin of   of End Else   of   of   of End; of CbPort);     .

Messagebox(' cannot open port ' + ' install port afresh please! ' , ' Error ' ,   of   of MbOk + MbDefButton1); Result:=ComBuff:= of   of End; of   of Exit; of 0;    SetupComm(hCommFile, inBuffersize, outBuffersize);   HComm32Window:=AllocateHWnd(CommWndProc);   HCloseEvent:=CreateEvent(Nil, true, false, HCommFile:= of   of   of   of   of   of CloseHandle(HCommFile); of   of   of   of   of   of Begin of   of If HCloseEvent = 0 Then of Nil);  Raise CommsError of   of   of   of 0;     .

Create (' cannot found incident ' ) MyComThread:= of   of   of   of Try of   End;  TMyCommWacth.

MyComThread:= of   of   of   of Create();   Except  HCommFile:= of   of   of   of   of CloseHandle(HCommFile); of   of   of   of   of CloseHandle(HCloseEvent); of   of   of Nil;    Raise ECommsError of   of   of 0;     .

Create(' cannot build surveillant line Cheng ' )   End;MyComThread.



HComm32Window:=HComm32Window;PurgeComm(hCommFile, PURGETXCLEAR);PurgeComm(hCommFile, PURGERXCLEAR);MyComThread.

Resume;result:=HCommFile;end;Function TComm32.

ComPortClose: Boolean;{port shuts   of   of   of Begin of   of methodological}begin   If HCommFile = 0 Then Result:=HCommFile:= of   of CloseHandle(hCommFile); of   of CloseHandle(HCloseEvent); of   of CloseMyComThread; of   of End; of   of Exit; of   of False;    0;   Result:=}procedure TComm32 of True;end;{message window.

CommWndProc(Var Msg: TMessage);var Comstate, dwerrorcode:INteger;begin   ClearCommError(hCommfile, dwErrorCode, @ComState);   OnCommData(Pcha(msg.

LParam) , msg.

}procedure TComm32 of Cheng of line of sight of inspect of cancel of LocalFree(hcommfile);end;{of WParam);   .

CloseMyComThread;begin   If MyComThread <   > PurgeComm(HCommFile of   of   of   of   of SetEvent(HCloseEvent); of   of   of   of   of   of Begin of Nil Then   , if (WaitForSingleObject(MyComThread of   of PURGERXABORT + PURGERXCLEAR); .

Handle, myComThread of   of   of   of   of   of 10000) = WAITTIMEOUT) Then.

MyComThread of   of   of   of Terminate;     .

MyComThread:= of   of   of   of Free;    Nil   Endend;end.

CNC Milling CNC Machining