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


using System; 
using System.Collections.Generic; 
//using System.Linq; 
using System.Text; 
using System.Collections; 
using System.Windows.Forms; 
 
namespace Modbus数据采集工具 
{ 
    class CsvReader 
    { 
        private ArrayList m_tagList; 
        private string m_modbusIP; 
        private int m_port; 
        private byte m_slaveAddress; 
        public CsvReader(ref ArrayList tagList,string modbusIP,int port, byte slaveAddress) 
        { 
            m_tagList = tagList; 
            m_modbusIP = modbusIP; 
            m_port = port; 
            m_slaveAddress = slaveAddress; 
        } 
 
        public bool ReadCsv() 
        { 
            try 
            { 
                string strPath = System.Environment.CurrentDirectory; 
                strPath += "\\"; 
                strPath += "modbus.csv"; 
                CsvStreamReader csvReader = new CsvStreamReader(strPath); 
 
                for (int i = 2; i <= csvReader.RowCount; i++) 
                { 
                    TAG tag = new TAG(); 
                    tag.modAdd = Convert.ToInt32(csvReader[i, 1]); 
                    switch (csvReader[i, 2]) 
                    { 
                        case "1": 
                            tag.tagType = TAG_TYPE.coilstatus; 
                            break; 
                        case "2": 
                            tag.tagType = TAG_TYPE.inputStatus; 
                            break; 
                        case "3": 
                            tag.tagType = TAG_TYPE.holdingregister; 
                            break; 
                        case "4": 
                            tag.tagType = TAG_TYPE.inputregister; 
                            break; 
                        default: 
                            tag.tagType = TAG_TYPE.other; 
                            break; 
                    } 
 
                    tag.length = Convert.ToInt32(csvReader[i, 3]); 
 
                    switch (csvReader[i, 4]) 
                    { 
                        case "char": 
                            tag.type = typeof(char); 
                            break; 
                        case "int16": 
                            tag.type = typeof(Int16); 
                            break; 
                        case "int": 
                        case "int32": 
                            tag.type = typeof(int); 
                            break; 
                        case "float": 
                            tag.type = typeof(float); 
                            break; 
                        case "double": 
                            tag.type = typeof(double); 
                            break; 
                        default: 
                            break; 
                    } 
 
                    if (tag.tagType != TAG_TYPE.other && tag.type != null) 
                    { 
                        m_tagList.Add(tag); 
                    }    
                } 
            } 
            catch (System.Exception ex) 
            { 
                Console.WriteLine(ex.Message); 
                MessageBox.Show("csv 配置有误!"); 
                return false; 
            } 
            return true; 
        } 
    } 
}