C # using ActiveX to read RFID from a Web Client

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:
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 "";}}}
 
 
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;namespace RFIDReader{    [ComImport, GuidAttribute("0CBD6597-3953-4B88-8C9F-F58B1B023413 ")]    [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)]
Publicpartialclass reader: usercontrol, IObjectSafety
{

Public reader ()
{
Initializecomponent ();
}

# Region IObjectSafety Member

Publicvoid getinterfaccesafyoptions (INT riid, outint pdwsupportedoptions, outint pdwenabledoptions)
{
Pdwsupportedoptions = 1;
Pdwenabledoptions = 2;
}

Publicvoid setinterfacesafetyoptions (INT riid, int dwoptionssetmask, int dwenabledoptions)
{
Thrownew notimplementedexception ();
}

# Endregion

Privatevoid timereffectick (Object sender, eventargs E)
{
Readrfid RFID = new readrfid ();
String STR = RFID. cardnum;
If (STR! = "")
{
Textbox1.text = STR; getinfo ();
}
}
Publicint timerspan
{
Get {return timer1.interval ;}
Set {timer1.interval = value ;}
}
Publicstring cardnum
{
Get {return textbox1.text ;}
}
Privatevoid getinfo ()
{
This. label1.text = "CCCC ";
}
}
} In order to display controls on all client ie, the assemblyinfo. in CS, add the following statement [Assembly: allowpartiallytrustedcallers ()] Next, right-click the project, properties, generate, register for com interoperability, check and compile. If no problem exists, in the test, you can 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.cnblogs.com/yungboy/archive/2011/01/10/1932433.html

Source File: http://files.cnblogs.com/qidian10/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.