当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

C#用Activex实现Web客户端读取RFID功能

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:50:59

:2011-04-18 22:02:54

本文介绍了在C#中使用Activex实现Web客户端读取RFID功能。

由于要在Web项目中采用RFID读取功能,所以有必要开发Activex,一般情况下开发Activex都采用VC,VB等,而本文采用C#编写Activex的方式实现。

本文方法参考网络

1.编写WindowsFromControls

2.发布WindowsFormControls为Activex

3.在web中使用该Activex

1、编写windows控件

如何编写不再详述(注意一个地方,GUID自己用vs工具生成一个,下面会用到。我的0CBD6597-3953-4B88-8C9F-F58B1B023413)

重要的类:

  1. using System;   
  2. using System.Runtime.InteropServices;   
  3. namespace RFIDReader   
  4. {   
  5. public class ReadRfid   
  6. {   
  7. [DllImport("MasterRD.dll")]   
  8. private static extern int rf_init_com(int port, int baud);   
  9. [DllImport("MasterRD.dll")]   
  10. private static extern int rf_request(short icdev, byte model, ref short TagType);   
  11. [DllImport("MasterRD.dll")]   
  12. private static extern int rf_write(int icdev, char _Adr, char _Data);   
  13. [DllImport("MasterRD.dll")]   
  14. private static extern int rf_anticoll(short icdev, byte bcnt, ref byte ppsnr, ref byte pRLength);   
  15. [DllImport("MasterRD.dll")]   
  16. private static extern int rf_ClosePort();   
  17. public string CardNum   
  18. {   
  19. get { return getCardNum(); }   
  20. }   
  21. private string getCardNum()   
  22. {   
  23. int post = 4; //调用COM1口   
  24. int baud = 9600;   
  25. int i = -1;   
  26. byte model = 82;   
  27. byte b1 = 4;   
  28. short TagType = 4;   
  29. byte[] buf1 = new byte[200];   
  30. try   
  31. {   
  32. rf_init_com(post, baud);   
  33. rf_request(0, model, ref TagType);   
  34. rf_anticoll(0, 4, ref buf1[0], ref b1);   
  35. string s1 = "";   
  36. for (i = 0; i < b1; i++)   
  37. {   
  38. s1 = s1 + System.Convert.ToString(long.Parse(buf1[i].ToString()), 16).ToUpper();   
  39. }   
  40. rf_ClosePort();   
  41. if (s1 == "0000")   
  42. throw (new Exception()); }   
  43. return s1;   
  44. }   
  45. catch (Exception)   
  46. {   
  47. }   
  48. return "";   
  49. }   
  50. }   
  51. }  

view sourceprint?

  1. using System;   
  2.  
  3. using System.Collections.Generic;   
  4. using System.Linq;   
  5. using System.Text;   
  6. using System.Runtime.InteropServices;   
  7. namespace RFIDReader   
  8. {   
  9. [ComImport, GuidAttribute("<SPAN style="COLOR: #800000">0CBD6597-3953-4B88-8C9F-F58B1B023413</SPAN>
  10. <SPAN style="COLOR: #800000"> </SPAN>")]   
  11. [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]   
  12. public interface IObjectSafety   
  13. {   
  14. [PreserveSig]   
  15. void GetInterfacceSafyOptions(   
  16. int riid,   
  17. out int pdwSupportedOptions,   
  18. out int pdwEnabledOptions);   
  19. [PreserveSig]   
  20. void SetInterfaceSafetyOptions(   
  21. int riid,   
  22. int dwOptionsSetMask,   
  23. int dwEnabledOptions);   
  24. }   
  25. }   
  26. using System;
    using System.Collections.Generic;
    using System.ComponentModel;  
  27. using System.Drawing;  
  28. using System.Data;  
  29. using System.Linq;  
  30. using System.Text;  
  31. using System.Windows.Forms;  
  32. using System.Runtime.InteropServices;  
  33. using CJ;  
  34. namespace RFIDReader{     
  35.  [Guid("0CBD6597-3953-4B88-8C9F-F58B1B023413"), ProgId("RFIDReader.Reader"), ComVisible(true)]      
  36.  public partial class Reader : UserControl,IObjectSafety      
  37. {     
  38.      public Reader()       
  39.    {            
  40.   InitializeComponent();      
  41.      }        
  42.   #region IObjectSafety 成员      
  43. public void GetInterfacceSafyOptions(int riid, out int pdwSupportedOptions, out int pdwEnabledOptions)    
  44.       {     
  45.        pdwSupportedOptions = 1;        
  46.       pdwEnabledOptions = 2;      
  47.     }       
  48.    public void SetInterfaceSafetyOptions(int riid, int dwOptionsSetMask, int dwEnabledOptions)   
  49.      {             
  50.  throw new NotImplementedException();        
  51.   }     
  52.      #endregion       
  53.    private void timer1_Tick(object sender, EventArgs e)        
  54.   {             
  55.  ReadRfid rfid=new ReadRfid();        
  56.       string str = rfid.CardNum;          
  57.     if (str != "")             
  58.  {               
  59.    textBox1.Text = str; GetInfo();         
  60.      }        
  61.   }        
  62.   public int TimerSpan     
  63.      {             
  64.  get { return timer1.Interval; }      
  65.         set { timer1.Interval = value; }        
  66.   }        public string CardNum        
  67.   {       
  68.        get { return textBox1.Text; }      
  69.     }      
  70.     private void GetInfo()       
  71.    {                      
  72.     this.label1.Text = "cccc";    
  73.       }    }}

为了能够在所有客户端ie上显示控件,要在程序的AssemblyInfo.cs里添加如下语句

[assembly: AllowPartiallyTrustedCallers()]

下一步,右键该项目,属性,生成,将为com互操作注册,打上勾勾

然后编译,如果没有问题,那么测试下,应该可以读取RFID的ID到文本框了。

2.制作安装程序

跟普通的制作安装程序一样,新建一个安装程序,然后删掉里面的文件夹。

鼠标右键空白区域-》添加-》项目输出--》选择主输出

这样即可生成安装包了。

到现在其实已经可以用了,但为了方便我们可以进一步生成cab包。

下载CABARC.exe。解压缩,到bin目录中执行如下doc命令

cabarc n 生成的cab名.cab 安装文件.msi install.inf

install.inf内容如下:

  1. [version]   
  2.  
  3. signature="$CHICAGO$"   
  4. AdvancedINF=2.0  
  5.  
  6. [Setup Hooks]   
  7. hook1hook1=hook1   
  8. [hook1]  
  9. run=msiexec.exe /i "%EXTRACT_DIR%\ReaderInstaller.msi" /qn 

修改成自己的安装文件即可

3.在web中使用。

新建一个web项目,在default.aspx中输入一下代码即可使用

  1. <object id="RFIDReader" classid="clsid:0CBD6597-3953-4B88-8C9F-F58B1B023413"   
  2. codebase="RFID/RFIDREADER.cab"> 
  3. </object> 

这里的clsid就是自己生成的GUID编号

这里的RFID使用的是MasterRD.dll和CFCom.dll不同产品使用可能不同,同时注意RFID的COM端口号,本例为测试例子,所以写死了COM口,客户端IE浏览时,需要将RFID的端口改成对应的。

注意:如果发布到服务器上,客户端ie上无法显示控件,那么请将访问地址添加到ie的受信任站点,如果不能安装cab那么只能用户自己安装Activex了。

原文链接:http://www.cnblogs.com/qidian10/archive/2011/04/06/2006976.html


C#用Activex实现Web客户端读取RFID功能