当前位置:编程学习 > C#/ASP.NET >>

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) 
重要的类:
 

view sourceprint?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 ""; 
        } 
  
    } 
}
view sourceprint?  
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-F58B1B
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,