首页 新闻 会员 周边 捐助

串口操作,提示“指定的转化无效”

1
悬赏园豆:15 [待解决问题]
<title>我是title</title>
    <script type="text/javascript">
        window.onload = function () {
            document.getElementById("Object1").RegJs(window, "Show"); //将页面对象,和所需调用的js方法名传给activex
        }

        //activex调用的js方法
        function Show(str) {
            document.getElementById("text1").value = str;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
     <div> 
        
 <object id="Object1" classid="clsid:82611CA0-938D-4AC8-A6B1-17D4313E8C31"   width="500"
        height="100" 
        codebase="/lib/setup.exe">       
    </object>

    <input type="text" id="text1" />
    </div>
    </form>

 

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 UserControl1 : UserControl, IObjectSafety
    {
       
        public string InputEnd
        {
            get;
            set;
        }
        SHDocVw.ShellWindows sws = new SHDocVw.ShellWindows();  
        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 UserControl1()
        {
            InitializeComponent();
           // string test = Show();
          //  InputEnd = test;

            Show();
        }
        static  string aa = "";
        public void Show()
        {
                serialPort1.PortName = "COM1";
                serialPort1.BaudRate = 9600;
                serialPort1.Parity = 0;           
                serialPort1.DataBits = 8;
                serialPort1.Open();
                byte[] data = new byte[2]; 
                data[0] = 0x1b;
                data[1] = 0x5d;
                serialPort1.Write(data, 0, 2);
               
                serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
            
    
          //  return aa;
        }

        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('?'));
              //  aa = rightInfo;
         
            
                //将括号里的值传到页面的文本框中
               // MessageBox.Show(rightInfo);
      
                  temphtml.execScript(functionstr + "('读取过来的参数')", "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
            {
                
                temphtml.execScript(functionstr + "('读取过来的参数')", "JScript");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (SHDocVw.InternetExplorer iw in sws)
            {
                //iw.LocationName是IE窗口标题,也就是当前想更改的页面 <title> </title> 中的值 
                if (iw.LocationName == "我是title")
                {
                    mshtml.HTMLDocument doc = (mshtml.HTMLDocument)iw.Document;


                    //改变HTML控件中ID是b的值   
                  //  (doc.getElementById("b")).innerHTML = "ActiveX改变HTML里控件的值";

                    //调用HTML中的JS函数change(a)   
                    string a = "ActiveX调用JS函数";
                    mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc.parentWindow;
                    win.execScript("show('" + a + "')", "JavaScript");
                }
            }
        }
    
    }
}

我想实现刷卡后立即就可以把卡号显示到页面的文本框中,我写了一个activex插件,用activex调用页面的js,完成赋值,现在的问题是用按钮测试了,可以把activex的值传到文本框中,可是触发刷卡事件后,在事件里写调用js的方法时候就提示指定的转换无效,同样都是事件,为什么按钮事件可以,刷卡事件就不行了呢,我测试了tempHtml不为空,就是调用方法的时候就提示错误,我把页面代码页粘过来

双魂人生的主页 双魂人生 | 初学一级 | 园豆:63
提问于:2012-05-31 10:26
< >
分享
所有回答(2)
1

我也是碰到这样的问题 ,我后来发现“转换无效”是因为我在线程里面调用了js ,如果把它放到主线程(就像你的按钮)那样 就不会 了。

王芳兵 | 园豆:202 (菜鸟二级) | 2013-11-01 09:39
0

我也遇到同样的问题 请问是怎么解决的

维度思源 | 园豆:202 (菜鸟二级) | 2015-02-28 14:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册