B
blackcow
Unregistered / Unconfirmed
GUEST, unregistred user!
请大家帮我看下我编的程序那有问题:这个程序基本上可以运行但有问题,测试密度不起作
用,选择测试密度时,下位机没有反应,还有在用spcomm控件接收监控实时数据时,在一帧数据
中丢掉了一些位,是不是和spcomm控件有关系呢,还是有其它原因,请高手指教.
:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm, Menus, ComCtrls, ExtCtrls, DB, DBTables,
Grids, DBGrids;
type
TForm1 = class(TForm)
Comm1: TComm;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
Image1: TImage;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
TabSheet5: TTabSheet;
TabSheet6: TTabSheet;
StatusBar1: TStatusBar;
MainMenu1: TMainMenu;
estMenu1: TMenuItem;
ReSetStation1: TMenuItem;
ConnectionServer1: TMenuItem;
GetDensity1: TMenuItem;
H1: TMenuItem;
AnimationA1: TMenuItem;
Panel1: TPanel;
Image2: TImage;
Label2: TLabel;
Image3: TImage;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Database1: TDatabase;
Table1: TTable;
Memo1: TMemo;
Timer1: TTimer;
Image4: TImage;
Image5: TImage;
Image6: TImage;
Image7: TImage;
DBGrid2: TDBGrid;
Image8: TImage;
DataSource2: TDataSource;
Query1: TQuery;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure ReSetStation1Click(Sender: TObject);
procedure ConnectionServer1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
procedure GetDensity1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
receData:array [1..1024] of byte;
receString:string;
iConnStatus:integer;
{$R *.dfm}
function ConvertHexChar(ch:char):byte;
begin
if (ch>='0') and (ch<='9') then
ConvertHexChar:=ord(ch)-ord('0')
else if (ch>='a') and (ch<='f') then
ConvertHexChar:=ord(ch)-ord('a')+10
else if (ch>='A') and (ch<='F') then
ConvertHexChar:=ord(ch)-ord('A')+10;
end;
procedure StringHex(str:string; var result:byte);
begin
result:=ConvertHexChar(str[1])*16+ConvertHexChar(str[2]);
end;
procedure SendData(acceptString:string);
var
sedata:byte;
begin
StringHex(acceptString,sedata);
sleep(500);
if not Form1.Comm1.WriteCommData(@sedata,1) then
ShowMessage('数据发送失败!');
end;
function ConnectSrv():boolean;
begin
SendData('FE');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Comm1.StartComm;
PageControl1.ActivePage:=TabSheet1;
Label2.Caption:=FormatDateTime('hh:mm:ss',now);
ConnectSrv();
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
receString:='';
move(buffer^,pchar(@receData)^,bufferlength);
for i:=1 to bufferlength do
receString:=receString+inttohex(receData,1);
Memo1.Lines.Add(receString);
//if AnsiStrLIComp(pchar(receString),'FA',2)=0 then
if receString='FA' then
begin
if (length(receString)=4) and (receString[3]='F') and (receString[4]='E') then
begin
iConnStatus:=4;
Label1.Caption:=Label1.Caption+' 服务器连接成功';
end;
SendData('FF');
sleep(500);
end;
if AnsiStrLIComp(pchar(receString),'FFFA',4)=0 then
begin
iConnStatus:=1;
Label1.Caption:=' 服务器连接成功,无历史数据!';
SendData('FD');
sleep(500);
end
else if receString='FFFB' then
begin
iConnStatus:=2;
Label1.Caption:=' 服务器连接成功,未上移历史数据!';
end
else if receString='FFFC' then
begin
iConnStatus:=3;
Label1.Caption:=' 服务器连接成功,历史数据上移!';
end;
end;
procedure TForm1.ReSetStation1Click(Sender: TObject);
begin
SendData('E0');
end;
procedure TForm1.ConnectionServer1Click(Sender: TObject);
begin
ConnectSrv();
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Comm1.StopComm;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label2.Caption:=FormatDateTime('hh:mm:ss',now);
end;
procedure TForm1.GetDensity1Click(Sender: TObject);//测试密度
begin
SendData('C0');
end;
end.
用,选择测试密度时,下位机没有反应,还有在用spcomm控件接收监控实时数据时,在一帧数据
中丢掉了一些位,是不是和spcomm控件有关系呢,还是有其它原因,请高手指教.
:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm, Menus, ComCtrls, ExtCtrls, DB, DBTables,
Grids, DBGrids;
type
TForm1 = class(TForm)
Comm1: TComm;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
Image1: TImage;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
TabSheet5: TTabSheet;
TabSheet6: TTabSheet;
StatusBar1: TStatusBar;
MainMenu1: TMainMenu;
estMenu1: TMenuItem;
ReSetStation1: TMenuItem;
ConnectionServer1: TMenuItem;
GetDensity1: TMenuItem;
H1: TMenuItem;
AnimationA1: TMenuItem;
Panel1: TPanel;
Image2: TImage;
Label2: TLabel;
Image3: TImage;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Database1: TDatabase;
Table1: TTable;
Memo1: TMemo;
Timer1: TTimer;
Image4: TImage;
Image5: TImage;
Image6: TImage;
Image7: TImage;
DBGrid2: TDBGrid;
Image8: TImage;
DataSource2: TDataSource;
Query1: TQuery;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure ReSetStation1Click(Sender: TObject);
procedure ConnectionServer1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
procedure GetDensity1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
receData:array [1..1024] of byte;
receString:string;
iConnStatus:integer;
{$R *.dfm}
function ConvertHexChar(ch:char):byte;
begin
if (ch>='0') and (ch<='9') then
ConvertHexChar:=ord(ch)-ord('0')
else if (ch>='a') and (ch<='f') then
ConvertHexChar:=ord(ch)-ord('a')+10
else if (ch>='A') and (ch<='F') then
ConvertHexChar:=ord(ch)-ord('A')+10;
end;
procedure StringHex(str:string; var result:byte);
begin
result:=ConvertHexChar(str[1])*16+ConvertHexChar(str[2]);
end;
procedure SendData(acceptString:string);
var
sedata:byte;
begin
StringHex(acceptString,sedata);
sleep(500);
if not Form1.Comm1.WriteCommData(@sedata,1) then
ShowMessage('数据发送失败!');
end;
function ConnectSrv():boolean;
begin
SendData('FE');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Comm1.StartComm;
PageControl1.ActivePage:=TabSheet1;
Label2.Caption:=FormatDateTime('hh:mm:ss',now);
ConnectSrv();
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
receString:='';
move(buffer^,pchar(@receData)^,bufferlength);
for i:=1 to bufferlength do
receString:=receString+inttohex(receData,1);
Memo1.Lines.Add(receString);
//if AnsiStrLIComp(pchar(receString),'FA',2)=0 then
if receString='FA' then
begin
if (length(receString)=4) and (receString[3]='F') and (receString[4]='E') then
begin
iConnStatus:=4;
Label1.Caption:=Label1.Caption+' 服务器连接成功';
end;
SendData('FF');
sleep(500);
end;
if AnsiStrLIComp(pchar(receString),'FFFA',4)=0 then
begin
iConnStatus:=1;
Label1.Caption:=' 服务器连接成功,无历史数据!';
SendData('FD');
sleep(500);
end
else if receString='FFFB' then
begin
iConnStatus:=2;
Label1.Caption:=' 服务器连接成功,未上移历史数据!';
end
else if receString='FFFC' then
begin
iConnStatus:=3;
Label1.Caption:=' 服务器连接成功,历史数据上移!';
end;
end;
procedure TForm1.ReSetStation1Click(Sender: TObject);
begin
SendData('E0');
end;
procedure TForm1.ConnectionServer1Click(Sender: TObject);
begin
ConnectSrv();
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Comm1.StopComm;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label2.Caption:=FormatDateTime('hh:mm:ss',now);
end;
procedure TForm1.GetDensity1Click(Sender: TObject);//测试密度
begin
SendData('C0');
end;
end.