WEB页获取串口数据

摘要:
使用System.Text;使用System.Runtime.InteropServices;使用System.Text;使用System.Runtime.InteropServices;使用System.IO.Ports;使用System.Text;使用System.Threading;

最近做一个B/S的项目,需要读取电子秤的值,之前一直没做过,也没有经验,于是在网上找到很多  大致分两种

  1. 使用ActiveX控件,JS调用MSCOMM32.dll的串口控件对串口进行控制
  2. 使用C#语言的控件对串口进行控制,然后使用JS+AJAX与C#进行交互获得串口数据

详情见  使用JS获得串口数据 http://blog.csdn.net/xuing/article/details/6688306    但是小弟用这两种办法都获取到数据

串口配置如下:

1                  serialPort1.PortName = "COM1";          //端口名称
2             serialPort1.BaudRate = 1200;            //波特率
3             serialPort1.Parity = Parity.None;       //奇偶效验
4             serialPort1.StopBits = StopBits.One;    //效验
5             serialPort1.DataBits = 8;               //每个字节的数据位长度

最后换种思路:使用C#写一个ActiveX控件(吉日老师提醒)最后嵌入网页中读取数据   如下:

  1. 第一步:新建项目,如下图,选择windows下的类库项目。WEB页获取串口数据第1张
  2. 在项目中添加一个类:IObjectSafety.cs WEB页获取串口数据第2张
  3. IObjectSafety.cs代码如下:
    WEB页获取串口数据第3张WEB页获取串口数据第4张
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    namespace MyActive
    {
        //Guid唯一,不可变更,否则将无法通过IE浏览器的ActiveX控件的安全认证  
        [ComImport, Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IObjectSafety
        {
            [PreserveSig]
            void GetInterfacceSafyOptions(int riid,out int pdwSupportedOptions,out int pdwEnabledOptions);
        }
    }
    View Code
  4. 添加一个用户控件 MyActiveXControl.cs WEB页获取串口数据第5张
  5. 修改 MyActiveXControl.cs 代码,让其继承IObjectSafety,定义相应的Guid,该Guid就是ActiveX的classid 
    WEB页获取串口数据第6张WEB页获取串口数据第7张
    using System;
     using System.Collections.Generic;
     using System.ComponentModel;
     using System.Drawing;
     using System.Data;
     using System.Text;
     using System.Windows.Forms;
     using System.Runtime.InteropServices;
    
    namespace MyActiveX
     {
         [Guid("218849AF-1B2C-457B-ACD5-B42AC8D17EB7"), ComVisible(true)]
         public partial class MyActiveXControl : UserControl,IObjectSafety
         {
             public MyActiveXControl()
             {
                 InitializeComponent();
             }
    
            #region IObjectSafety 成员 用于ActiveX控件安全信任
            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
         }
     }
    View Code

    至此   Active控件制作完毕      下面我们添加文本框、按钮、SerialPort、Timer控件进行测试                                    
    WEB页获取串口数据第8张WEB页获取串口数据第9张

  6. 添加响应的事件代码如下
    WEB页获取串口数据第10张WEB页获取串口数据第11张
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.IO.Ports;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using MyActive;
    
    namespace MyActiveX
    {
        //不可改变
        [Guid("218849AF-1B2C-457B-ACD5-B42AC8D17EB7"), ComVisible(true)]
        public partial class MyActiveXControl : UserControl, IObjectSafety
        {
            
            public MyActiveXControl()
            {
                InitializeComponent();
            }
    
            public delegate void HandleInterfaceUpdataDelegate(string text);//定义一个委托
                         
            private HandleInterfaceUpdataDelegate interfaceUpdataHandle;//声明
    
            bool isClose = false;//是否关闭
    
            #region IObjectSafety 成员 用于ActiveX控件安全信任
            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 button1_Click(object sender, EventArgs e)
            {
                try
                {
                    interfaceUpdataHandle = new HandleInterfaceUpdataDelegate(UpdateTextBox);//实例化委托对象 
    
                    serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                    if (!serialPort1.IsOpen)
                    {
                        serialPort1.Open();
                    }
                    button2.Enabled = true;
                    button1.Enabled=false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
                timer1.Enabled = true;
                
            }
            /// <summary>
            /// 控件加载事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void MyActiveXControl_Load(object sender, EventArgs e)
            {
                setOrgComb();
            }
            /// <summary>
            /// 初始化串口
            /// </summary>
            private void setOrgComb()
            {
                serialPort1.PortName = "COM1";          //端口名称
                serialPort1.BaudRate = 1200;            //波特率
                serialPort1.Parity = Parity.None;       //奇偶效验
                serialPort1.StopBits = StopBits.One;    //效验
                serialPort1.DataBits = 8;               //每个字节的数据位长度
            }
            /// <summary>
            /// 更新数据
            /// </summary>
            /// <param name="text"></param>
            private void UpdateTextBox(string text)
            {
                //richTextBox1.Text = text + "
    	" + richTextBox1.Text;
                richTextBox1.Text = text;
            }
    
            /// <summary>
            /// 接收数据是发生
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                //获取接收缓冲区中数据的字节数
                if (serialPort1.BytesToRead > 5)
                {
    
                    string strTemp = serialPort1.ReadExisting();//读取串口
                    double weight = -1;//获取到的重量
                    foreach (string str in strTemp.Split('='))//获取稳定的值
                    {
                        double flog = 0;
                        //数据是否正常
                        if(double.TryParse(str, out flog)&&str.IndexOf('.')>0&&str[str.Length-1]!='.')
                        {
                            //数据转换   串口获取到的数据是倒叙的  因此进行反转
                            char[] charArray = str.ToCharArray();
                            Array.Reverse(charArray);
                            string left = new string(charArray).Split('.')[0];
                            string right = new string(charArray).Split('.')[1];
                            if (right.Length==2)
                            {
                                weight = int.Parse(left) + int.Parse(right) / 100.0;
                            }
                        }
                    }
                    if(weight>=0)
                    {
                        //在拥有控件的基础窗口句柄的线程上,用指定的参数列表执行指定委托。                    
                        this.Invoke(interfaceUpdataHandle, weight.ToString());//取到数据   更新
                    }
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    button1.Enabled = true;
                    button2.Enabled = false;
                    serialPort1.Close();
                    timer1.Enabled = false;
                   
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (isClose)
                {
                    return;
                }
                try
                {
                    string send = "" + (char)(27) + 'p';
                    send = serialPort1.ReadExisting();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    button2_Click(null, null);
                }
            }
        }
    }
    View Code


     

  7. 至此读取串口数据的Active控件制作完毕   下面我们来制作一个安装包,新建一个安装项目WEB页获取串口数据第12张
  8. 在安装项目的文件系统中添加刚才之前我们制作的ActiveX的DLL:MyActiveX.dll
    (特别注意:在文件添加进来后,右击文件选择属性,设置其属性Register值为:vsdraCOM)

 WEB页获取串口数据第13张

    9.    生成安装程序,在项目MyActiveXSetup1Debug下找到Setup1.msi,双击安装它。
         然后在该目录下新建一个html文件(test.html)用于测试我们的ActiceX控件。HTML代码如下:

WEB页获取串口数据第14张WEB页获取串口数据第15张
<html>
 <title>Powered by yyzq.net Email:yq@yyzq.net</title>
 <head>
 </head>
 <body>
 <div>
 <object id="yyzq" classid="clsid:218849AF-1B2C-457B-ACD5-B42AC8D17EB7"
         width="320"
         height="240"
         codebase="Setup1.msi">
 </object>
 </div>
 </body>
 </html>
View Code

在IE浏览器下打开test.html,点击start     开始监听

WEB页获取串口数据第16张

 WEB页获取串口数据第17张

免责声明:文章转载自《WEB页获取串口数据》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇游戏攻略 Re:LieF ~親愛なるあなたへ~ (relief给挚爱的你)ASP.NET MVC 视图(二)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

微信小程序自定义组件-下拉框

这个是网址https://www.cnblogs.com/zjjDaily/p/9548433.html 微信小程序之自定义select下拉选项框组件知识点:组件,animation,获取当前点击元素的索引与内容 微信小程序中没有select下拉选项框,所以只有自定义。自定义的话,可以选择模板的方式,也可以选择组件的方式来创建。 这次我选择了组件,这样只需...

Apache CarbonData 2.0 开发实用系列之一:与Spark SQL集成使用

【摘要】 在Spark SQL中使用CarbonData 【准备CarbonData】 在浏览器地址栏输入以下链接,点击"download"按钮下载已经准备好的CarbonData jar包 链接:https://github.com/QiangCai/carbonjars/blob/master/master/apache-carbondata-2.1....

Windows编程系列:Windows中的消息

win32控制台程序 控制台程序整个执行过程是按照代码的顺序依次执行,到main函数的结束,标志着整个程序的退出。 1 int main() 2 { 3 4 return 0; 5 } 整个过程可以描述为以下: Windows应用程序 Windows应用程序会响应来自用户和操作系统的事件。 来自用户的事件包括:鼠标单击,按键,触摸屏手势...

SQL Server 一些使用小技巧

1、查询的时候把某一个字段的值拼接成字符串 以下是演示数据。 第一种方式:使用自定义变量 DECLARE @Names NVARCHAR(128) SET @Names='' -- 需要先赋值为空字符串,不然结果会是 null SELECT @Names=@Names+S_Name+',' -- S_Name 类型为...

mysq优化三之buffer pool

sql语句执行流程   不管是select还是update,都是要查询把页取出来放在内存中,mysql中有一个单独的区域用来存放页,这就是buffer pool innodb architecture  mysql启动的时候,会在内存中开辟一个128M的空间,这个空间就是buffer pool  当再取一个页放在buffer pool中的什么位置呢...

集成WPF与Windows窗体

下载source - 1.92 MB 介绍 本文讨论如何将WPF XPS文档查看器集成到Windows窗体应用程序中。 “WPF给我们带来了一个美好的未来,我们都应该开始使用它,把我们所有的产品都换成它。” 这是个好主意,但对我们大多数人来说,这是不可能的。例如,我们的主要产品有成千上万行代码和数百个表单。把所有这些都扔进垃圾桶,然后重新开始,这将是经济上...