Delphi中关于OleVariant 自动化变量的一点小小的认识(原发于2007-6-13)

本文分享了使用Delphi编程语言与CAN总线接口卡交互的经验,详细介绍了如何利用OleVariant类型变量实现数据的发送与接收,解决了在实际应用中遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记下我的经历,希望对你有用。

上个星期,因为要帮老师带CAN总线的实验,买的是北京三纯科技的CAN总线的接口卡和CAN总线PCI 的接口卡。实验要我自己想如何做。晕。不过没有办法。想来想去,只能通过电脑控制PCI 接口的CAN卡,然后去控制其他的接口卡,三纯都是用51单片机做的。接口其实很简单。   但是电脑上没有很好的控制软件。所以就只有自己写一个。

还好他们提供了与 PCI 总线接口的  ActiveX 控件。  也有例程,不过是VC++ 和VB的,可是我想用Delphi 来写。

于是导入  提供的 sc2102c.ocx  控件。 

一开始,对于,打开CAN 口, 复位啊。都没有问题。可是到了发送和接收的时候就不行了。
sc2102.send    函数的发送的数据要    Olevariant 变量,    呵呵,说来不怕你笑,Delphi 我还用的不熟,所以我根本就没有见过什么Olevariant   变量。于是看 Delphi 的帮助,得到结果如下:

The OleVariant type exists on both the Windows and Linux platforms. The main difference between Variant and OleVariant is that Variant can contain data types that only the current application knows what to do with. OleVariant can only contain the data types defined as compatible with OLE Automation which means that the data types that can be passed between programs or across the network without worrying about whether the other end will know how to handle the data.
When you assign a Variant that contains custom data (such as a Delphi string, or a one of the new custom variant types) to an OleVariant, the runtime library tries to convert the Variant into one of the OleVariant standard data types (such as a Delphi string converts to an OLE BSTR string). For example, if a variant containing an AnsiString is assigned to an OleVariant, the AnsiString becomes a WideString. The same is true when passing a Variant to an OleVariant function parameter.   

大意只是讲到 olevariant 的好处,还说到了数据的自动变化。 看完后,还是不明白。于是上网找。看看如何才能给 Olevariant   赋值。   找好久也找不到想要的。最后发现用 VarArrayCreate   可以创建数组空间。可是还是不知如何给他们赋值。再上网找。又找到一点。那个控件可以发送成功了。可是到了接收的那个地方,我又不知,如何从   Olevariant 变量中收到的值取出来。 没有办法。再上网找。还是没有结果。(其实已在 Mscomm 串口控件中得到启示。)但还是不会。因为程序总是出错。
后来同学帮忙用VC++写了一个,其实有VC++例子的。可是Delphi还是不会啊。 同学说,那个控件可以用VC打开的,我听了很高兴,因为我不是计算机专业,是学电子的。电脑编程还是很多不知道,而且我不会VC++   。于是用 Delphi 打开后发现     定义 :  Variant *   。 是一个Variant   指针。突然间不知道什么原因。我发现其实已找到答案。OleVariant   就如 Variant 的指针

申明 a: OleVariant 
然后申请空间:         a:=VarArrayCreate([0, m-1], varByte);
申请 m 个字节的空间。
然后用 dat: array[0..m]   of bye     对a进行赋值,如下:
     for i:=0 to  m-1 do
       begin
               a[i]:=dat[i];
       end;

这样就可以了。 a 就是一个数组。长度 m     ,就如Delphi 中的动态数组一样。 在程度中我是这样写的。

procedure TForm1.can0_sendClick(Sender: TObject);
var
       a: olevariant;
       i:integer;
       len:integer;
begin
   len:= strtoint(len_0.Text);
   if( len>0 ) then
   begin
       ID0 := strtoint(id_0.Text) ;
       RTR0 := strtoint(rtr_0.Text) ;

       a:=VarArrayCreate([0, len-1], varByte);
       for i:=0 to len-1 do
       begin
               a[i]:=byte(send_edit_0.Text[i+1]);
       end;
       if( pcican.SEND(0,ID0,RTR0,len,a)) then
       begin
               form1.StatusBar1.Panels[3].Text:='can0数据发送成功';
       end;
                                                                         olevariant
     end;
end

上面是发送的程序,下面是接到的,其实接收要简单,因为 OleVariant 在接收时会自己动申请空间。
   buff : OleVariant 
   然后经过接收的函数申请空间。如下:
         pcican.RECEIVE(1,buff )         //     SC2102c.oxc 控件的接口函数。 

于是 buff   是指向一个 byte 数组的指针。引用它的元素其实很简单: buff [ i ] 就可以了。
我是这样写的.

procedure TForm1.pcicanCanEvent(ASender: TObject; IntFlag: Smallint);
var
       buff: olevariant;
       s: array[0..7] of byte;
       i:integer;
begin
       if((IntFlag and 16)=16) then
       begin
               if( pcican.RECEIVE(1,buff )) then
               begin
               for i:=0 to 7 do
               begin
                       s[i]:=buff[i];
               end;
               can1_rec_memo.Lines.Add(BytesToStr(s));
               label19.Caption:=inttostr(pcican.ID);
               end;
       end
       else
       if((IntFlag and 1)=1) then
       begin
               if( pcican.RECEIVE(0,buff )) then
               begin
               for i:=0 to 7 do
               begin
                       s[i]:=buff[i];
               end;
               can0_rec_memo.Lines.Add( BytesToStr(s) );
               end;
       end;
end;

上面的程序是针对, 三纯的PCI 接口的CAN总线接口卡的 ocx 控件所写. 

在网上找关于 Olevariant   如何存入数据,   如何取出数据,   其本上没有一个人,清楚的说出来,不过也许是我没理解.   看到一个要用到 Olybums  (忘了如何拼了) 相机的控件,   和我原来的问题很像, 找来找去也没有一个人回答,可能是大家没有遇到,或是知道,可是没有写出来. 

呵呵,   这次, 是我第一次写出来与大家分享,   一家之言,   因为没有如何如何地看书,   不对的地方 希望大家可以指出.   共同进步.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值