采用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();
}
}
}
楼主最近还看过