1
110538
Unregistered / Unconfirmed
GUEST, unregistred user!
最近用MSCOMM控件编写一个PC机与PLC通讯的程序,这之间有个计算PLC控制电位的过程,但是程序在运行时总是跳过这一不,无法实现想要的功能,哪位高手可以解决,一定高分送上,程序的一段原代码如下:
unit againcomm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, MSCommLib_TLB, StdCtrls;
type
TForm1 = class(TForm)
MSComm1: TMSComm;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure MSComm1Comm(Sender: TObject);
procedure Edit5Change(Sender: TObject);
private
{ Private declarations }
NowPosition:integer;
pReadMW20To24:array [0..7]of byte;
pReadMW20To24Var:variant;
procedure ReadMW20To24(mw: Integer);
function caculatepos():integer;
public
a:integer;
caculatepos:Integer;
ReceiveData:array [0..6] of byte;
{ Public declarations }
end;
function CRC16(bData: array of Byte
bDataLen: Integer): Word
stdcall;
external 'CRC.dll' Name 'CRC16';
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
MSComm1.CommPort:=1;//串口初始化
MSComm1.Settings :='9600,n,8,2';
MSComm1.RThreshold:=1;
MSComm1.InputMode:=1;
MSComm1.PortOpen:= false
//comm口设置
MSComm1.InBufferCount:=0;
MSComm1.InputLen:=0;
MSComm1.OutBufferCount:=0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MSComm1.PortOpen:=true;
MSComm1.DTREnable:=true;
MSComm1.RTSEnable:=true;
Application.ProcessMessages;
ReadMW20To24(20);
Application.ProcessMessages;
//a:=caculatepos();
Edit2.Text:=IntToStr(a);
end;
procedure TForm1.ReadMW20To24(mw: Integer);
var CountCheck:Integer;
begin
pReadMW20To24[0]:=$01;
pReadMW20To24[1]:=$03;
pReadMW20To24[2]:=$00;
pReadMW20To24[3]:=mw;
pReadMW20To24[4]:=$00;
pReadMW20To24[5]:=$01;
pReadMW20To24[6]:=CRC16(pReadMW20To24,6)div 256;
pReadMW20To24[7]:=CRC16(pReadMW20To24,6)mod 256;
pReadMW20To24Var:=VarArrayCreate([0,7],varByte);
for CountCheck:=0 to 7 do
pReadMW20To24Var[CountCheck]:=pReadMW20To24[CountCheck];
MSComm1.InBufferCount:=0;
MSComm1.InputLen:=0;
MSComm1.OutBufferCount:=0;
MSComm1.Output:=pReadMW20To24Var;
end;
procedure TForm1.MSComm1Comm(Sender: TObject);
var
lengeh:integer;
ReceiveDataVar:Variant;
a:Integer;
//*DataVar:Variant;
j:integer;
begin
if MSComm1.CommEvent=ComEvReceive then
begin
//length:=MSComm1.InBufferCount
//if length=7 then begin //读PLC得到的回复,回来7个数字
ReceiveDataVar:=VarArrayCreate([0,6],varByte);
ReceiveDataVar:= MSComm1.input;
MSComm1.InBufferCount:=0;
for j:=0 to 6 do
ReceiveData[j]:=ReceiveDataVar[j];
a:=ReceiveData[3]*256+ReceiveData[4]
//Edit1.Text:=IntToStr(a);
//end;// end if 是读数据
end;
end;
function TForm1.caculatepos(data:array of byte): integer;
begin
//data:=ReceiveData;
caculatepos:=ReceiveData[3]*256+ReceiveData[4];
end;
end.
unit againcomm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, MSCommLib_TLB, StdCtrls;
type
TForm1 = class(TForm)
MSComm1: TMSComm;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure MSComm1Comm(Sender: TObject);
procedure Edit5Change(Sender: TObject);
private
{ Private declarations }
NowPosition:integer;
pReadMW20To24:array [0..7]of byte;
pReadMW20To24Var:variant;
procedure ReadMW20To24(mw: Integer);
function caculatepos():integer;
public
a:integer;
caculatepos:Integer;
ReceiveData:array [0..6] of byte;
{ Public declarations }
end;
function CRC16(bData: array of Byte
bDataLen: Integer): Word
stdcall;
external 'CRC.dll' Name 'CRC16';
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
MSComm1.CommPort:=1;//串口初始化
MSComm1.Settings :='9600,n,8,2';
MSComm1.RThreshold:=1;
MSComm1.InputMode:=1;
MSComm1.PortOpen:= false
//comm口设置
MSComm1.InBufferCount:=0;
MSComm1.InputLen:=0;
MSComm1.OutBufferCount:=0;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MSComm1.PortOpen:=true;
MSComm1.DTREnable:=true;
MSComm1.RTSEnable:=true;
Application.ProcessMessages;
ReadMW20To24(20);
Application.ProcessMessages;
//a:=caculatepos();
Edit2.Text:=IntToStr(a);
end;
procedure TForm1.ReadMW20To24(mw: Integer);
var CountCheck:Integer;
begin
pReadMW20To24[0]:=$01;
pReadMW20To24[1]:=$03;
pReadMW20To24[2]:=$00;
pReadMW20To24[3]:=mw;
pReadMW20To24[4]:=$00;
pReadMW20To24[5]:=$01;
pReadMW20To24[6]:=CRC16(pReadMW20To24,6)div 256;
pReadMW20To24[7]:=CRC16(pReadMW20To24,6)mod 256;
pReadMW20To24Var:=VarArrayCreate([0,7],varByte);
for CountCheck:=0 to 7 do
pReadMW20To24Var[CountCheck]:=pReadMW20To24[CountCheck];
MSComm1.InBufferCount:=0;
MSComm1.InputLen:=0;
MSComm1.OutBufferCount:=0;
MSComm1.Output:=pReadMW20To24Var;
end;
procedure TForm1.MSComm1Comm(Sender: TObject);
var
lengeh:integer;
ReceiveDataVar:Variant;
a:Integer;
//*DataVar:Variant;
j:integer;
begin
if MSComm1.CommEvent=ComEvReceive then
begin
//length:=MSComm1.InBufferCount
//if length=7 then begin //读PLC得到的回复,回来7个数字
ReceiveDataVar:=VarArrayCreate([0,6],varByte);
ReceiveDataVar:= MSComm1.input;
MSComm1.InBufferCount:=0;
for j:=0 to 6 do
ReceiveData[j]:=ReceiveDataVar[j];
a:=ReceiveData[3]*256+ReceiveData[4]
//Edit1.Text:=IntToStr(a);
//end;// end if 是读数据
end;
end;
function TForm1.caculatepos(data:array of byte): integer;
begin
//data:=ReceiveData;
caculatepos:=ReceiveData[3]*256+ReceiveData[4];
end;
end.