C # code for using Activex to read RFID functions from Web clients

Source: Internet
Author: User

Because RFID reading is used in Web projects, it is necessary to develop Activex. Generally, Activex is developed using VC, VB, etc. However, these two parts are not very familiar, therefore, C # is used to compile Activex. For more information, see network 1. compile WindowsFromControls2. publish WindowsFormControls to Activex3. use this Activex in the web. First, compile the windows Control and do not elaborate on it. (Note that a guid is generated by using the vs tool, which will be used below. My 0CBD6597-3953-4B88-8C9F-F58B1B023413) important classes: Copy codeThe Code is 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 PSNR, 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 the 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 codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Runtime. InteropServices;

Namespace RFIDReader
{
[ComImport, guidattriort ("<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 codeThe Code is 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 Member
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 timereffectick (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 ";
}
}
}

To display controls on ie on all clients, add the following statement to AssemblyInfo. cs of the program:
[Assembly: AllowPartiallyTrustedCallers ()] Next Step: Right-click the project and select Properties, generate, register for com interoperability, check and compile. If there is no problem, test, you should be able to read the rfid id to the text box. 2. Create an installation program, just like a normal installation program, and then delete the folder. Right-click blank area-add-project output-select master output to generate the installation package. Now we can use it, but we can further generate the cab package for convenience. Download cabarc.exe. Decompress the package and run the following doc command in the bin directory. The contents of the cab name. cab Installation File. msi install. infinstall. inf generated by cabarc n are as follows:

[Version]
Signature = "$ CHICAGO $"
AdvancedINF = 2.0

[Setup Hooks]
Hook1 = hook1
[Hook1]
Runningmsiexec.exe/I "% EXTRACT_DIR % \ ReaderInstaller. msi"/qn

Modify your own installation file.

3. Use in web.

Create a web project and enter the code in default. aspx to use

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

The clsid here is the guid id generated by the user.

The RFID here uses MasterRD. dll and CFCom. the use of different dll products may be different. Pay attention to the rfid com port number. This example is a test example. Therefore, the comport is dead. During IE browser on the client, you need to change the RFID port to the corresponding one.

Note: If the control cannot be displayed on the ie client when it is published to the server, add the access address to the trusted site of ie. If the cab cannot be installed, you can only install Activex on your own.

References http://www.jb51.net/article/27115.htm
Source File: 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.