由於要在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 >0CBD6597-3953-4B88-8C9F-F58B1B023413</SPAN><SPAN > </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