c# ModbusTcp 与台达RTU-EN01通讯程序 点击:3965 | 回复:8



fireT

    
  • 精华:0帖
  • 求助:2帖
  • 帖子:16帖 | 55回
  • 年度积分:19
  • 历史总积分:589
  • 注册:2009年2月07日
发表于:2012-02-09 15:18:05
楼主

采用vs2010  c#做的一个SCADA系统.其中有一个MODBUS TCP采集现场传感器状态,然后保存到SQL2005数据库中,现在将部分程序分享,不足之处还请指正。采用轮询绝对能用!

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 System.Net.Sockets;
using System.Threading;
namespace ModbusTcp
{
    public partial class Form1 : Form
    {
        # region 变量
        SQLHelper.HXDataExecute_SQL mysql = new SQLHelper.HXDataExecute_SQL(true);
        ModbusHelper.Modbus mymodbus;
        Thread thread1;
        #endregion

        #region 属性

        #endregion

        #region 函数
        void AddListbox(string messages)
        {
            lock (this)
            {
                listBox1.Items.Add(DateTime.Now.ToLongTimeString() + "    " + messages);
                listBox1.SelectedIndex = listBox1.Items.Count - 1;
                if (listBox1.Items.Count >100)
                {
                    listBox1.Items.Clear();
                }
            }
        }
        void thread()
        {
            //Thread1();
            while (true)
            {
                Thread2();
                //Thread.Sleep(500);
                Thread3();
                //Thread.Sleep(500);
                Thread4();
                //Thread.Sleep(500);
                Thread5();
                //Thread.Sleep(500);
            }
        }
        void Thread1()
        {
            try
            {
                AddListbox("启动线程1");
                //byte【】 SendBuf = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x06, 0x05, 0x02, 0x05, 0x00, 0x00, 0x50};
                mymodbus=new ModbusHelper.Modbus("192.168.1.15",502);
                byte【】 SendBuf = mymodbus.CreateCommand_TCP("05", "02", "0500", "80");
                byte【】 RecBuf = new byte【19】;
                AddListbox("192.168.1.15开始连接!");
                TcpClient newclient = new TcpClient();

                newclient.Connect("192.168.1.15", 502);
                if (newclient.Connected)
                {
                    NetworkStream ns = mymodbus.myTcpClient.GetStream();
                    AddListbox("192.168.1.15连接成功!");
                    string rec = string.Empty;
                    while (true)
                    {
                        rec = string.Empty;
                        ns.Write(SendBuf, 0, SendBuf.Length);
                        ns.Read(RecBuf, 0, RecBuf.Length);
                        for (int i = 9; i < RecBuf.Length; i++)
                        {
                            //rec += RecBuf【i】.ToString() + ",";
                            string sss = "00000000" + Convert.ToString(RecBuf【i】, 2);
                            sss = sss.Substring(sss.Length - 8, 8);
                            for (int s = 0; s < sss.Length; s++)
                            {
                                rec += sss.Substring(7 - s, 1);
                            }
                        }
                        //AddListbox(RecBuf.Length.ToString());
                        AddListbox("数量"+rec.Length +":"+rec );
                        //Thread.Sleep(1000);
                    }
                }
                else
                {
                    AddListbox("192.168.1.15连接失败!");
                }
            }
            catch (Exception ex)
            {
                AddListbox(ex.Message+mymodbus.ErrMessages );
            }
        }
        void Thread2()
        {
           
                mymodbus = new ModbusHelper.Modbus("192.168.1.15", 502);
                string recstring = string.Empty;
                string sata = mymodbus.GetBit_Output_TCP(05, 0x0500, 80, out recstring);
                if (sata == "0")
                {
                    AddListbox(recstring.Length.ToString()+","+ recstring );

                }
                else
                {
                    AddListbox(sata);
                }
           
        }
        void Thread3()
        {
           
                try
                {
                    mymodbus = new ModbusHelper.Modbus("192.168.1.15", 502);
                    int【】 recstring = null;
                    string sata = mymodbus.GetRegister_Keeping_TCP (05, 0x1600, 10, out recstring);
                    if (sata == "0")
                    {
                        AddListbox(recstring.Length.ToString()+":"+mymodbus.intToString(recstring) );

                    }
                    else
                    {
                        AddListbox(sata);
                    }
                }
                catch (Exception ex)
                {
                    AddListbox(ex.Message);
                }
          
        }
        void Thread4()
        {
            try
            {
                mymodbus = new ModbusHelper.Modbus("192.168.1.15", 502);
                if (mymodbus.SetSingleBit_TCP (05, 0x0502, 1))
                {
                    AddListbox("0x0502" + "打开成功");
                }
                else
                {
                    AddListbox(mymodbus.ErrMessages);
                }
            }
            catch (Exception ex)
            {
                AddListbox(ex.Message);
            }
        }
        void Thread5()
        {
            try
            {
                mymodbus = new ModbusHelper.Modbus("192.168.1.15", 502);
               // for (int s = 0; s < 100; s++)
                //{
                    if (mymodbus.SetSingleRegister_TCP(05, 0x1601, 35))
                    {
                        AddListbox("0x1601" + "操作成功");
                        //Thread.Sleep(1000);
                    }

                    else
                    {
                        AddListbox(mymodbus.ErrMessages);
                    }
               // }
            }
            catch (Exception ex)
            {
                AddListbox(ex.Message);
            }
        }
        #endregion

        public Form1()
        {
            InitializeComponent();
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            //不是退出程序则到系统托盘  
            this.Dispose();
            System.Environment.Exit(0);
            e.Cancel = true;
            this.Visible = false;

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //int  ss = 1281;
            //MessageBox.Show(ss.ToString("X"));

            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            thread1 = new Thread(new ThreadStart(thread));
            thread1.IsBackground = false;
            thread1.Start();
        }
    }
}

 



楼主最近还看过



水中鱼

  • 精华:43帖
  • 求助:4帖
  • 帖子:324帖 | 3802回
  • 年度积分:0
  • 历史总积分:15038
  • 注册:2011年11月10日
发表于:2012-02-09 20:44:28
1楼

感谢楼主分享,支持!!

hpulizhong

  • 精华:0帖
  • 求助:0帖
  • 帖子:1帖 | 350回
  • 年度积分:0
  • 历史总积分:1282
  • 注册:2011年2月24日
发表于:2015-10-18 20:52:59
2楼

非常感谢楼主分享,楼主真是太慷慨了!!

一朵花

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 372回
  • 年度积分:0
  • 历史总积分:108
  • 注册:2014年4月10日
发表于:2017-10-18 14:24:19
3楼

感谢楼主分享,支持!!

宁雪晶

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 53回
  • 年度积分:0
  • 历史总积分:22
  • 注册:2016年4月05日
发表于:2017-11-16 23:19:05
4楼

6666666666666666666666666666

小逸爸爸

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 479回
  • 年度积分:0
  • 历史总积分:276
  • 注册:2016年1月27日
发表于:2019-04-09 13:30:09
5楼

xie xie fen xiang

小逸爸爸

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 479回
  • 年度积分:0
  • 历史总积分:276
  • 注册:2016年1月27日
发表于:2019-05-16 15:44:40
6楼

感谢楼主分享,支持!!

涅灭

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 121回
  • 年度积分:0
  • 历史总积分:144
  • 注册:2018年9月30日
发表于:2019-06-28 13:52:05
7楼

感谢楼主分享,支持!!

Druid

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 115回
  • 年度积分:0
  • 历史总积分:67
  • 注册:2011年1月28日
发表于:2020-06-17 11:25:03
8楼

感谢分享,学习学习


热门招聘
相关主题

官方公众号

智造工程师