unit unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
cmdStop: TButton;
cmd_Next: TButton;
cmd_Last: TButton;
cmd_IMS: TButton;
cmd_Exit: TButton;
cmdBegin: TButton;
IMSCtr: TLabel;
txt820: TEdit;
lbl821: TLabel;
lbl822: TLabel;
lbl823: TLabel;
lbl824: TLabel;
lbl825: TLabel;
lbl826: TLabel;
lbl827: TLabel;
lbl902: TLabel;
lbl903: TLabel;
lblSTime: TLabel;
lblETime: TLabel;
timIMS: TTimer;
txtOut: TEdit;
procedure FormCreate(Sender: TObject);
procedure cmdStopClick(Sender: TObject);
procedure cmd_NextClick(Sender: TObject);
procedure cmd_LastClick(Sender: TObject);
procedure cmd_IMSClick(Sender: TObject);
procedure cmd_ExitClick(Sender: TObject);
procedure cmdBeginClick(Sender: TObject);
procedure timIMSTimer(Sender: TObject);
private
{ Private declarations }
procedure InitSystem;
procedure ViewIMS;
procedure PrcRequest;
procedure ProUnPackData;
procedure Answer;
procedure UnPackProcess;
public
{ Public declarations }
end;
type
IMSType = record
C806: Integer;
C820: Single;
C821: Single;
C822: Single;
C823: Single;
C824: Single;
C825: Single;
C826: Single;
C827: Single;
C828: TDateTime;
C840: Single;
C902: Single;
C903: Single;
C904: TDateTime;
C905: TDateTime;
C906: Single;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
const
DeviceNumber = 11;
var
IMSData: array[0..DeviceNumber] of IMSType;
intCtrPoint: Integer;
intUnPackOK: Integer;
strUnPackData: string;
strTempData: string;
strUnPackedData: string;
strSpsAddress: string;
strDevAddress: string;
SOH, STX, ETX, ACK, NAK: string;
procedure TForm1.InitSystem;
var
intPoint: Integer;
StrTemp: string;
begin
SOH := Chr($1);
STX := Chr($2);
ETX := Chr($3);
ACK := Chr($6);
NAK := Chr($15);
intCtrPoint := 0;
//格式化这句话不会翻译,代替Format(intCtrPoint + 1, "0#")
case length(IntToStr(intCtrPoint + 1)) of
0: StrTemp := '0' + IntToStr(intCtrPoint + 1);
else
StrTemp := IntToStr(intCtrPoint + 1);
end;
IMSCtr.Caption := 'IMS: ' + StrTemp;
cmdStop.Enabled := False;
for intPoint := 0 to DeviceNumber do
begin
IMSData[intPoint].C806 := 0;
IMSData[intPoint].C820 := 1000;
IMSData[intPoint].C821 := 0;
IMSData[intPoint].C822 := 0;
IMSData[intPoint].C823 := 0;
IMSData[intPoint].C824 := 0;
IMSData[intPoint].C825 := 25.54;
IMSData[intPoint].C826 := 154.38;
IMSData[intPoint].C827 := 19.56;
IMSData[intPoint].C828 := Now;
IMSData[intPoint].C840 := 0;
IMSData[intPoint].C902 := 0;
IMSData[intPoint].C903 := 0;
IMSData[intPoint].C904 := Now;
IMSData[intPoint].C905 := Now;
IMSData[intPoint].C906 := 0;
end;
txt820.Text := FloatToStr(IMSData[intCtrPoint].C820);
ViewIMS;
comIMS.InputLen := 0;
comIMS.PortOpen := True;
intUnPackOK := 0;
end;
procedure TForm1.ViewIMS;
begin
{这块不翻译了,专门的Format格式忘记了,让我都使用循环太累了,
lbl821.Caption := Format(IMSData[intCtrPoint].C821, '######.#0 ');
lbl822.Caption := Format(IMSData[intCtrPoint].C822, '######.#0 ');
lbl823.Caption := Format(IMSData[intCtrPoint].C823, '######.#0 ');
lbl824.Caption := Format(IMSData[intCtrPoint].C824, '######.#0 ');
lbl825.Caption := Format(IMSData[intCtrPoint].C825, '######.#0 ');
lbl826.Caption := Format(IMSData[intCtrPoint].C826, '######.#0 ');
lbl827.Caption := Format(IMSData[intCtrPoint].C827, '######.#0 ');
lbl902.Caption := Format(IMSData[intCtrPoint].C902, '######.#0 ');
lbl903.Caption := Format(IMSData[intCtrPoint).C903, '######.#0 ');
lblSTime.Caption := Format(IMSData[intCtrPoint].C905, 'hh: mm: ss');
lblETime.Caption := Format(IMSData[intCtrPoint].C904, 'hh: mm: ss');}
end;
procedure TForm1.cmdBeginClick(Sender: TObject);
begin
timIMS.Enabled := True;
cmdBegin.Enabled := False;
cmdStop.Enabled := True;
end;
procedure TForm1.cmd_ExitClick(Sender: TObject);
begin
if comIMS.PortOpen = True then
comIMS.PortOpen := False;
Close;
end;
procedure TForm1.cmd_IMSClick(Sender: TObject);
begin
if IMSData[intCtrPoint].C806 = 0 then
begin
try
IMSData[intCtrPoint].C820 := StrToFloat(txt820.Text);
except
end;
IMSData[intCtrPoint].C821 := IMSData[intCtrPoint].C820;
IMSData[intCtrPoint].C806 := 99;
IMSData[intCtrPoint].C905 := Now;
ViewIMS;
end;
end;
procedure TForm1.cmd_LastClick(Sender: TObject);
begin
try
if IMSData[intCtrPoint].C806 = 0 then
IMSData[intCtrPoint].C820 := StrToFloat(txt820.Text);
except
end;
intCtrPoint := intCtrPoint - 1;
if intCtrPoint < 0 then intCtrPoint := DeviceNumber;
//IMSCtr.Caption := 'IMS : ' + Format(intCtrPoint + 1, "0#");不翻译了
txt820.Text := FloatToStr(IMSData[intCtrPoint].C820);
ViewIMS;
end;
procedure TForm1.cmd_NextClick(Sender: TObject);
begin
try
if IMSData[intCtrPoint].C806 = 0 then
IMSData[intCtrPoint].C820 := StrToFloat(txt820.Text);
except
end;
intCtrPoint := intCtrPoint + 1;
if intCtrPoint > DeviceNumber then intCtrPoint := 0;
//IMSCtr.Caption := 'IMS : ' + Format(intCtrPoint + 1, '0#');不翻译了
txt820.Text := FloatToStr(IMSData[intCtrPoint].C820);
ViewIMS;
end;
procedure TForm1.cmdStopClick(Sender: TObject);
begin
timIMS.Enabled := False;
cmdBegin.Enabled := True;
cmdStop.Enabled := False;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
InitSystem;
end;
procedure TForm1.timIMSTimer(Sender: TObject);
var
intPoint: Integer;
begin
for intPoint := 0 to DeviceNumber do
begin
if IMSData[intPoint].C806 > 0 then
begin
IMSData[intPoint].C821 := IMSData[intPoint].C821 - Random * 10; //Random范围?
if IMSData[intPoint].C821 <= 0 then
begin
IMSData[intPoint].C902 := IMSData[intPoint].C820;
IMSData[intPoint].C903 := IMSData[intPoint].C820 * 1.00024;
IMSData[intPoint].C820 := 0;
IMSData[intPoint].C822 := 0;
IMSData[intPoint].C823 := 0;
IMSData[intPoint].C824 := 0;
if intPoint = intCtrPoint then txt820.Text := '0';
IMSData[intPoint].C904 := Now;
IMSData[intPoint].C821 := 0;
IMSData[intPoint].C806 := 0;
end
else
begin
IMSData[intPoint].C822 := IMSData[intPoint].C820 - IMSData[intPoint].C821;
IMSData[intPoint].C823 := IMSData[intPoint].C822 * 1.00024;
IMSData[intPoint].C824 := Random * 10; //范围?
end;
end;
IMSData[intPoint].C825 := IMSData[intPoint].C825 + Random - 0.5;
IMSData[intPoint].C826 := IMSData[intPoint].C826 + Random - 0.5;
IMSData[intPoint].C827 := IMSData[intPoint].C827 + Random - 0.5;
end;
ViewIMS;
PrcRequest;
end;
procedure TForm1.PrcRequest;
begin
if comIMS.InBufferCount > 0 then
begin
strUnPackData := comIMS.Input;
ProUnPackData;
if intUnPackOK = 99 then
begin
intUnPackOK := 0;
Answer;
end
else if intUnPackOK < 0 then
intUnPackOK := 0;
end;
end;
{ ================================================
功能: 获取数据效验值
================================================}
function strCheckSum(strData: string): string;
var
intTemp, intSum: Integer;
begin
intSum := 0;
for intTemp := 1 to Length(strData) do
intSum := intSum + ord(Copy(strData, intTemp, 1)[1]);
// 原文strCheckSum := Hex((intSum / $10) Mod $10)& Hex(intSum Mod $10);
strCheckSum := IntToHex(((intSum div $10) mod $10), 1) + IntToHex((intSum mod $10), 1); //可能有问题,取余和取整在VB和Delphi里面弄混了
end;
{ ================================================
功能: 处理卸载协议数据
================================================}
procedure TForm1.ProUnPackData;
var
intEndPoint: Integer;
begin
intEndPoint := 0;
case intUnPackOK of
0:
begin
if Copy(strUnPackData, 1, 1) = SOH then
begin
// intEndPoint := InStr(1, strUnPackData, ETX);//不知道对应函数
if (intEndPoint < 1) then
begin
strTempData := strUnPackData;
intUnPackOK := 1;
end
else
begin
{If 代替了Case,因为Delphi的Case不支持变量}
if Length(strUnPackData) = intEndPoint then
begin
strTempData := strUnPackData;
intUnPackOK := 2;
end
else if Length(strUnPackData) = intEndPoint + 1 then
begin
strTempData := strUnPackData;
intUnPackOK := 3;
end
else if Length(strUnPackData) = intEndPoint + 2 then
begin
strTempData := strUnPackData;
UnPackProcess;
end
else
//数据错误
intUnPackOK := -1;
end;
end
else
//数据错误
intUnPackOK := -1
end;
1:
begin
// intEndPoint := InStr(1, strUnPackData, ETX);不知道对应函数
{If代替了case循环}
if (intEndPoint < 1) then
strTempData := strTempData + strUnPackData
else if Length(strUnPackData) = intEndPoint then
begin
strTempData := strTempData + strUnPackData;
intUnPackOK := 2;
end
else if Length(strUnPackData) = intEndPoint + 1 then
begin
strTempData := strTempData + strUnPackData;
intUnPackOK := 3;
end
else if Length(strUnPackData) = intEndPoint + 2 then
begin
strTempData := strTempData + strUnPackData;
UnPackProcess;
end
else
//数据错误
intUnPackOK := -1
end;
2:
begin
case Length(strUnPackData) of
1:
begin
strTempData := strTempData + strUnPackData;
intUnPackOK := 3;
end;
2:
begin
strTempData := strTempData + strUnPackData;
UnPackProcess;
end;
else
intUnPackOK := -1;
end;
end;
end;
end;
{ ================================================
功能: 从一个数据包中卸载协议
================================================}
procedure TForm1.UnPackProcess;
var
Tmp_Str: string;
begin
intUnPackOK := -1;
if Copy(strTempData, length(strTempData) - 2, 2) =
strCheckSum(Copy(strTempData, 1, length(strTempData) - 2)) then
if Copy(strTempData, 8, 1) = STX then
begin
strDevAddress := copy(strTempData, 2, 3);
strSpsAddress := copy(strTempData, 5, 3);
strUnPackedData := copy(strTempData, 9, Length(strTempData) - 11);
intUnPackOK := 99;
end;
end;
procedure TForm1.Answer;
var
strTemp: string;
intTemp: Integer;
intDataBegin: Integer;
intDataEnd: Integer;
intCmdType: Integer;
begin
if Copy(strUnPackedData, 1, 1) = 'R' then
begin
intDataBegin := 2;
strTemp := SOH + strSpsAddress + strDevAddress + STX;
repeat
// intDataEnd := InStr(intDataBegin, strUnPackedData, "; ")不知道如何翻译
if intDataEnd > 0 then
begin
// intCmdType = CInt(Copy(strUnPackedData, intDataBegin, intDataEnd - intDataBegin));//不知道如何翻译
intDataBegin := intDataEnd + 1;
end
else
begin
// intCmdType := CInt(Copy(strUnPackedData, length(...),Length(strUnPackedData) - intDataBegin + 1));
end;
if intCmdType = 806 then
begin
// strTemp := strTemp + '806=' + Format(IMSData(CInt(strDevAddress - 1)).C806, '#0');
end;
if intCmdType = 820 then
begin
// strTemp := strTemp + '820 = ' + Format(IMSData[CInt(strDevAddress - 1)].C820, "0#####");
end;
if intCmdType = 821 then
begin
// strTemp := strTemp + '821=' + Format(IMSData[CInt(strDevAddress - 1)].C821, "0#####.#0 ")
end;
if intCmdType = 822 then
begin
// strTemp := strTemp + '822=' + Format(IMSData[CInt(strDevAddress - 1)].C822, "0#####.#0 ")
end;
if intCmdType = 823 then
begin
// strTemp := strTemp + '823=' + Format(IMSData[CInt(strDevAddress - 1)].C823, "0#####.#0 ")
end;
if intCmdType = 824 then
begin
// strTemp := strTemp + '824=' + Format(IMSData[CInt(strDevAddress - 1)].C824, "0####")
end;
if intCmdType = 825 then
begin
// strTemp := strTemp + '825=' + Format(IMSData[CInt(strDevAddress - 1)].C825, "0##.#0 ")
end;
if intCmdType = 826 then
begin
// strTemp := strTemp + '826=' + Format(IMSData[CInt(strDevAddress - 1)].C826, "0###.#0 ")
end;
if intCmdType = 827 then
begin
// strTemp := strTemp + '827=' + Format(IMSData[CInt(strDevAddress - 1)].C827, "0###.# ")
end;
if intCmdType = 828 then
begin
// strTemp := strTemp + '828=' + Format(IMSData[CInt(strDevAddress - 1)].C828, "hh: mm: ss")
end;
if intCmdType = 840 then
begin
// strTemp := strTemp + '840=' + Format(IMSData[CInt(strDevAddress - 1)].C840, '0#######');
end;
if intCmdType = 902 then
begin
// strTemp := strTemp + '902=' + Format(IMSData[CInt(strDevAddress - 1)].C902, '0#####.#0');
end;
if intCmdType = 903 then
begin
// strTemp := strTemp + '903=' + Format(IMSData[CInt(strDevAddress - 1)].C903, '0#####.#0');
end;
if intCmdType = 904 then
begin
// strTemp := strTemp + '904=' + Format(IMSData[CInt(strDevAddress - 1)].C904, 'hh:mm');
end;
if intCmdType = 905 then
begin
// strTemp := strTemp + '905=' + Format(IMSData[CInt(strDevAddress - 1)].C905, 'hh:mm');
end;
if intCmdType = 906 then
begin
// strTemp := strTemp + '906=' + Format(IMSData[CInt(strDevAddress - 1)].C906, '0#######');
end;
if intDataEnd > 0 then
strTemp := strTemp + ';'
else
strTemp := strTemp + ETX;
until (intDataEnd < 0);
strTemp := strTemp + strCheckSum(strTemp);
comIMS.Output := strTemp;
txtOut.Text := '';
for intTemp := 1 to Length(strTemp) do
txtOut.Text := txtOut.Text + IntToHex(ord(Copy(strTemp, intTemp, 1)[1]), 1) + ';';
end;
end;
end.