C#用Activex實(shí)現(xiàn)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; //調(diào)用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項(xiàng)目,在default.aspx中輸入一下代碼即可使用
<object id="RFIDReader" classid="clsid:0CBD6597-3953-4B88-8C9F-F58B1B023413"
codebase="RFID/RFIDREADER.cab">
</object>
這里的clsid就是自己生成的GUID編號
這里的RFID使用的是MasterRD.dll和CFCom.dll不同產(chǎn)品使用可能不同,同時注意RFID的COM端口號,本例為測試?yán)?,所以寫死了COM口,客戶端IE瀏覽時,需要將RFID的端口改成對應(yīng)的。
注意:如果發(fā)布到服務(wù)器上,客戶端ie上無法顯示控件,那么請將訪問地址添加到ie的受信任站點(diǎn),如果不能安裝cab那么只能用戶自己安裝Activex了。
參考文獻(xiàn) http://chabaoo.cn/article/27115.htm
源文件下載地址:http://xiazai.jb51.net/201105/yuanma/RFIDReader.rar
- asp.net(c#)限制用戶輸入規(guī)定的字符和數(shù)字的代碼
- C#編程獲取客戶端計(jì)算機(jī)硬件及系統(tǒng)信息功能示例
- C#獲取客戶端相關(guān)信息實(shí)例總結(jié)
- C#聊天程序服務(wù)端與客戶端完整實(shí)例代碼
- c#多線程網(wǎng)絡(luò)聊天程序代碼分享(服務(wù)器端和客戶端)
- 在C#中對TCP客戶端的狀態(tài)封裝詳解
- C#實(shí)現(xiàn)支持?jǐn)帱c(diǎn)續(xù)傳多線程下載客戶端工具類
- 客戶端實(shí)現(xiàn)藍(lán)牙接收(C#)知識總結(jié)
- 獲取客戶端IP地址c#/vb.net各自實(shí)現(xiàn)代碼
- asp.net(C#)中給控件添加客戶端js事件的方法
- .NET/C#實(shí)現(xiàn)識別用戶訪問設(shè)備的方法
相關(guān)文章
C#利用System.Uri轉(zhuǎn)URL為絕對地址的方法
這篇文章主要介紹了C#利用System.Uri轉(zhuǎn)URL為絕對地址的方法,涉及C#操作URL的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02C#實(shí)現(xiàn)泛型動態(tài)循環(huán)數(shù)組隊(duì)列的方法
隊(duì)列一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu),本文通過實(shí)例代碼給大家介紹下C#實(shí)現(xiàn)泛型動態(tài)循環(huán)數(shù)組隊(duì)列的方法,感興趣的朋友一起看看吧2022-01-01C#實(shí)現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法
這篇文章主要介紹了C#實(shí)現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法,涉及C#操作DataSet及ListView控件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10C#調(diào)用FFplay實(shí)現(xiàn)播放視頻功能
這篇文章主要為大家詳細(xì)介紹了C#如何調(diào)用FFplay實(shí)現(xiàn)播放視頻功能,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10