请大家再次帮我分析一下我编的delphi串口通信程序的问题 (100分)

  • 主题发起人 主题发起人 blackcow
  • 开始时间 开始时间
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.
 
无话可说
 
你自己都分析好了,难道还不能解决?
 
怎么?想找人写代码?给我工资,我帮你写,反正现在闲着没事! [:)][8D]
 
这个和我要做的差不多,只不过楼主测油位,我是测水位。。
如果有人做给我一份也!!谢谢
代码:
GGSCW@SOHU.COM
最好再加一条:程序运行后屏蔽掉其他的系统功能:)
 
我可以帮你做,不过得给钱,[8D][8D][8D]
 
想偷懒的人真多

大家都有事,网上串口的例子也很多

估计光给分,没人为你单独做,虽然是很简单的程序
 
我可以帮你,不过。。。。
 
测试密度不起作用,怎么个不起作用法
请先把问题阐述清楚
另外,stringhex可以用
function stringhex(str:string):integer;
begin
result:=strtoint('$'+str);
end;
 
我想您求教一个问题:我在用delphi编写串口程序时,使用的是Pcomm链接库,但在接受对方给我发的16进制字节时,受到的是一些奇怪的字符。比如,对方发51 01 7d ff,我收到的却是'Q'#1'}'?,请问我怎样才能将这些字符转化为我需要的16进制字节呢?请告诉我,谢谢!
 
后退
顶部