最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#用Activex實(shí)現(xiàn)Web客戶端讀取RFID功能的代碼

 更新時(shí)間:2011年05月11日 23:01:29   作者:  
由于要在Web項(xiàng)目中采用RFID讀取功能,所以有必要開(kāi)發(fā)Activex,一般情況下開(kāi)發(fā)Activex都采用VC,VB等,但對(duì)這兩塊不是很熟悉,所以采用C#編寫(xiě)Activex的方式實(shí)現(xiàn)
由于要在Web項(xiàng)目中采用RFID讀取功能,所以有必要開(kāi)發(fā)Activex,一般情況下開(kāi)發(fā)Activex都采用VC,VB等,但對(duì)這兩塊不是很熟悉,所以采用C#編寫(xiě)Activex的方式實(shí)現(xiàn)。
本文方法參考網(wǎng)絡(luò)
1.編寫(xiě)WindowsFromControls
2.發(fā)布WindowsFormControls為Activex
3.在web中使用該Activex
 
首先編寫(xiě)windows控件
如何編寫(xiě)不再詳述(注意一個(gè)地方,GUID自己用vs工具生成一個(gè),下面會(huì)用到。我的0CBD6597-3953-4B88-8C9F-F58B1B023413) 
重要的類:
復(fù)制代碼 代碼如下:

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 "";
}

}
}

復(fù)制代碼 代碼如下:

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);
}
}

復(fù)制代碼 代碼如下:

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里添加如下語(yǔ)句
[assembly: AllowPartiallyTrustedCallers()]
下一步,右鍵該項(xiàng)目,屬性,生成,將為com互操作注冊(cè),打上勾勾
 
 
然后編譯,如果沒(méi)有問(wèn)題,那么測(cè)試下,應(yīng)該可以讀取RFID的ID到文本框了。
 
2.制作安裝程序
跟普通的制作安裝程序一樣,新建一個(gè)安裝程序,然后刪掉里面的文件夾。
鼠標(biāo)右鍵空白區(qū)域-》添加-》項(xiàng)目輸出--》選擇主輸出
 
這樣即可生成安裝包了。
 
到現(xiàn)在其實(shí)已經(jīng)可以用了,但為了方便我們可以進(jìn)一步生成cab包。
下載CABARC.exe。解壓縮,到bin目錄中執(zhí)行如下doc命令
cabarc n 生成的cab名.cab  安裝文件.msi  install.inf
install.inf內(nèi)容如下:

[version] 
signature="$CHICAGO$" 
AdvancedINF=2.0 

[Setup Hooks] 
hook1=hook1
[hook1]
run=msiexec.exe /i "%EXTRACT_DIR%\ReaderInstaller.msi" /qn

修改稱自己的安裝文件即可

3.在web中使用。

新建一個(gè)web項(xiàng)目,在default.aspx中輸入一下代碼即可使用

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

這里的clsid就是自己生成的GUID編號(hào)

這里的RFID使用的是MasterRD.dll和CFCom.dll不同產(chǎn)品使用可能不同,同時(shí)注意RFID的COM端口號(hào),本例為測(cè)試?yán)?,所以?xiě)死了COM口,客戶端IE瀏覽時(shí),需要將RFID的端口改成對(duì)應(yīng)的。

注意:如果發(fā)布到服務(wù)器上,客戶端ie上無(wú)法顯示控件,那么請(qǐng)將訪問(wèn)地址添加到ie的受信任站點(diǎn),如果不能安裝cab那么只能用戶自己安裝Activex了。

參考文獻(xiàn) http://www.fzitv.net/article/27115.htm
源文件下載地址:http://xiazai.jb51.net/201105/yuanma/RFIDReader.rar

相關(guān)文章

最新評(píng)論

阿克苏市| 汉寿县| 沁水县| 文水县| 浦江县| 东乌珠穆沁旗| 盐津县| 化德县| 株洲县| 宁安市| 姚安县| 彭水| 新蔡县| 中山市| 措美县| 芜湖县| 抚州市| 南召县| 集贤县| 宁德市| 海阳市| 自治县| 卓尼县| 新晃| 原平市| 乌拉特前旗| 湖北省| 玉林市| 镇雄县| 望奎县| 高阳县| 茶陵县| 长宁区| 伊宁县| 广汉市| 井研县| 柘荣县| 丹巴县| 朝阳区| 弋阳县| 库尔勒市|