{本程序?yàn)閟7-200 cpu224自由口通訊實(shí)驗(yàn),由上位機(jī)發(fā)送,PLC返回查詢輸入輸出狀態(tài)
1.01 xx FF;若為$01開(kāi)頭則視為只查詢輸入輸出狀態(tài)
2.02 xx FF;若為$02開(kāi)頭則視為查詢輸入輸出狀態(tài),同時(shí)寫(xiě)QB0
3.03 xx FF;若為$03開(kāi)頭則視為查詢輸入輸出狀態(tài),同時(shí)寫(xiě)QB1
其中FF為自由口結(jié)束字符
DATE:2013-11
Auther:hcwp}
unit unit1;
var
Form1: TForm1;
//Comm1:TComm;
InputArray:array[0..3]of Byte;
IB0,IB1,QB0,QB1:array[0..7]of Boolean;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
with Comm1 do
begin
BaudRate:= 9600;//波特率9600bps
Parity:=None;//奇偶交驗(yàn)
ByteSize:=_8;//數(shù)據(jù)位 8
StopBits:=_1;//停止位1
Comm1.CommPort:=3;
InBufferSize:=5;
InputLen:=5;
ParityCheck:=False;
StopBits:=_1;
DtrControl:=dtrEnable;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Comm1.StartComm;
Timer3.Enabled:=True;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: PAnsiChar;
BufferLength: Word);
var
aa: Integer;
ss: Integer;
dd:string;
begin
//aa:=12;
for ss:=0 to 4 do
begin
aa:=Byte(Buffer[ss]);
//sleep(100);
dd:=IntToHex(aa,2);
begin
{ for k := 1 to 14 do
begin
j[k]:='JvLED'+ IntToStr(k);
end; }
for iFlag:=7 downto 0 do
begin
if InputArray[0] and (1 shl iFlag)<>0 then
IB0[iFlag]:=True
else
IB0[iFlag]:=False;
end;
for i := 0 to 7 do
begin
if IB0=True then
j[I]:=True
else j[I]:=False;
end;
for iFlag:=7 downto 0 do
begin
if InputArray[1] and (1 shl iFlag)<>0 then
IB1[iFlag]:=True
else
IB1[iFlag]:=False;
end;
for i := 0 to 7 do
begin
if IB1=True then
j[I+8]:=True
else j[I+8]:=False;
end;
for k :=0 to 15 do
begin
case k of
0: JvLED0.Status:=j[0] ;
1: JvLED1.Status:=j[1] ;
2: JvLED2.Status:=j[2] ;
3: JvLED3.Status:=j[3] ;
4: JvLED4.Status:=j[4] ;
5: JvLED5.Status:=j[5] ;
6: JvLED6.Status:=j[6] ;
7: JvLED7.Status:=j[7] ;
8: JvLED8.Status:=j[8] ;
9: JvLED9.Status:=j[9] ;
10: JvLED10.Status:=j[10] ;
11: JvLED11.Status:=j[11] ;
12: JvLED12.Status:=j[12] ;
13: JvLED13.Status:=j[13] ;
14: JvLED14.Status:=j[14] ;
15: JvLED15.Status:=j[15] ;
end;
end;
end;
procedure TForm1.Timer2Timer(Sender: TObject);
var
iFlag:Integer;
k,I:Integer;
j:array[0..15]of Boolean;
begin
{ for k := 1 to 14 do
begin
j[k]:='JvLED'+ IntToStr(k);
end; }
for iFlag:=7 downto 0 do
begin
if InputArray[2] and (1 shl iFlag)<>0 then
QB0[iFlag]:=True
else
QB0[iFlag]:=False;
end;
for i := 0 to 7 do
begin
if QB0=True then
j[I]:=True
else j[I]:=False;
end;
for iFlag:=7 downto 0 do
begin
if InputArray[3] and (1 shl iFlag)<>0 then
QB1[iFlag]:=True
else
QB1[iFlag]:=False;
end;
for i := 0 to 7 do
begin
if QB1=True then
j[I+8]:=True
else j[I+8]:=False;
end;
for k :=0 to 15 do
begin
case k of
0: JvLED16.Status:=j[0] ;
1: JvLED17.Status:=j[1] ;
2: JvLED18.Status:=j[2] ;
3: JvLED19.Status:=j[3] ;
4: JvLED20.Status:=j[4] ;
5: JvLED21.Status:=j[5] ;
6: JvLED22.Status:=j[6] ;
7: JvLED23.Status:=j[7] ;
8: JvLED24.Status:=j[8] ;
9: JvLED25.Status:=j[9] ;
10: JvLED26.Status:=j[10] ;
11: JvLED27.Status:=j[11] ;
12: JvLED28.Status:=j[12] ;
13: JvLED29.Status:=j[13] ;
14: JvLED30.Status:=j[14] ;
15: JvLED31.Status:=j[15] ;
end;
end;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[2] and ($01);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$01: if CheckBox1.State= cbchecked then //選中狀態(tài)
begin
str:=Char($02)+char( InputArray[2])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[2] and ($fe); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($02)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox1.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[2] or ($01);
str:=Char($02)+char( temp1)+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.Timer3Timer(Sender: TObject);
var
str:string;
begin
str:=char($01)+ Char($00)+Chr($ff);
Comm1.Output:=str;
end;
procedure TForm1.CheckBox2Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[2] and ($02);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$02: if CheckBox2.State= cbchecked then //選中狀態(tài)
begin
str:=Char($02)+char( InputArray[2])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[2] and ($fd); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($02)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox2.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[2] or ($02);
str:=Char($02)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox3Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[2] and ($04);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$04: if CheckBox3.State= cbchecked then //選中狀態(tài)
begin
str:=Char($02)+char( InputArray[2])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[2] and ($fb); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($02)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox3.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[2] or ($04);
str:=Char($02)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox4Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[2] and ($08);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$08: if CheckBox4.State= cbchecked then //選中狀態(tài)
begin
str:=Char($02)+char( InputArray[2])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[2] and ($f7); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($02)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox4.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[2] or ($08);
str:=Char($02)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox5Click(Sender: TObject); //Q0.4
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[2] and ($10);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$10: if CheckBox5.State= cbchecked then //選中狀態(tài)
begin
str:=Char($02)+char( InputArray[2])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[2] and ($ef); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($02)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox5.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[2] or ($10);
str:=Char($02)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox6Click(Sender: TObject); //Q0.5
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[2] and ($20);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$20: if CheckBox6.State= cbchecked then //選中狀態(tài)
begin
str:=Char($02)+char( InputArray[2])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[2] and ($df); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($02)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox6.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[2] or ($20);
str:=Char($02)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox7Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[2] and ($40);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$40: if CheckBox7.State= cbchecked then //選中狀態(tài)
begin
str:=Char($02)+char( InputArray[2])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[2] and ($bf); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($02)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox7.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[2] or ($40);
str:=Char($02)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox8Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[2] and ($80);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$80: if CheckBox8.State= cbchecked then //選中狀態(tài)
begin
str:=Char($02)+char( InputArray[2])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[2] and ($7f); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($02)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox8.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[2] or ($80);
str:=Char($02)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox9Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[3] and ($01);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$01: if CheckBox9.State= cbchecked then //選中狀態(tài)
begin
str:=Char($03)+char( InputArray[3])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[3] and ($fe); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($03)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox9.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[3] or ($01);
str:=Char($03)+char( temp1)+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox10Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[3] and ($02);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$02: if CheckBox10.State= cbchecked then //選中狀態(tài)
begin
str:=Char($03)+char( InputArray[3])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[3] and ($fd); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($03)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox10.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[3] or ($02);
str:=Char($03)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox11Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[3] and ($04);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$04: if CheckBox11.State= cbchecked then //選中狀態(tài)
begin
str:=Char($03)+char( InputArray[3])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[3] and ($fb); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($03)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox11.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[3] or ($04);
str:=Char($03)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox12Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[3] and ($08);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$08: if CheckBox12.State= cbchecked then //選中狀態(tài)
begin
str:=Char($03)+char( InputArray[3])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[3] and ($f7); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($03)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox12.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[3] or ($08);
str:=Char($03)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox13Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[3] and ($10);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$10: if CheckBox13.State= cbchecked then //選中狀態(tài)
begin
str:=Char($03)+char( InputArray[3])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[3] and ($ef); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($03)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox13.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[3] or ($10);
str:=Char($03)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox14Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[3] and ($20);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$20: if CheckBox14.State= cbchecked then //選中狀態(tài)
begin
str:=Char($03)+char( InputArray[3])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[3] and ($df); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($03)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox14.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[3] or ($20);
str:=Char($03)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox15Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[3] and ($40);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$40: if CheckBox15.State= cbchecked then //選中狀態(tài)
begin
str:=Char($03)+char( InputArray[3])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[3] and ($bf); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($03)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox15.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[3] or ($40);
str:=Char($03)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
procedure TForm1.CheckBox16Click(Sender: TObject);
var
str:string;
temp,temp1:Byte;
begin
Timer3.Enabled:=False;
temp:= InputArray[3] and ($80);
case temp of
{判斷分兩種情況原先為此位為1,后再分選中和非選中兩種狀態(tài)的處理}
$80: if CheckBox16.State= cbchecked then //選中狀態(tài)
begin
str:=Char($03)+char( InputArray[3])+ Char($ff); //保持原狀態(tài)
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin
temp1:= InputArray[3] and ($7f); //與1111 1110進(jìn)行與運(yùn)算 ,使此位為0
str:=Char($03)+char(temp1)+ Char($ff);
Comm1.Output:=str;
end;
{判斷分兩種情況原先為此位為0,后再分選中和非選中兩種狀態(tài)的處理}
$00: if CheckBox16.State= cbchecked then //選中狀態(tài)
begin
temp1:= Inputarray[3] or ($80);
str:=Char($03)+char( temp1)+ Char($ff); //
Comm1.Output:=str;
end
else //未選中狀態(tài)
begin