線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1266
推到 Plurk!
推到 Facebook!

(串口發送命令)中出現錯誤invalid variant type conversion

缺席
blue36sea
一般會員


發表:13
回覆:35
積分:23
註冊:2008-10-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-07-12 22:07:46 IP:202.197.xxx.xxx 訂閱
(串口發送命令)中出現錯誤invalid variant type conversion:

程序如下所示:
var
a,b1,c1,d1,e1,f1,g1:integer;
b,c,d,e,f,g:double;
tmpvar:variant;
begin
if not mscomm1.PortOpen then
begin
timer20.Enabled:=false;
BitBtn14.Caption:='啓動刷新';
BitBtn15.Caption:='啓動刷新';
BitBtn16.Caption:='啓動刷新';
BitBtn17.Caption:='啓動刷新';
BitBtn22.Caption:='啓動刷新';
showmessage('請打開串口!');
exit;
end;
if (edit16.Text='') or (edit17.Text='') or (edit18.Text='') or (edit19.Text='') or (edit20.Text='')or (edit21.Text='') or (edit22.Text='')then
begin
showmessage('參數不能為空');
exit;
end;
a:=strtoint(edit16.Text);
b:=strtofloat(edit17.Text);
c:=strtofloat(edit18.Text);
d:=strtofloat(edit19.Text);
e:=strtofloat(edit20.Text);
f:=strtofloat(edit21.Text);
g:=strtofloat(edit22.Text);

if (a<=300)or(a>=1200) then
begin
showmessage('轉速超出範圍(300,1200)');
exit;
end
else
if (b<=0)or(b>1)or(d<=0)or(d>1)or(f<=0)or(f>1) then
begin
showmessage('Kp超出範圍(0,1]');
exit;
end
else
if (c<0)or(c>1)or(e<0)or(e>1)or(g<0)or(g>1) then
begin
showmessage('Ki超出範圍[0,1]');
exit;
end
else
if (a>300) and (a<1200) and (b>0) and (b<=1) and (c>=0) and (c<=1) and (d>0) and (d<=1)and (e>=0) and (e<=1) and (f>0) and (f<=1)and (g>=0) and (g<=1) then
begin
b:=b*32768;
c:=c*32768;
d:=b*32768;
e:=c*32768;
f:=b*32768;
g:=c*32768;
b1:=round(b);
c1:=round(c);
d1:=round(d);
e1:=round(e);
f1:=round(f);
g1:=round(g);
tmpvar:=vararraycreate([1,1],varbyte);
tmpvar[1]:=224;
vararrayredim(tmpvar,2);
tmpvar[2]:=a mod 256;
vararrayredim(tmpvar,3);
tmpvar[3]:=a div 256;
vararrayredim(tmpvar,4);
tmpvar[4]:=b1 mod 256;
vararrayredim(tmpvar,5);
tmpvar[5]:=b1 div 256;
vararrayredim(tmpvar,6);
tmpvar[6]:=c1 mod 256;
vararrayredim(tmpvar,7);
tmpvar[7]:=c1 div 256;
vararrayredim(tmpvar,8);
tmpvar[8]:=d1 mod 256;
vararrayredim(tmpvar,9);
tmpvar[9]:=d1 div 256;
vararrayredim(tmpvar,10);
tmpvar[10]:=e1 mod 256;
vararrayredim(tmpvar,11);
tmpvar[11]:=e1 div 256;
vararrayredim(tmpvar,12);
tmpvar[12]:=f1 mod 256;
vararrayredim(tmpvar,13);
tmpvar[13]:=f1 div 256;
vararrayredim(tmpvar,14);
tmpvar[14]:=g1 mod 256;
vararrayredim(tmpvar,15);
tmpvar[15]:=g1 div 256;
mscomm1.output:=tmpvar;
showmessage('命令發送成功!');
end
else
begin
showmessage('輸入參數有誤,請重新設置!');
exit;
end;
end;
-------------------------------------------------------------------

我將該模塊獨立出來,將語句
tmpvar:=vararraycreate([1,1],varbyte);
修改成
tmpvar:=vararraycreate([1,1],varVariant);
測試正常
可是放回串口通信程序中,該部分就出現錯誤“無效的屬性值”!
請問各位大大,這個問題究竟要怎麽解決啊!!
blue36sea
一般會員


發表:13
回覆:35
積分:23
註冊:2008-10-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-07-13 10:53:26 IP:202.197.xxx.xxx 訂閱
我在另一個模塊中發現命令中只用了7個字節程序運行正常:
b:=b*32768;
c:=c*32768;
b1:=round(b);
c1:=round(c);
tmpvar:=vararraycreate([1,1],varbyte);
tmpvar[1]:=224;
vararrayredim(tmpvar,2);
tmpvar[2]:=a mod 256;
vararrayredim(tmpvar,3);
tmpvar[3]:=a div 256;
vararrayredim(tmpvar,4);
tmpvar[4]:=b1 mod 256;
vararrayredim(tmpvar,5);
tmpvar[5]:=b1 div 256;
vararrayredim(tmpvar,6);
tmpvar[6]:=c1 mod 256;
vararrayredim(tmpvar,7);
tmpvar[7]:=c1 div 256;
mscomm1.output:=tmpvar;
showmessage('命令發送成功!');
但是我在出錯的程序中出錯位置在“vararrayredim(tmpvar,9);”
請問為什麽會這樣?

blue36sea
一般會員


發表:13
回覆:35
積分:23
註冊:2008-10-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-07-13 16:39:31 IP:202.197.xxx.xxx 訂閱
解決了
修改後的程序如下:
var
i,a,b1,c1,d1,e1,f1,g1:integer;
b,c,d,e,f,g:double;
tmpvar:variant;
buf:array[1..15]of byte;
begin
if not mscomm1.PortOpen then
begin
timer20.Enabled:=false;
BitBtn14.Caption:='啓動刷新';
BitBtn15.Caption:='啓動刷新';
BitBtn16.Caption:='啓動刷新';
BitBtn17.Caption:='啓動刷新';
BitBtn22.Caption:='啓動刷新';
showmessage('請打開串口!');
exit;
end
else
if (edit1.Text='') or (edit2.Text='') or (edit23.Text='') or (edit24.Text='') or (edit25.Text='')or (edit26.Text='') or (edit27.Text='')then
begin
showmessage('參數不能為空');
exit;
end;
a:=strtoint(edit1.Text);
b:=strtofloat(edit2.Text);
c:=strtofloat(edit23.Text);
d:=strtofloat(edit24.Text);
e:=strtofloat(edit25.Text);
f:=strtofloat(edit26.Text);
g:=strtofloat(edit27.Text);

if (a<=0)or(a>=1500) then
begin
showmessage('轉速超出範圍(0,1500)');
exit;
end
else
if (b<=0)or(b>1)or(d<=0)or(d>1)or(f<=0)or(f>1) then
begin
showmessage('Kp超出範圍(0,1]');
exit;
end
else
if (c<0)or(c>1)or(e<0)or(e>1)or(g<0)or(g>1) then
begin
showmessage('Ki超出範圍[0,1]');
exit;
end
else
if (a>0) and (a<1500) and (b>0) and (b<=1) and (c>=0) and (c<=1) and (d>0) and (d<=1)and (e>=0) and (e<=1) and (f>0) and (f<=1)and (g>=0) and (g<=1) then
begin
b:=b*32768;
c:=c*32768;
d:=b*32768;
e:=c*32768;
f:=b*32768;
g:=c*32768;
b1:=round(b);
c1:=round(c);
d1:=round(d);
e1:=round(e);
f1:=round(f);
g1:=round(g);
buf[1]:=224;buf[2]:=a mod 256;buf[3]:=a div 256;
buf[4]:=b1 mod 256;buf[5]:=b1 div 256;
buf[6]:=c1 mod 256;buf[7]:=c1 div 256;
buf[8]:=d1 mod 256;buf[9]:=d1 div 256;
buf[10]:=e1 mod 256;buf[11]:=e1 div 256;
buf[12]:=f1 mod 256;buf[13]:=f1 div 256;
buf[14]:=g1 mod 256;buf[15]:=g1 div 256;
tmpvar:=vararraycreate([1,15],varbyte);
for i:=1 to 15 do
begin
tmpvar[i]:=Buf[i];
end;
mscomm1.output:=tmpvar;
showmessage('命令發送成功!');
exit;
end
else
begin
showmessage('輸入參數有誤,請重新設置!');
exit;
end;
end;
系統時間:2024-05-15 7:00:19
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!