A serial port to send data format is meet two packages to send order, including 1 package is f5 0 03 fa _ _ a _ fa _ _ _ fa _ _ _... Fa _ _ _c0 00 and pack 2 is f5 0 02 b fb _ _ fb_ _ fb _ _... Fb _ _ c0 00 (fa 0 a what are two hexadecimal data)
(including f5 oa o3 and f5 ob o2 in baotou, 03 and 02 is behind the length of the data frame) c0 00 is tail, fa and fb is each frame head, how to extract each frame at the back of the line of three or two data and display in the iplot box), and end at the front of the two figures in the memo box
unit Unit1;
Interface
USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, Registry, StdCtrls, iComponent, iVCLComponent,
ICustomComponent iPlotComponent, iPlot, OleCtrls MSCommLib_TLB;
Type
TForm1=class (TForm)
ComboBox1: TComboBox;
Label1: TLabel;
Label2: TLabel;
ComboBox2: TComboBox;
MSComm1: TMSComm;
Openport: TButton;
Exit: TButton;
The clear: TButton;
Findport: TButton;
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
Memo4: TMemo;
Memo5: TMemo;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
IPlot2: TiPlot;
IPlot1: TiPlot;
Procedure MSComm1Comm (Sender: TObject);
procedure FormCreate(Sender: TObject);
Procedure findportClick (Sender: TObject);
Procedure clearClick (Sender: TObject);
Procedure ExitClick (Sender: TObject);
Procedure OpenportClick (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;
Var//define variables, global variables,
Form1: TForm1;
Comindex: array [0.. 6] of integer;
Indata: array [0.. 99900000] of the integer;
A, b, j, k, h and d: integer;
Implementation
{$R *. DFM}
Procedure TForm1. FormCreate (Sender: TObject);
The begin
Mscomm1. InBufferCount:=0;//empty the receive buffer, the current receive buffer to receive the length of the data, the count:=MSComm1. InBufferCount
Mscomm1. InputLen:=0;//Input read the entire content of the buffer once read all data
Mscomm1. OutBufferCount:=0;//to empty send buffer
A:=0;
B:=0;
J:=0;
K:=0;
H:=10;
end;
Procedure TForm1. FindportClick (Sender: TObject);
Var//by reading the registry to obtain, a serial port information is stored in the registry to this place
Reg: TRegistry;//use TRegistry object to access the information in the registry file
Ts: TStrings;//TStrings is an abstract class, in the actual development, can list of string manipulation belongs to component (such as TListBox)
I: integer;//TStrings class defines its methods, but it did not achieve these methods to manipulate strings, TStrings derived class implements these methods
The begin
Ts:=TStringList. Create;
Reg:=TRegistry. Create;//create TRegistry object
Reg. RootKey:=HKEY_LOCAL_MACHINE;//specify the root key, store the current system of hardware and software configuration information
Reg. OpenKey (' hardware \ devicemap \ serialcomm ', true);
Reg. GetValueNames (ts);//get data under the primary key value name
Memo1. Lines. The Add [I] (ts);
Memo1. Lines. The Add (reg. ReadString (ts) [I]);
For I:=1 to ts. Do the Count - 1
The begin//read serial number set serial port
ComboBox1. Items. The Add (reg. ReadString (ts) Strings [I]));//String is a basic data types, and TStrings is a class, the class of the object in each row is a String
Comindex [I] :=strtoint (reg. ReadString (ts) Strings [I]) [4]);//an character data can be converted into numeric data
Memo1. Lines. The Add (reg. ReadString (ts) Strings [I]));//the string value under each text (receive) show in Memo1
end;
Ts. Free;
Reg. CloseKey;
Reg. Free;
end;
Procedure TForm1. OpenportClick (Sender: TObject);
Var
ComSetting: String;
The begin
If not MSComm1. PortOpen then
The begin
MSComm1.Com mPort:=comindex [ComboBox1 ItemIndex + 1];
ComSetting:=ComboBox2. Text;
MSComm1. Settings:=ComSetting + ', N, 8, 1 '.
MSComm1. An InputMode:=comInputModeBinary;
MSComm1. PortOpen:=True;
End
The else begin
MSComm1. PortOpen:=False;
end;
end;
Procedure TForm1. ExitClick (Sender: TObject);
The begin
If MSComm1. PortOpen=True then
The begin
MSComm1. PortOpen:=False;
end;
Application. The Terminate;
end;
Procedure TForm1. MSComm1Comm (Sender: TObject);
Var
SerialInput: variant;
I, c, d, inputlen: Integer;
Y1, y2, y3, y4, y5, y6: Integer;
Y11, y22 y33, y44, y55 and y66, datastr1: string;
C1 and c2: double;
The begin
Inputlen:=MSComm1. InBufferCount;
SerialInput:=MSComm1. Input;
For I:=0 to do inputlen - 1
The begin
Datastr1:=datastr1 + '+ LowerCase (IntToHex (SerialInput [I], 2));
Indata [a] :=SerialInput [I];
Inc (a);
end;
For I:=b to do a - 1
The begin
If (indata [I]=$f5) and (indata [I + 1)=$0 a) and (indata [I + 2]=$3) then
The begin
For d:=I to do a - 4
The begin
If (indata [d + 3]=$fa) then
The begin
If (indata [d + 4]=$fa) then
The begin
End
The else
The begin
Y1:=indata [I + 4] * 256 * 256 + indata + 5 [d] * 256 + indata + 6 [d].
Y11:=FormatFloat (' 00.000 ', y1);
If TryStrToFloat (y11, c1) then
The begin
Iplot1. Channel [0]. AddXY (j/h, c1);
end;
Inc (j);
end;
End
The else
The begin
If (indata [d + 3]=$c0) and (indata [d + 4]=$00) then
The begin
Y2:=indata [d + 1);
Y3:=indata [d + 2];
Y22:=FormatFloat (' 00.000 ', y2);
Y33:=FormatFloat (' 00.000 ', y3);
Memo2. Lines. The Add (y22);
Memo3. Lines. The Add (y33);
end;
end;
end;
end;
If (indata [I]=$f5) and (indata [I + 1)=$0 b) and (indata [I + 2]=$02) then
The begin
For c:=I to do a - 4
The begin
If (indata + 3 [c]=$fb) then
The begin
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull