程序代码如下:procedure TForm1.Timer1Timer(Sender: TObject);
begin
if socketconnected=false then
begin
clientsocket1.Close;
clientsocket1.Open;
end
else
upsend();
end;
procedure TForm1.SendktData( );
var senddata:array[0..7]of byte;
begin
senddata[0]:=$45;
senddata[1]:=$01;
begin
comm1.WriteCommData(@senddata,2);
if not comm1.WriteCommData(@senddata,2) then
sleep(1);
end;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: word);
var
readdata:array[1..4] of byte;
begin
move(buffer^,readdata,bufferlength);
temperature[0]:=readdata[2];
temperature[1]:=readdata[3];
temperature[2]:=$00;
temperature[3]:=$00;
end;procedure TForm1.Comm1ReceiveEror(Sender: TObject; EventMask: Cardinal);
begin
if eventMask=CE_OVERRUN THEN
error1:=true;
end;procedure tform1.Wb_sendCmd();
var sendstr:array[0..5] of byte;
begin
sendstr[0]:=byte($7E);
sendstr[1]:=byte($09);
sendstr[2]:=byte($F7);
sendstr[3]:=byte($50);
sendstr[4]:=byte($B0);
sendstr[5]:=byte($0D);
if comm2connected=true then
begin
comm2.WriteCommData(@sendstr,6);
if not comm2.WriteCommData(@sendstr,6) then
sleep(1);
end;
end;procedure tform1.Wb_sendAck(Wb_Command:byte);
var j,i,sendcount:integer;
var Ack:array[0..6] of byte;
var sendcd:array[0..49] of byte;
var addr:byte;
var senddata:string;
begin
addr:=$09;
ack[0]:=$7e;
ack[1]:=addr;
ack[2]:=$ff-addr+1;
ack[3]:=$51;
ack[4]:=$07;//(wb_command) and ($70);
ack[5]:=$a8;
ack[6]:=$0d;
j:=0;
for i:=0 to 6 do
begin
if (i>0) and (i<6) then
begin
if (ack[i]=$0d)or(ack[i]=$05) then
begin
sendcd[j]:=$05;
sendcd[j+1]:=ack[i]-$05;
inc(j);
end
else
begin
sendcd[j]:=ack[i];
inc(j);
end;
end
else
begin
sendcd[j]:=ack[i];
inc(j);
end;
end;
sendcount:=j;
for i:=0 to j-1 do
begin
senddata:=senddata+inttohex(sendcd[i],2)+'';
end;
if not comm2.WriteCommData(@sendcd,7) then //11.23 (@sendcd[i],1)
sleep(10); SLEEP(100);
comm2.StopComm;
comm2connected:=false;
end;procedure TForm1.Comm2ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var j,i,k:integer;
var readdata:array[0..99] of byte;
var getdata:array[0..99] of byte;
var viewstring:string;
begin
viewstring:='';
for j:=1 to 99 do
readdata[j]:=$00;
for j:=1 to 99 do
getdata[j]:=$00;
move(buffer^,readdata,bufferlength);
for i:=0 to bufferlength-1 do
viewstring:=viewstring+ inttohex(readdata[i],2)+'';
if readdata[bufferlength-1]=$0d then
begin
j:=0;
k:=200;
for i:=0 to 99 do
begin
if k<>i then
begin
if readdata[i]=$05 then
begin
getdata[j]:=readdata[i]+readdata[i+1];
k:=i+1;
end
else
getdata[j]:=readdata[i];
inc(j);
if readdata[i]=$0d then
break;
end;
end;
voltage[0]:=getdata[16];
voltage[1]:=getdata[17];
voltage[2]:=getdata[18];
voltage[3]:=getdata[19];
voltage[4]:=getdata[20];
voltage[5]:=getdata[21];
sleep(100);
Wb_sendAck(getdata [4]);
end;
end;procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
var inidata:array[0..25] of byte;
begin
inidata[0]:=$ff;
inidata[1]:=$ff;
inidata[2]:=$ff;
inidata[3]:=$ff;
inidata[4]:=$ff;
inidata[5]:=$02;
inidata[6]:=$10;
inidata[7]:=$50;
inidata[8]:=$00;
inidata[9]:=$68;
inidata[10]:=$39;
inidata[11]:=$39;
inidata[12]:=$39;
inidata[13]:=$39;
inidata[14]:=$39;
inidata[15]:=$39;
inidata[16]:=$68;
inidata[17]:=$81;
inidata[18]:=$21;
inidata[19]:=$90;
inidata[20]:=$01;
inidata[21]:=$06;
inidata[22]:=$81;
inidata[23]:=$b5;
inidata[24]:=$a7;
inidata[25]:=$03;
socketconnected:=true;
ClientSocket1.Socket.SendBuf(inidata,26);
end;//上传这里始终有问题,有的时候数据上传不上去,要不就是只有一个数据(本来应该有2个数据,温度和电压),大家帮我修改一下,谢谢,还有我想showmessage上传的数据,语句怎么写啊
procedure tform1.upsend();
var aircondata:Myarray;
var airlength:integer;
var voltagedata:Myarray;
var vollength:integer;
var i:integer;
var avoltage,bvoltage,cvoltage,MaxVol:real;
begin
aircondata[0]:=temperature[0]; // $00;
aircondata[1]:=temperature[1]; // $01;
aircondata[2]:=temperature[2]; // $27;
aircondata[3]:=temperature[3]; // $0D;
for i:=4 to 50 do
begin
aircondata[i]:=$00;
end; airlength:=packagekt(@aircondata);
if socketconnected=True then
clientsocket1.Socket.SendBuf(aircondata,airlength); Avoltage:=(voltage[0]+voltage[1]*256)/10000*220;
Bvoltage:=(voltage[2]+voltage[3]*256)/10000*220;
Cvoltage:=(voltage[4]+voltage[5]*256)/10000*220; MaxVol:=0.0;
if Avoltage>MaxVol then MaxVol:=Avoltage;
if Bvoltage>MaxVol then MaxVol:=Bvoltage;
if Cvoltage>MaxVol then MaxVol:=Cvoltage; if MaxVol=Avoltage then
begin
voltagedata[0]:=ceil(voltage[0]/10000.0*220.0*1000/60);
voltagedata[1]:=ceil(voltage[1]/10000.0*220.0*1000/60);
end
else if MaxVol=Bvoltage then
begin
voltagedata[0]:=ceil(voltage[2]/10000.0*220.0*1000/60);
voltagedata[1]:=ceil(voltage[3]/10000.0*220.0*1000/60);
end
else if MaxVol=Cvoltage then
begin
voltagedata[0]:=ceil(voltage[4]/10000.0*220.0*1000/60);
voltagedata[1]:=ceil(voltage[5]/10000.0*220.0*1000/60);
end;
//这里是判读电压传感器数据的地方,这里这样写合理吗?这里判断没有读到传感器数据应该如何写啊,谢谢。
if MaxVol=null then
begin
voltagedata[0]:=$00;
voltagedata[1]:=$00;
end
else if MaxVol<10 then
begin
voltagedata[0]:=$00;
voltagedata[1]:=$00;
end else if MaxVol<100 then
begin
voltagedata[0]:=$52;
voltagedata[1]:=$0E;
end
else if MaxVol>300 then
begin
voltagedata[0]:=$52;
voltagedata[1]:=$0E;
end; for i:=2 to 50 do
begin
voltagedata[i]:=$00;
end; vollength:=packagevol(@voltagedata);
if socketconnected=True then
clientsocket1.Socket.SendBuf(voltagedata,vollength);
end;procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
socketconnected:=false;
end;end.现在的问题就是我注释那个地方,数据有时候能正常上传,有时候不能。如果我想判断我的传感器不能读到数据应该怎么判读?如果其他地方有问题大家再帮我改改,谢谢,这个问题困扰我好几天了,不得其解,新人,忘大家指导。
begin
if socketconnected=false then
begin
clientsocket1.Close;
clientsocket1.Open;
end
else
upsend();
end;
procedure TForm1.SendktData( );
var senddata:array[0..7]of byte;
begin
senddata[0]:=$45;
senddata[1]:=$01;
begin
comm1.WriteCommData(@senddata,2);
if not comm1.WriteCommData(@senddata,2) then
sleep(1);
end;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: word);
var
readdata:array[1..4] of byte;
begin
move(buffer^,readdata,bufferlength);
temperature[0]:=readdata[2];
temperature[1]:=readdata[3];
temperature[2]:=$00;
temperature[3]:=$00;
end;procedure TForm1.Comm1ReceiveEror(Sender: TObject; EventMask: Cardinal);
begin
if eventMask=CE_OVERRUN THEN
error1:=true;
end;procedure tform1.Wb_sendCmd();
var sendstr:array[0..5] of byte;
begin
sendstr[0]:=byte($7E);
sendstr[1]:=byte($09);
sendstr[2]:=byte($F7);
sendstr[3]:=byte($50);
sendstr[4]:=byte($B0);
sendstr[5]:=byte($0D);
if comm2connected=true then
begin
comm2.WriteCommData(@sendstr,6);
if not comm2.WriteCommData(@sendstr,6) then
sleep(1);
end;
end;procedure tform1.Wb_sendAck(Wb_Command:byte);
var j,i,sendcount:integer;
var Ack:array[0..6] of byte;
var sendcd:array[0..49] of byte;
var addr:byte;
var senddata:string;
begin
addr:=$09;
ack[0]:=$7e;
ack[1]:=addr;
ack[2]:=$ff-addr+1;
ack[3]:=$51;
ack[4]:=$07;//(wb_command) and ($70);
ack[5]:=$a8;
ack[6]:=$0d;
j:=0;
for i:=0 to 6 do
begin
if (i>0) and (i<6) then
begin
if (ack[i]=$0d)or(ack[i]=$05) then
begin
sendcd[j]:=$05;
sendcd[j+1]:=ack[i]-$05;
inc(j);
end
else
begin
sendcd[j]:=ack[i];
inc(j);
end;
end
else
begin
sendcd[j]:=ack[i];
inc(j);
end;
end;
sendcount:=j;
for i:=0 to j-1 do
begin
senddata:=senddata+inttohex(sendcd[i],2)+'';
end;
if not comm2.WriteCommData(@sendcd,7) then //11.23 (@sendcd[i],1)
sleep(10); SLEEP(100);
comm2.StopComm;
comm2connected:=false;
end;procedure TForm1.Comm2ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var j,i,k:integer;
var readdata:array[0..99] of byte;
var getdata:array[0..99] of byte;
var viewstring:string;
begin
viewstring:='';
for j:=1 to 99 do
readdata[j]:=$00;
for j:=1 to 99 do
getdata[j]:=$00;
move(buffer^,readdata,bufferlength);
for i:=0 to bufferlength-1 do
viewstring:=viewstring+ inttohex(readdata[i],2)+'';
if readdata[bufferlength-1]=$0d then
begin
j:=0;
k:=200;
for i:=0 to 99 do
begin
if k<>i then
begin
if readdata[i]=$05 then
begin
getdata[j]:=readdata[i]+readdata[i+1];
k:=i+1;
end
else
getdata[j]:=readdata[i];
inc(j);
if readdata[i]=$0d then
break;
end;
end;
voltage[0]:=getdata[16];
voltage[1]:=getdata[17];
voltage[2]:=getdata[18];
voltage[3]:=getdata[19];
voltage[4]:=getdata[20];
voltage[5]:=getdata[21];
sleep(100);
Wb_sendAck(getdata [4]);
end;
end;procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
var inidata:array[0..25] of byte;
begin
inidata[0]:=$ff;
inidata[1]:=$ff;
inidata[2]:=$ff;
inidata[3]:=$ff;
inidata[4]:=$ff;
inidata[5]:=$02;
inidata[6]:=$10;
inidata[7]:=$50;
inidata[8]:=$00;
inidata[9]:=$68;
inidata[10]:=$39;
inidata[11]:=$39;
inidata[12]:=$39;
inidata[13]:=$39;
inidata[14]:=$39;
inidata[15]:=$39;
inidata[16]:=$68;
inidata[17]:=$81;
inidata[18]:=$21;
inidata[19]:=$90;
inidata[20]:=$01;
inidata[21]:=$06;
inidata[22]:=$81;
inidata[23]:=$b5;
inidata[24]:=$a7;
inidata[25]:=$03;
socketconnected:=true;
ClientSocket1.Socket.SendBuf(inidata,26);
end;//上传这里始终有问题,有的时候数据上传不上去,要不就是只有一个数据(本来应该有2个数据,温度和电压),大家帮我修改一下,谢谢,还有我想showmessage上传的数据,语句怎么写啊
procedure tform1.upsend();
var aircondata:Myarray;
var airlength:integer;
var voltagedata:Myarray;
var vollength:integer;
var i:integer;
var avoltage,bvoltage,cvoltage,MaxVol:real;
begin
aircondata[0]:=temperature[0]; // $00;
aircondata[1]:=temperature[1]; // $01;
aircondata[2]:=temperature[2]; // $27;
aircondata[3]:=temperature[3]; // $0D;
for i:=4 to 50 do
begin
aircondata[i]:=$00;
end; airlength:=packagekt(@aircondata);
if socketconnected=True then
clientsocket1.Socket.SendBuf(aircondata,airlength); Avoltage:=(voltage[0]+voltage[1]*256)/10000*220;
Bvoltage:=(voltage[2]+voltage[3]*256)/10000*220;
Cvoltage:=(voltage[4]+voltage[5]*256)/10000*220; MaxVol:=0.0;
if Avoltage>MaxVol then MaxVol:=Avoltage;
if Bvoltage>MaxVol then MaxVol:=Bvoltage;
if Cvoltage>MaxVol then MaxVol:=Cvoltage; if MaxVol=Avoltage then
begin
voltagedata[0]:=ceil(voltage[0]/10000.0*220.0*1000/60);
voltagedata[1]:=ceil(voltage[1]/10000.0*220.0*1000/60);
end
else if MaxVol=Bvoltage then
begin
voltagedata[0]:=ceil(voltage[2]/10000.0*220.0*1000/60);
voltagedata[1]:=ceil(voltage[3]/10000.0*220.0*1000/60);
end
else if MaxVol=Cvoltage then
begin
voltagedata[0]:=ceil(voltage[4]/10000.0*220.0*1000/60);
voltagedata[1]:=ceil(voltage[5]/10000.0*220.0*1000/60);
end;
//这里是判读电压传感器数据的地方,这里这样写合理吗?这里判断没有读到传感器数据应该如何写啊,谢谢。
if MaxVol=null then
begin
voltagedata[0]:=$00;
voltagedata[1]:=$00;
end
else if MaxVol<10 then
begin
voltagedata[0]:=$00;
voltagedata[1]:=$00;
end else if MaxVol<100 then
begin
voltagedata[0]:=$52;
voltagedata[1]:=$0E;
end
else if MaxVol>300 then
begin
voltagedata[0]:=$52;
voltagedata[1]:=$0E;
end; for i:=2 to 50 do
begin
voltagedata[i]:=$00;
end; vollength:=packagevol(@voltagedata);
if socketconnected=True then
clientsocket1.Socket.SendBuf(voltagedata,vollength);
end;procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
socketconnected:=false;
end;end.现在的问题就是我注释那个地方,数据有时候能正常上传,有时候不能。如果我想判断我的传感器不能读到数据应该怎么判读?如果其他地方有问题大家再帮我改改,谢谢,这个问题困扰我好几天了,不得其解,新人,忘大家指导。
begin
Timer1.enabled := false;
try
if socketconnected=false then
begin
clientsocket1.Close;
clientsocket1.Open;
end
else
upsend();
finally
Timer1.enabled := true;
end;
end;
先把这里的东西改一下
if MaxVol=null then
begin
voltagedata[0]:=$00;
voltagedata[1]:=$00;
end
else if MaxVol <10 then
begin
voltagedata[0]:=$00;
voltagedata[1]:=$00;
end else if MaxVol <100 then
begin
voltagedata[0]:=$52;
voltagedata[1]:=$0E;
end
else if MaxVol>300 then
begin
voltagedata[0]:=$52;
voltagedata[1]:=$0E;
end;
还有,这段内容我想用case语句来代替的话,小于符号如何表示啊?
if Bvoltage>MaxVol then MaxVol:=Bvoltage;
if Cvoltage>MaxVol then MaxVol:=Cvoltage; if MaxVol=Avoltage then
begin
voltagedata[0]:=ceil(voltage[0]/10000.0*220.0*1000/60);
voltagedata[1]:=ceil(voltage[1]/10000.0*220.0*1000/60);
end
else if MaxVol=Bvoltage then
begin
voltagedata[0]:=ceil(voltage[2]/10000.0*220.0*1000/60);
voltagedata[1]:=ceil(voltage[3]/10000.0*220.0*1000/60);
end
else if MaxVol=Cvoltage then
begin
voltagedata[0]:=ceil(voltage[4]/10000.0*220.0*1000/60);
voltagedata[1]:=ceil(voltage[5]/10000.0*220.0*1000/60);
end;
//这里是判读电压传感器数据的地方,这里这样写合理吗?这里判断没有读到传感器数据应该如何写啊,谢谢。
if MaxVol=null then
begin
voltagedata[0]:=$00;
voltagedata[1]:=$00;
end
else if MaxVol <10 then
begin
voltagedata[0]:=$00;
voltagedata[1]:=$00;
end else if MaxVol <100 then
begin
voltagedata[0]:=$52;
voltagedata[1]:=$0E;
end
else if MaxVol>300 then
begin
voltagedata[0]:=$52;
voltagedata[1]:=$0E;
end;
这些语句我想用case来表示,但是case的值不能为real
我该怎么写?