?? unit2.pas
字號:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, SPComm;
type
TForm2 = class(TForm)
Label1: TLabel;
ComboBox2: TComboBox;
ComboBox3: TComboBox;
ComboBox4: TComboBox;
ComboBox5: TComboBox;
ComboBox6: TComboBox;
ComboBox7: TComboBox;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Comm1: TComm;
ComboBox1: TComboBox;
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses Unit1, Unit3,MATH;
{$R *.dfm}
procedure TForm2.BitBtn2Click(Sender: TObject);
var BaudRate :integer;
begin
Comm1.CommName:=ComboBox1.Text; //1
ComboBox2.ItemIndex := 0; //2
Comm1.ByteSize := TByteSize(ComboBox3.ItemIndex); //3
Comm1.BaudRate := StrToInt(ComboBox4.Text); //4
Comm1.StopBits := TStopBits(ComboBox5.ItemIndex); //5
ComboBox6.ItemIndex := 0; //6
Comm1.Parity := None;
if TryStrToInt(ComboBox4.Text,BaudRate) then
Comm1.BaudRate := BaudRate;
// Comm1.ByteSize := TByteSize(ComboBox3.ItemIndex);
// Comm1.StopBits := TStopBits(ComboBox5.ItemIndex);
// Comm1.Parity := TParity(ComboBox4.ItemIndex);
// ComboBox1.ItemIndex := 0;
// Comm1.CommName := ComboBox1.Text;
// ComboBox6.ItemIndex := 2;
//.BaudRate := StrToInt(ComboBox6.Text);
// ComboBox4.ItemIndex := 0;
// Comm1.Parity := None;
// ComboBox3.ItemIndex := 0;
//Comm1.ByteSize := _8;
//ComboBox5.ItemIndex := 0;
// Comm1.StopBits := _1;
if not TryStrToInt(ComboBox4.Text,BaudRate) then
begin
Application.MessageBox('波特率設(shè)定有誤'+#13+
' 請重新輸入','警告',MB_ICONWARNING or MB_OK);
// ComboBox6.SetFocus;
exit;
end;
Comm1.StartComm;
ComHaveSet:=55;
close;
end;
procedure TForm2.BitBtn1Click(Sender: TObject);
begin
ComboBox1.ItemIndex := 0;
Comm1.CommName := ComboBox1.Text;
ComboBox2.ItemIndex := 0;
ComboBox3.ItemIndex := 3;
Comm1.ByteSize := _8;
ComboBox4.ItemIndex := 1;
// Comm1.BaudRate := 9600;
ComboBox5.ItemIndex := 0;
Comm1.StopBits := _1;
ComboBox6.ItemIndex := 0;
Comm1.Parity := None;
ComboBox7.ItemIndex := 0;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
ComboBox1.ItemIndex := 0;
Comm1.CommName := ComboBox1.Text;
ComboBox2.ItemIndex := 0;
ComboBox3.ItemIndex := 3;
Comm1.ByteSize := _8;
ComboBox4.ItemIndex := 1;
// Comm1.BaudRate := 9600;
ComboBox5.ItemIndex := 0;
Comm1.StopBits := _1;
ComboBox6.ItemIndex := 0;
Comm1.Parity := None;
ComboBox7.ItemIndex := 0;
end;
function StrToHexStr(const S:string):string;
//字符串轉(zhuǎn)換成16進制字符串
var
I:Integer;
begin
for I:=1 to Length(S) do
begin
if I=1 then
Result:=IntToHex(Ord(S[1]),2)
else Result:=Result+' '+IntToHex(Ord(S[I]),2);
end;
end;
// #define CoefficientV 0.000404 /*2V電池電壓的系數(shù)*********************/
//#define COEFFICIENT_6V 0.001221 /*6V電池電壓的系數(shù)*********************/
//#define COEFFICIENT_12V 0.002394 /*12V電池電壓的系數(shù)********************/
//#define COEFFICIENT_T 0.045754 /*溫度的系數(shù)***************************/
Const COEFFICIENT_2V=0.000404;
Const COEFFICIENT_6V = 0.001221;
Const COEFFICIENT_12V = 0.002394;
Const COEFFICIENT_T = 0.045754 ;
procedure TForm2.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
type
IArr = ^integer;
var
aa: array [1..2048] of byte;
// liao: array [1..500] of byte;
//i, n, nFileNum: integer;
i: integer;
//OpenMode:Word;
// buf: Pointer;
// bFlag: Boolean;
// aFile: TFileStream;
CommrevStr: shortstring;
liaoxumingtest :integer;
DianChiData:double;
DianChiDataXiao:double;
CoefficientV:double;
viewString: string;
begin
try
viewString := '';
CoefficientV:=0;
// Memo1.Lines.Clear;
Move(IArr(buffer)^, aa, bufferLength);
viewString := '接收: ';
for I := 1 to BufferLength do
begin
CommrevStr[i] := Char(aa[i]);
// liao[i]:= Char(aa[i]);
viewstring := viewstring + ' '+ IntToHex(aa[i], 2)+'';
end;
// Form1.Memo1.Lines.Add('Recv Length:' + IntToStr(BufferLength));
// Form1.Label2.Caption:= IntToHex(aa[1],);
// Form1.Label2.Caption:= IntToHex(aa[1], 2)+IntToHex(aa[3], 2);
//將整數(shù)轉(zhuǎn)換為十六進制字符串, IntToHex(int Value, int Digits)
// liaoxumingtest:= aa[1] *256 +1 ;// StrToInt( IntToHex(aa[1], 2));
// Form1.Label3.Caption:= IntToStr(aa[1] *256 +1);
if JiaoYangJianChe=55 then
begin
JiaoYangJianChe:=0;
liaoxumingtest:= aa[10] *256 +aa[9];
Form1.Label2.Caption:= IntToStr(liaoxumingtest);
if (liaoxumingtest>386)or (liaoxumingtest<176) then
begin
Form1.Label2.Font.Color:=clRed;
Application.MessageBox('不得了 不得了'+#13+
' 出軌了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
end
else
begin
Form1.Label2.Font.Color:=clWindowText;
end;
liaoxumingtest:= aa[12] *256 +aa[11];
Form1.Label3.Caption:= IntToStr(liaoxumingtest);
if (liaoxumingtest>3918)or (liaoxumingtest<3780) then
begin
Form1.Label3.Font.Color:=clRed;
Application.MessageBox('不得了 不得了'+#13+
' 出軌了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
end
else
begin
Form1.Label3.Font.Color:=clWindowText;
end;
liaoxumingtest:= aa[14] *256 +aa[13];
Form1.Label4.Caption:= IntToStr(liaoxumingtest);
if (liaoxumingtest>306)or (liaoxumingtest<96) then
begin
Form1.Label4.Font.Color:=clRed;
Application.MessageBox('不得了 不得了'+#13+
' 出軌了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
end
else
begin
Form1.Label4.Font.Color:=clWindowText;
end;
liaoxumingtest:= aa[16] *256 +aa[15];
Form1.Label5.Caption:= IntToStr(liaoxumingtest);
if (liaoxumingtest>3999)or (liaoxumingtest<3789) then
begin
Form1.Label5.Font.Color:=clRed;
Application.MessageBox('不得了 不得了'+#13+
' 出軌了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
end
else
begin
Form1.Label5.Font.Color:=clWindowText;
end;
liaoxumingtest:= aa[18] *256 +aa[17];
Form1.Label6.Caption:= IntToStr(liaoxumingtest);
if (liaoxumingtest>324)or (liaoxumingtest<104) then
begin
Form1.Label6.Font.Color:=clRed;
Application.MessageBox('不得了 不得了'+#13+
' 出軌了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
end
else
begin
Form1.Label6.Font.Color:=clWindowText;
end;
liaoxumingtest:= aa[20] *256 +aa[19];
Form1.Label7.Caption:= IntToStr(liaoxumingtest);
if (liaoxumingtest>3900)or (liaoxumingtest<3790) then
begin
Form1.Label7.Font.Color:=clRed;
Application.MessageBox('不得了 不得了'+#13+
' 出軌了,快找一下原因','警告',MB_ICONWARNING or MB_OK);
end
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -