国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

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

2020-01-24 03:44:11
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
由于要在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) //m.survivalescaperooms.com/article/27115.htm
源文件下載地址:http://xiazai.VeVB.COm/201105/yuanma/RFIDReader.rar

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿瓦提县| 布尔津县| 夏津县| 黔西县| 荔浦县| 巴林右旗| 方城县| 吉木乃县| 新密市| 农安县| 临夏市| 张家川| 瑞金市| 虞城县| 北宁市| 苏尼特右旗| 高雄县| 宁夏| 沾益县| 桃江县| 焦作市| 宜兴市| 宜君县| 将乐县| 罗源县| 盐边县| 廊坊市| 肥西县| 大渡口区| 东安县| 湖南省| 榕江县| 武功县| 西充县| 葫芦岛市| 伽师县| 兴国县| 荆州市| 莱州市| 施甸县| 枣强县|