C#用Activex实现Web客户端读取RFID功能的代码
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 "";
}
}
}
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";
}
}
}
为了能够在所有客户端ie上显示控件,要在程序的AssemblyInfo.cs里添加如下语句
[assembly: AllowPartiallyTrustedCallers()]
[version]
signature="$CHICAGO$"
AdvancedINF=2.0
[Setup Hooks]
hook1=hook1
[hook1]
run=msiexec.exe /i "%EXTRACT_DIR%\ReaderInstaller.msi" /qn
修改称自己的安装文件即可
3.在web中使用。
新建一个web项目,在default.aspx中输入一下代码即可使用
<object id="RFIDReader" classid="clsid:0CBD6597-3953-4B88-8C9F-F58B1B023413"
codebase="RFID/RFIDREADER.cab">
</object>
这里的clsid就是自己生成的GUID编号
这里的RFID使用的是MasterRD.dll和CFCom.dll不同产品使用可能不同,同时注意RFID的COM端口号,本例为测试例子,所以写死了COM口,客户端IE浏览时,需要将RFID的端口改成对应的。
注意:如果发布到服务器上,客户端ie上无法显示控件,那么请将访问地址添加到ie的受信任站点,如果不能安装cab那么只能用户自己安装Activex了。
参考文献 https://www.jb51.net/article/27115.htm
源文件下载地址:http://xiazai.jb51.net/201105/yuanma/RFIDReader.rar
相关文章
C#实现读取DataSet数据并显示在ListView控件中的方法
这篇文章主要介绍了C#实现读取DataSet数据并显示在ListView控件中的方法,涉及C#操作DataSet及ListView控件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下2015-10-10
最新评论