页面源码:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jzyd.aspx.cs" Inherits="BSCard.jzyd" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <script type="text/javascript"> window.onload = function () { document.getElementById("Object1").RegJs(window, "Show"); //将页面对象,和所需调用的js方法名传给activex } //关闭页面时关闭串口 window.onbeforeunload=function() { document.getElementById("Object1").closeSp(); } //activex调用的js方法 function Show(str) { document.getElementById("text1").value = str; } //将卡号加载到页面中 function load() { document.getElementById("text1").value = document.getElementById("Object1").GetCard(); } function keyDown() { var keyCode = event.keyCode; //按下空格键的时候,重新发送命令 if (keyCode==32) { document.getElementById("Object1").load(); } //回车键的时候,获取卡号 if (keyCode==13) { load(); } } </script></head><body onkeydown="keyDown()"> <form id="form1" runat="server"> <div> <input type="text" id="text1" /> <object id="Object1" classid="clsid:82611CA0-938D-4AC8-A6B1-17D4313E8C32" width="100" height="25" style="display:none" codebase="/lib/setup.exe"> </object> <input type="button" value="获取卡号" onclick="load()" /> </div> </form></body></html>
插件源码: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 System.Security;using System.Threading;using mshtml;using System.Reflection;namespace CardReaderControl{ [Guid("82611CA0-938D-4AC8-A6B1-17D4313E8C31")] public partial class ReadCard : UserControl, IObjectSafety { public string InputEnd { get; set; } private IHTMLWindow2 temphtml = null; private string functionstr = ""; public void RegJs(object win, string fuc) { temphtml = (IHTMLWindow2)win; if (temphtml != null && !string.IsNullOrEmpty(fuc)) { functionstr = fuc; } else { temphtml = null; functionstr = ""; MessageBox.Show("注册脚本失败!"); } } #region IObjectSafety 成员 private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}"; private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}"; private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}"; private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}"; private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}"; private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001; private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002; private const int S_OK = 0; private const int E_FAIL = unchecked((int)0x80004005); private const int E_NOINTERFACE = unchecked((int)0x80004002); private bool _fSafeForScripting = true; private bool _fSafeForInitializing = true; public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions) { int Rslt = E_FAIL; string strGUID = riid.ToString("B"); pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; switch (strGUID) { case _IID_IDispatch: case _IID_IDispatchEx: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForScripting == true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForInitializing == true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA; break; default: Rslt = E_NOINTERFACE; break; } return Rslt; } public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions) { int Rslt = E_FAIL; string strGUID = riid.ToString("B"); switch (strGUID) { case _IID_IDispatch: case _IID_IDispatchEx: if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true)) Rslt = S_OK; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true)) Rslt = S_OK; break; default: Rslt = E_NOINTERFACE; break; } return Rslt; } #endregion public ReadCard() { InitializeComponent(); LoadInfo(); } public void LoadInfo() { //获取可用的串口号 foreach (string com in System.IO.Ports.SerialPort.GetPortNames()) { //若串口关闭,则打开 if (serialPort1.IsOpen) { serialPort1.Close(); } //若串口可以打开说明可以用,否则继续查找可用的串口 if (serialPort1.IsOpen) { serialPort1.PortName = com; break; } } serialPort1.BaudRate = 9600; serialPort1.Parity = 0; serialPort1.DataBits = 8; //若串口关闭,则打开 if (!serialPort1.IsOpen) { serialPort1.Open(); } byte[] data = new byte[2]; data[0] = 0x1b; data[1] = 0x5d; serialPort1.Write(data, 0, 2); //触发刷卡事件后执行serialPort1_DataReceived方法,返回卡号 serialPort1.DataReceived =new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived); } void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { try { Thread.Sleep(50);//保证足够的时间读取完整数据 int bytes = serialPort1.BytesToRead; // 创建字节数组 byte[] buffer = new byte[bytes]; // 读取缓冲区的数据到数组 serialPort1.Read(buffer, 0, bytes); string strbuffer = Encoding.ASCII.GetString(buffer);//将二进制转换成字符 string romovestartChar = strbuffer.Substring(2); string rightInfo = romovestartChar.Substring(0, romovestartChar.IndexOf('?')); /*刷完后立即调用页面的js,把卡号传到页面的文本框中,可是下面的语句在在按钮事件中执行,但是在这个事件中执行的 时候提示指定的转化无效,至今没找到解决办法,不明白同样都是事件,为什么它有问题*/ //activex调用js temphtml.execScript(functionstr "('" rightInfo "')", "JScript"); if (serialPort1.IsOpen) { serialPort1.Close(); } } catch (Exception ex) { if (serialPort1.IsOpen) { serialPort1.Close(); } MessageBox.Show(ex.Message); } } //测试activex调用js是否成功 private void button1_Click(object sender, EventArgs e) { try { //经过测试,activex调用js成功,可触发事件后直接把值传到文本框中 //activex调用js temphtml.execScript(functionstr "('读取过来的参数')", "JScript"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } //测试用按钮取出刷卡后的数据,并返回到文本框中 private void button3_Click(object sender, EventArgs e) { try { Thread.Sleep(50);//保证足够的时间读取完整数据 int bytes = serialPort1.BytesToRead; //如果btyes为0,则没刷卡,否则刷卡 if (bytes != 0) { // 创建字节数组 byte[] buffer = new byte[bytes]; // 读取缓冲区的数据到数组 serialPort1.Read(buffer, 0, bytes); string strbuffer = Encoding.ASCII.GetString(buffer);//将二进制转换成字符 string romovestartChar = strbuffer.Substring(2); string rightInfo = romovestartChar.Substring(0, romovestartChar.IndexOf('?')); //activex调用js temphtml.execScript(functionstr "('" rightInfo "')", "JScript"); } else { MessageBox.Show("请刷卡后接受数据"); } if (serialPort1.IsOpen) { serialPort1.Close(); } } catch (Exception ex) { if (serialPort1.IsOpen) { serialPort1.Close(); } MessageBox.Show(ex.Message); } } //用来获取Guid号 private void button4_Click(object sender, EventArgs e) { MessageBox.Show(Guid.NewGuid().ToString()); } }}
评论