C # code _c# tutorial on using ActiveX to implement Web client read RFID function

Source: Internet
Author: User
Tags port number
Because of the use of RFID in the Web project to read the function, so it is necessary to develop ActiveX, the general development of ActiveX use VC,VB, etc., but the two are not very familiar with, so use C # to write ActiveX way to achieve.
This article method reference network
1. Preparation of Windowsfromcontrols
2. Publish Windowsformcontrols as ActiveX
3. Use this ActiveX in the Web
First write the Windows control
How to write no more details (note a place where the GUID generates one for itself with the VS tool, which is used below). My 0cbd6597-3953-4b88-8c9f-f58b1b023413)
Important classes:
Copy Code code as follows:

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; Call COM1 Port
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 "";
}

}
}

Copy Code code as follows:

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

Copy Code code as follows:

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

In order to be able to display the control on all client IE, add the following statement to the AssemblyInfo.cs of the program
[Assembly:allowpartiallytrustedcallers ()]
Next, right-click on the item, properties, build, register for COM interop, tick
Then compile, if there is no problem, then the test should be able to read the RFID ID to the text box.
2. Make installation program
Like a normal Setup program, create a new installer and delete the folder inside.
right mouse button blank area-"add-" project output--"Select Primary output
This allows the installation package to be generated.
It is now in fact available, but for convenience we can further generate the CAB package.
Download CABARC.exe. Extract, execute the following doc command into the bin directory
Cabarc n generated cab name. cab installation file. msi Install.inf
Install.inf contents are as follows:

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

[Setup hooks] 
Ho Ok1=hook1
[Hook1]
run=msiexec.exe/i "%extract_dir%\readerinstaller.msi"/qn

Modifies its own installation file

3. In We b in use.

To create a new Web project, enter the code in default.aspx to use the

<object id= "Rfidreader classid=" clsid:0 cbd6597-3953-4b88-8c9f-f58b1b023413 "
        codebase=" Rfid/ Rfidreader.cab
    </object>

The CLSID here is the GUID number you generated

Here the use of RFID is MasterRD.dll and CFCom.dll different products use may be different, while paying attention to the RFID COM port number, this example for testing examples, so write dead COM port, client IE browsing, the need to change the RFID port to correspond.

Note: If the control cannot be displayed on client IE on the server, add the access address to the trusted site of IE, and the user can only install ActiveX if the CAB cannot be installed.

reference http://www.jb51.net/article/27115.htm
source file download Address: http://xiazai.jb51.net/201105/ Yuanma/rfidreader.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.