C#.net 数据采集1

这段时间对数据采集忽感兴趣,就在网上查了相关资料,并动手实践。

 

一、添加com引用:Microsoft Xml 3.0;

二、添加using:using MSXML2;  

三、代码:(一个TextBox、两个RichTextBox、两个Button)

 

//以下代码实现对“百度”首页html的获取以及以<div id=m>.+?</div>为例,实现对所采集html的处理。

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using MSXML2;
using System.Text.RegularExpressions;

 

namespace caiji
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

         private void button1_Click(object sender, EventArgs e)
        {//点击按钮读取“百度”首页html
            string url="http://www.baidu.com";
            if(this.textBox1.Text!="")
            {//输入网址
                url = this.textBox1.Text.Trim().ToString();
            }
            string result=this.GetRemoteHtmlCode(url);//调用GetRemoteHtmlCode()方法

            //将结果显示在richTexBox上
            this.richTextBox1.Text = result;

        }
     
        /// 获取远程文件源代码
        /// </summary>
        /// <param name="url">远程url</param>
        /// <returns></returns>
        public string GetRemoteHtmlCode(string Url)
        {
            string s = "";
            MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
            _xmlhttp.open("GET", Url, false, null, null);
            _xmlhttp.send("");
            if (_xmlhttp.readyState == 4)
            {
                s = System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody);
            }
            return s;
        }

        private void button2_Click(object sender, EventArgs e)
        {//以<div id=m>.+?</div>为例,实现对所采集html的处理。
            string Reg = "<div id=m>.+?</div>";
            string str=this.richTextBox1.Text.Trim().ToString();
            string GetValue = this.GetRegValue(Reg,str);
            this.richTextBox2.Text = GetValue;
        }
        public string GetRegValue(string RegexString, string RemoteStr)
        {
            string MatchVale = "";
            Regex r = new Regex(RegexString);
            Match m = r.Match(RemoteStr);
            if (m.Success)
            {
                MatchVale = m.Value;
            }
            return MatchVale;
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个完整的C#西门子数据采集的示例案例,使用了S7.Net库来与西门子PLC进行通信。这个示例演示了如何连接到PLC,读取和写入数据。 ```csharp using System; using S7.Net; class Program { static void Main(string[] args) { // 创建一个连接到PLC的实例 Plc plc = new Plc(CpuType.S71200, "192.168.0.1", 0, 1); try { // 连接到PLC plc.Open(); // 读取PLC中的数据 var data = plc.Read("DB1.DBD10"); // 将数据转换为浮点数 float value = S7.Net.Conversion.ConvertToFloat(data); // 输出读取到的数据 Console.WriteLine("读取到的数据: " + value); // 写入数据到PLC float newValue = 123.45f; byte[] newData = S7.Net.Conversion.ConvertToByteArray(newValue); plc.Write("DB1.DBD10", newData); // 等待一段时间,以便观察写入的效果 System.Threading.Thread.Sleep(500); // 再次读取PLC中的数据 data = plc.Read("DB1.DBD10"); value = S7.Net.Conversion.ConvertToFloat(data); Console.WriteLine("重新读取到的数据: " + value); } catch (Exception ex) { Console.WriteLine("发生异常: " + ex.Message); } finally { // 关闭连接 plc.Close(); } Console.ReadLine(); } } ``` 在这个示例中,我们创建了一个Plc实例来连接到PLC。然后,我们使用Read方法读取PLC中的数据,并使用ConvertToFloat方法将数据转换为浮点数。 接下来,我们使用Write方法将新的数据写入到PLC的指定地址。然后,我们等待一段时间,以便观察写入的效果。 最后,我们再次读取PLC中的数据,以确认写入操作的成功。 请注意,在使用这个示例时,你需要根据实际情况修改IP地址、数据块(DB)和地址(DBD)等。同时,确保你具有与PLC通信所需的权限和凭据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值