?? unit1.~pas
字號:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, TeEngine, Series, ExtCtrls, TeeProcs, Chart,
OleCtrls, MSCommLib_TLB;
type
TForm1 = class(TForm)
tbshtSignalChart: TPageControl;
tbshtDebug: TTabSheet;
rchdtSerialIn: TRichEdit;
cmbbxSerialIn: TComboBox;
Label7: TLabel;
btRecStart: TButton;
btRecStop: TButton;
cmbbxBaudIn: TComboBox;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
btRecSave: TButton;
cmbbxTypeIn: TComboBox;
cmbbxSerialOut: TComboBox;
Label13: TLabel;
cmbbxBaudOut: TComboBox;
rchdtSerialOut: TRichEdit;
btSendOut: TButton;
dtCode1: TEdit;
lbCode1: TLabel;
lbCode2: TLabel;
dtCode3: TEdit;
cmbbxTypeOut: TComboBox;
Label16: TLabel;
Label51: TLabel;
tmrSerialIn: TTimer;
MSCommOut: TMSComm;
MSCommIn: TMSComm;
dtCode2: TEdit;
dtCode4: TEdit;
lbCode3: TLabel;
lbCode4: TLabel;
ImgTrace: TImage;
Label17: TLabel;
Label18: TLabel;
Label19: TLabel;
Label20: TLabel;
Label21: TLabel;
Label22: TLabel;
Label23: TLabel;
Label24: TLabel;
Label25: TLabel;
Label26: TLabel;
lbShowAd0: TLabel;
lbShowAd1: TLabel;
lbShowAd4: TLabel;
lbShowAd5: TLabel;
lbShowAd2: TLabel;
lbShowGps5: TLabel;
lbShowGps1: TLabel;
lbShowAd7: TLabel;
lbShowAd6: TLabel;
lbShowAd3: TLabel;
Label37: TLabel;
lbShowGps6: TLabel;
lbShowGps4: TLabel;
Label53: TLabel;
Label54: TLabel;
Label55: TLabel;
lbShowGps0: TLabel;
btSaveTrace: TButton;
btLoadTrace: TButton;
btSaveData: TButton;
Label1: TLabel;
lbShowModel: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label14: TLabel;
Label15: TLabel;
Label39: TLabel;
Label40: TLabel;
lbShowPara0: TLabel;
lbShowPara1: TLabel;
lbShowPara2: TLabel;
lbShowPara3: TLabel;
lbShowPara4: TLabel;
lbShowPara5: TLabel;
lbShowPara6: TLabel;
lbShowPara7: TLabel;
Label49: TLabel;
lbShowGps2: TLabel;
Label57: TLabel;
lbShowGps3: TLabel;
Label59: TLabel;
cmbbxModel: TComboBox;
btSetModel: TButton;
cmbbxParaNo: TComboBox;
Label38: TLabel;
Label60: TLabel;
Label61: TLabel;
dtParaValue: TEdit;
btADInit: TButton;
btSetPara: TButton;
Label62: TLabel;
rdgrpDataCode: TRadioGroup;
dlgSaveData: TSaveDialog;
dlgLoadTrace: TOpenDialog;
dlgSaveTrace: TSaveDialog;
rchdtTmp: TRichEdit;
Label2: TLabel;
TabSheet1: TTabSheet;
chrtAcc: TChart;
chrtGyro: TChart;
srsGyroY: TFastLineSeries;
srsGyroX: TFastLineSeries;
srsGyroZ: TFastLineSeries;
srsAcc1: TFastLineSeries;
srsAcc2: TFastLineSeries;
chrtHeight: TChart;
srsHeight: TFastLineSeries;
Label12: TLabel;
btCharClear: TButton;
tbshtSignal: TTabSheet;
procedure FormCreate(Sender: TObject);
procedure btRecStartClick(Sender: TObject);
procedure btRecStopClick(Sender: TObject);
procedure btRecSaveClick(Sender: TObject);
procedure btSendOutClick(Sender: TObject);
procedure tmrSerialInTimer(Sender: TObject);
procedure btSetModelClick(Sender: TObject);
procedure btSetParaClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btSaveTraceClick(Sender: TObject);
procedure btLoadTraceClick(Sender: TObject);
procedure btADInitClick(Sender: TObject);
procedure btSaveDataClick(Sender: TObject);
procedure btCharClearClick(Sender: TObject);
procedure dtCode1Change(Sender: TObject);
procedure dtCode2Change(Sender: TObject);
procedure dtCode3Change(Sender: TObject);
procedure dtCode4Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
InitADDataFlag:Boolean;
end;
/////////////////////////////////////////////////////////////////
{
串口接受線程
}
TSerialInThread =class(TThread)
private
SerialInput:Variant; //保存該線程從串口獲取得到的數(shù)據(jù)矩陣
Seriallength : integer; //該線程需要處理的數(shù)據(jù)的個數(shù)
SerialStr:String; //用于保存處理后數(shù)據(jù)的字符串變量
protected
procedure Execute; override; //需要重載
procedure GetData; //從串口獲取數(shù)據(jù)的過程
procedure TransToStr; //轉(zhuǎn)換為適合顯示的字符串的過程
procedure PostData ; //顯示在用戶界面的過程
end;
/////////////////////////////////////////////////////////////////
{
串口發(fā)送線程
}
TSerialOutThread =class(TThread)
private
DataOrCode:Boolean; //是否要求輸出指令,F(xiàn)alse表明輸出數(shù)據(jù)
SerialOutput:String; //發(fā)送數(shù)據(jù)時的數(shù)據(jù)矩陣
Seriallength : integer; //發(fā)送數(shù)據(jù)時,數(shù)據(jù)長度
tmpVar:Variant; //發(fā)送指令時的數(shù)據(jù)矩陣
CheckOk:Boolean; //用戶確定的數(shù)據(jù)或是指令是否有錯誤
protected
procedure Execute; override; //需要重載
procedure GetData; //從用戶界面獲取需要輸出的數(shù)據(jù)或是指令代碼
procedure PackData; //根據(jù)協(xié)議打包數(shù)據(jù)
procedure PackCode; //根據(jù)協(xié)議打包指令
procedure PostData; //發(fā)送給串口控件,實現(xiàn)數(shù)據(jù)的發(fā)送
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
GpsDataCountMax : integer = 20; //GPS數(shù)據(jù)包中數(shù)據(jù)的個數(shù)
ParaDataCountMax: integer = 9; //參數(shù)數(shù)據(jù)包中數(shù)據(jù)的個數(shù)
ADDataCountMax:integer =16; //傳感器數(shù)據(jù)包中數(shù)據(jù)的個數(shù)
var
{
實現(xiàn)線程互斥的句柄。
}
hMutex:THandle =0; //串口接受數(shù)據(jù)的線程使用的互斥句柄
hMutex2:THandle =0; //串口發(fā)送數(shù)據(jù)的線程使用的互斥句柄
{
用于指令上下傳的全局變量
}
ReceiveCharFlag:Boolean; //是否以字符接受串口,F(xiàn)alse表示以數(shù)據(jù)接受
ParaDataFlag:Boolean; //正在截取參數(shù)數(shù)據(jù)包的標志
GPSDataFlag:Boolean; //正在截取GPS數(shù)據(jù)包的標志
ADDataFlag:Boolean; //正在截取傳感器數(shù)據(jù)包的標志
ParaNewDataFlag:Boolean; //截取完參數(shù)的標志
GPSNewDataFlag:Boolean; //截取完GPS數(shù)據(jù)包的標志
ADNewDataFlag:Boolean; //截取完傳感器數(shù)據(jù)包的標志
ParaDataCount:Integer; //截取參數(shù)數(shù)據(jù)包的計數(shù)器
ADDataCount:Integer; //截取傳感器數(shù)據(jù)包的計數(shù)器
GpsDataCount:Integer; //截取GPS數(shù)據(jù)包的計數(shù)器
DownStr:String; //用于保存串口下傳的所有數(shù)據(jù)
GpsStr:String; //保存GPS數(shù)據(jù)包的數(shù)據(jù)
ADStr:String; //保存?zhèn)鞲衅鲾?shù)據(jù)包的數(shù)據(jù)
ParaDataArray:array[0..8] of Byte; //接受參數(shù)數(shù)據(jù)包的數(shù)組
GpsDataArray:array[0..19] of Byte; //接受GPS數(shù)據(jù)包的數(shù)組
ADDataArray:array[0..15] of Byte; //接受傳感器數(shù)據(jù)包的數(shù)組
ADDataDblArray:array[0..7] of double; //傳感器實際的采樣值
ADDataArrayMean:array[0..13] of Byte; //保存?zhèn)鞲衅黛o態(tài)均值的數(shù)組。
//不考慮系統(tǒng)電壓。
ParaStrArray:array[0..3] of String; //顯示參數(shù)數(shù)據(jù)的字符串數(shù)組
GpsStrArray:array[0..6] of String; //顯示GPS數(shù)據(jù)的字符串數(shù)組
ADStrArray:array[0..7] of String; //顯示傳感器的字符串數(shù)組
{
用于指令上下傳的全局變量
}
CodeOutStr:array[1..4] of String; //當前上傳的指令代碼
CodeInStr:array[0..3] of String; //下傳返回的指令代碼
{
用于繪圖的全局變量
}
gpsMeta: TMetaFile; //用于保存軌跡圖像的對象
posXArray,posYArray: array of double; //保存當前飛行軌跡點的數(shù)組
posXArrayLoad,posYArrayLoad: array of double; //保存載入的軌跡點的數(shù)組
posLength: Integer; //當前飛行軌跡點的總數(shù)
posLengthLoad: Integer; //當前飛行軌跡點的總數(shù)
{
函數(shù)hex()是將0~f字符轉(zhuǎn)換為0~15的整數(shù)。
}
function hex(c:String):Integer ;
var
x:integer;
begin
if (c='0') or (c=' ') then
x:=0
else if c='1' then
x:=1
else if c = '2' then
x:=2
else if c='3' then
x:=3
else if c='4' then
x:=4
else if c='5' then
x:=5
else if c='6' then
x:=6
else if c='7' then
x:=7
else if c='8' then
x:=8
else if c='9' then
x:=9
else if ((c='a') or (c='A')) then
x:=10
else if (c='b') or (c='B') then
x:=11
else if (c='c') or (c='C') then
x:=12
else if (c='d') or (c='D') then
x:=13
else if (c='e') or (c='E') then
x:=14
else if (c='f') or (c='F') then
x:=15
else
x:=-1;
Result:=x;
end;
function HexToInt(S:String): Integer;
var
tmpInt1,tmpInt2:Integer ;
begin
if Length(S)=1 then
begin
Result:=hex(S[1]);
end
else if Length(S)=2 then
begin
tmpInt1:=hex(S[1]);
tmpInt2:=hex(S[2]);
if (tmpInt1=-1) or (tmpInt2=-1) then
Result:=-1
else
Result:= tmpInt1*16+tmpInt2;
end
else
Result:=-1;
end;
function GetMaxValue(x,y,x1,y1: Array of Double;len,len1: Integer): double;
var
i: Integer;
begin
result := 0;
for i:=0 to len-1 do
begin
if abs(x[i])>result then
result := abs(x[i]);
if abs(y[i]) > result then
result := abs(y[i]);
end;
for i:=0 to len1-1 do
begin
if abs(x1[i])>result then
result := abs(x1[i]);
if abs(y1[i]) > result then
result := abs(y1[i]);
end;
end;
/////////////////////////////////////////////////////////////////
{ 繪制飛行軌跡的函數(shù)
紅色軌跡是載入的,
藍色的軌跡是當前軌跡點
1. posXArray,posYArray 是當前軌跡點的坐標,
posLength是當前軌跡點的個數(shù)
2. posXArrayLoad,posYArrayLoad 是載入的軌跡點的坐標,
posLengthLoad是載入的軌跡點的個數(shù)
}
procedure DrawAxis;
var
i,x,y,wid,h: Integer;
pstep,ratio,maxV : double;
can: TMetaFileCanvas;
begin
can := TMetaFileCanvas.Create(gpsMeta,0); //建立一個畫布對象
gpsMeta.Width := Form1.ImgTrace.Width; //確定畫布對應圖像的大小
gpsMeta.Height := Form1.ImgTrace.Height;
wid := gpsMeta.Width;
h := gpsMeta.Height;
{
確定直角坐標的最大值,取10,100,200,500,1000,2000,5000,10000,50000,100000 的最小值
缺省狀態(tài)取 100
}
maxV := GetMaxValue(posXArray,posYArray,posXArrayLoad,posYArrayLoad,posLength,posLengthLoad);
if maxV = 0 then
maxV := 100;
if maxV <= 10 then
begin
maxV := 10;
end
else if maxV <= 100 then
begin
maxV := 100;
end
else if maxV <= 200 then
maxV := 200
else if maxV<= 500 then
maxV := 500
else if maxV <= 1000 then
begin
maxV := 1000;
end
else if maxV<= 2000 then
maxV := 2000
else if maxV<= 5000 then
maxV := 5000
else if maxV <= 10000 then
begin
maxV := 10000;
end
else if maxV<= 50000 then
maxV := 50000
else if maxV <=100000 then
begin
maxV := 100000;
end
else
begin
maxV := 1000000;
end;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -