www.pudn.com > Modbus.rar > Form1.cs, change:2013-04-22,size:3773b


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.Collections; 
using System.Threading; 
 
namespace Modbus数据采集工具 
{ 
    public partial class Form1 : Form 
    { 
        private ArrayList m_tagList; 
        private Queue m_dataQueue; 
        private AutoResetEvent m_sendEvent; 
        private string m_dstIP; //目标地址 
        private int m_modbusPort;              //modbus端口号 
        private string m_modbusIP;//modbus主机地址 
        private string m_srcIP = "192.168.102.204";//本机地址 
        private byte m_slaveAdd;              //modbus的slave address 
        private int m_startIndex; 
        public static int nTime; 
 
        public Form1() 
        { 
            log4net.Config.XmlConfigurator.Configure(); 
 
            InitializeComponent(); 
            m_tagList = new ArrayList(); 
            m_dataQueue = Queue.Synchronized(new Queue()); 
            m_sendEvent = new AutoResetEvent(false); 
        } 
 
        private void button1_Click(object sender, EventArgs e) 
        {            
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            m_dstIP = "192.168.102.204"; 
            m_modbusPort = 504; 
            m_modbusIP = "192.168.102.211"; 
            m_slaveAdd = 1; 
 
            Ini ini = new Ini("config.ini"); 
            m_dstIP = ini.ReadValue("配置", "目标地址"); 
            m_srcIP = ini.ReadValue("配置", "源地址"); 
            m_modbusPort = Convert.ToInt32(ini.ReadValue("网口modbus", "设备端口")); 
            m_modbusIP = ini.ReadValue("网口modbus", "设备IP"); 
            m_slaveAdd = Convert.ToByte(ini.ReadValue("网口modbus", "slaveAdd")); 
            m_startIndex = Convert.ToInt32(ini.ReadValue("配置", "起始序号")); 
            nTime = Convert.ToInt32(ini.ReadValue("配置", "采集时间间隔")); 
       
            CsvReader reader = new CsvReader(ref m_tagList,m_modbusIP,m_modbusPort,m_slaveAdd); 
            reader.ReadCsv();//读取csv文件  
        } 
 
        private void btnOpenGuangyao_Click(object sender, EventArgs e) 
        { 
            try{ 
				 OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
				  
				 openFileDialog1.Filter = "CSV Files (.csv)|*.csv|All Files (*.*)|*.*"; 
				 openFileDialog1.FilterIndex = 1; 
 
				 if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK){ 
				 } 
			 } 
			 catch(Exception ex){ 
				 MessageBox.Show(ex.ToString()); 
			 } 
        } 
 
        private void btnOpenFarm_Click(object sender, EventArgs e) 
        { 
            try{ 
				 OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
				  
				 openFileDialog1.Filter = "CSV Files (.csv)|*.csv|All Files (*.*)|*.*"; 
				 openFileDialog1.FilterIndex = 1; 
 
				 if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK){ 
				 } 
			 } 
			 catch(Exception ex){ 
				 MessageBox.Show(ex.ToString()); 
			 } 
        } 
 
        private void btnCSVSaveFile_Click(object sender, EventArgs e) 
        { 
            String tmpStr = ""; 
 
			SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
 
			saveFileDialog1.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*"; 
			saveFileDialog1.FilterIndex = 2; 
			saveFileDialog1.RestoreDirectory = true; 
 
			if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK){ 
			    System.IO.StreamWriter sw = new System.IO.StreamWriter(saveFileDialog1.FileName, false, Encoding.GetEncoding("GB2312")); 
				sw.WriteLine(tmpStr); 
				sw.Flush(); 
				sw.Close(); 
			} 
        } 
    } 
}