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

开发 后端
今天我们将要谈到的是C#用Activex实现Web客户端读取RFID功能,这也是在实际生产过程中会用到的方法。

由于要在Web项目中采用RFID读取功能,所以有必要开发Activex,一般情况下开发Activex都采用VC,VB等,但对这两块不是很熟悉,所以采用C#编写Activex的方式实现。

本文方法参考网络

1.编写WindowsFromControls

2.发布WindowsFormControls为Activex

3.在web中使用该Activex

首先编写windows控件

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

重要的类:

 

 

 

 

 

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

view sourceprint?

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

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

 

[assembly: AllowPartiallyTrustedCallers()] 
  • 1.

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

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

2.制作安装程序

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

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

这样即可生成安装包了。

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

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

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

install.inf内容如下:

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

 

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

3.在web中使用。

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

 

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

这里的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

【编辑推荐】

  1. .NET不用控件实现文件夹监测系统
  2. 详解C#中不同类的类型
  3. 浅谈C#中标准Dispose模式的实现
  4. C#图片处理的3种高级实用方法
  5. C# 4.0新特性:协变与逆变中的编程思想
责任编辑:彭凡 来源: 博客园
相关推荐

2009-08-21 15:36:41

服务端与客户端

2009-08-21 15:54:40

服务端与客户端

2009-08-06 17:12:13

C# WebServi

2015-05-12 14:16:15

C#ActiveX控件web调用

2009-08-21 17:53:25

C#网络编程客户端程序

2020-03-19 08:00:00

客户端KubernetesAPI

2009-08-21 14:33:15

C#异步传输字符串

2009-08-21 15:59:22

服务端与客户端通信

2009-08-21 16:14:52

服务端与客户端通信

2009-02-04 17:39:14

ibmdwWebSphereDataPower

2009-08-21 16:37:54

C#客户端程序

2009-08-07 13:55:35

Java客户端类调用C# WebServi

2009-08-21 17:48:43

C#网络编程

2011-08-25 10:37:15

leveldb的访问封C#客户端源码

2024-12-23 06:00:00

TCPC#网络

2024-05-06 08:00:00

C#IP地址

2013-06-08 09:59:15

VMwarevSphere Web

2023-11-03 08:15:27

PythonC#

2009-08-12 10:35:50

C#调用ActiveX

2009-08-06 16:58:40

C#编写ActiveX
点赞
收藏

51CTO技术栈公众号