www.pudn.com > Modbus.rar > modbusReader.cs, change:2013-04-09,size:10023b


using System; 
using System.Collections.Generic; 
/*using*/ //System.Linq; 
using System.Text; 
using System.Collections; 
//using Modbus.Device; 
using System.Net.Sockets; 
using System.Threading; 
using Modbus.Device; 
using log4net; 
 
namespace Modbus数据采集工具 
{ 
    class ModbusReader 
    { 
        public static readonly ILog _log = LogManager.GetLogger(typeof(ModbusReader)); 
 
        private ArrayList m_tagList; 
        private string m_modbusIP; 
        private Queue m_dataQueue; 
        private byte m_slaveAdd; 
        private bool m_bStop = true; 
        private int m_modbusPort = 502; 
 
        public ModbusReader(ref ArrayList tagList, string modbusIP, int modbusPort, ref Queue dataQueue, byte slaveAdd) 
        { 
            m_tagList = tagList; 
            m_modbusIP = modbusIP; 
            m_dataQueue = dataQueue; 
            m_modbusPort = modbusPort; 
            m_slaveAdd = slaveAdd; 
 
            //创建一个线程,采集数据 ReadModbus 
            Thread tReadModbus = null; 
            tReadModbus = new Thread(new ThreadStart(this.Read)); 
            tReadModbus.IsBackground = true; ; 
            tReadModbus.Start(); 
        } 
        public void Read() 
        { 
            while (m_bStop) 
            { 
                try 
                { 
                    _log.DebugFormat("ReadModbus start"); 
                    ReadModbus(); 
                    _log.DebugFormat("ReadModbus end"); 
                } 
                catch (System.Exception ex) 
                { 
                    m_dataQueue.Enqueue(ex.Message); 
                    //Console.WriteLine("{0}\n", ex.Message); 
                    _log.DebugFormat(ex.Message); 
                } 
                Thread.Sleep(Form1.nTime); 
            } 
        } 
        //字节序交换 
        private byte[] Swapf(ref byte[]  objects) 
        { 
 
            byte temp = 0; 
 
            temp = objects[3]; 
            objects[3] = objects[1]; 
            objects[1] = temp; 
 
            temp = objects[2]; 
            objects[2] = objects[0]; 
            objects[0] = temp; 
 
            return objects; 
        } 
 
        //字节序交换 
        private byte[] SwapI(ref byte[] objects) 
        { 
            byte temp = 0; 
 
            temp = objects[2]; 
            objects[2] = objects[0]; 
            objects[0] = temp; 
 
            temp = objects[3]; 
            objects[3] = objects[1]; 
            objects[1] = temp; 
 
            return objects; 
        }  
        public bool ReadModbus() 
        { 
            int n = 0; 
            
            TcpClient client = new TcpClient(m_modbusIP, m_modbusPort); 
            ModbusIpMaster m_master = ModbusIpMaster.CreateTcp(client); 
 
            foreach (object obj in m_tagList) 
            { 
                TAG tag = (TAG)obj; 
 
                switch (tag.tagType) 
                { 
                    #region 读取coilstatus 
                    case TAG_TYPE.coilstatus: 
                        bool[] bRes; 
                        bRes = m_master.ReadCoils(m_slaveAdd, (ushort)tag.modAdd, 1); 
                        if (bRes[0]) 
                        { 
                            tag.value = 1; 
                        } 
                        else 
                        { 
                            tag.value = 0; 
                        } 
                        break; 
                    #endregion 
                    #region 读取inputStatus 
                    case TAG_TYPE.inputStatus: 
                        bRes = m_master.ReadInputs(m_slaveAdd, (ushort)tag.modAdd, 1); 
                        if (bRes[0]) 
                        { 
                            tag.value = 1; 
                        } 
                        else 
                        { 
                            tag.value = 0; 
                        } 
                        break; 
                    #endregion 
                    #region 读取holdingregister 
                    case TAG_TYPE.holdingregister: 
                        { 
                            if (tag.type == typeof(int)) 
                            { 
                                ushort[] uRes; 
                                uRes = m_master.ReadHoldingRegisters(m_slaveAdd, (ushort)tag.modAdd, (ushort)(tag.length / 2)); 
                                byte[] b = new byte[4]; 
                                Buffer.BlockCopy(uRes, 0, b, 0, 4); 
                                _log.DebugFormat("tag.value:{0}", tag.value); 
                                tag.value = BitConverter.ToInt32(SwapI(ref b), 0); 
                            } 
                            else if (tag.type == typeof(Int16)) 
                            { 
                                ushort[] uRes; 
                                uRes = m_master.ReadHoldingRegisters(m_slaveAdd, (ushort)tag.modAdd, (ushort)(tag.length / 2)); 
 
                                byte[] b = new byte[2]; 
                                Buffer.BlockCopy(uRes, 0, b, 0, 2); 
                                _log.DebugFormat("tag.value:{0}", tag.value); 
                                tag.value = BitConverter.ToInt16(b, 0); 
                            } 
                            else if (tag.type == typeof(float)) 
                            { 
                                ushort[] uRes; 
                                uRes = m_master.ReadHoldingRegisters(m_slaveAdd, (ushort)tag.modAdd, (ushort)(tag.length / 2)); 
                                byte[] b = new byte[4]; 
                                Buffer.BlockCopy(uRes, 0, b, 0, 4); 
                                _log.DebugFormat("tag.value:{0}", tag.value); 
                                tag.value = BitConverter.ToSingle(Swapf(ref b), 0); 
                            } 
                            else if (tag.type == typeof(short)) 
                            { 
                                ushort[] uRes; 
                                uRes = m_master.ReadHoldingRegisters(m_slaveAdd, (ushort)tag.modAdd, (ushort)(tag.length / 2)); 
 
                                byte[] b = new byte[2]; 
                                Buffer.BlockCopy(uRes, 0, b, 0, 2); 
                                _log.DebugFormat("tag.value:{0}", tag.value); 
                                tag.value = BitConverter.ToInt16(b, 0); 
                            } 
                            else if (tag.type == typeof(double)) 
                            { 
                                ushort[] uRes; 
                                uRes = m_master.ReadHoldingRegisters(m_slaveAdd, (ushort)tag.modAdd, (ushort)(tag.length / 2)); 
                                 byte[] b = new byte[8]; 
                                Buffer.BlockCopy(uRes, 0, b, 0, 8); 
                                _log.DebugFormat("tag.value:{0}", tag.value); 
                                tag.value = BitConverter.ToDouble(b, 0); 
                            } 
                            break; 
                        } 
                    #endregion 
                    #region 读取inputregister 
                    case TAG_TYPE.inputregister: 
                        { 
                            if (tag.type == typeof(int)) 
                            { 
                                ushort[] uRes; 
                                uRes = m_master.ReadInputRegisters(m_slaveAdd, (ushort)tag.modAdd, (ushort)(tag.length / 2)); 
                                byte[] b = new byte[4]; 
                                Buffer.BlockCopy(uRes, 0, b, 0, 4); 
                                tag.value = BitConverter.ToInt32(b, 0); 
                            } 
                            else if (tag.type == typeof(Int16)) 
                            { 
                                ushort[] uRes; 
                                uRes = m_master.ReadInputRegisters(m_slaveAdd, (ushort)tag.modAdd, (ushort)(tag.length / 2)); 
                                byte[] b = new byte[2]; 
                                Buffer.BlockCopy(uRes, 0, b, 0, 2); 
                                tag.value = BitConverter.ToInt16(b, 0); 
                            } 
                            else if (tag.type == typeof(float)) 
                            { 
                                ushort[] uRes; 
                                uRes =m_master.ReadInputRegisters(m_slaveAdd, (ushort)tag.modAdd, (ushort)(tag.length / 2)); 
                                byte[] b = new byte[4]; 
                                Buffer.BlockCopy(uRes, 0, b, 0, 4); 
                                tag.value = BitConverter.ToSingle(Swapf(ref b), 0); 
                            } 
                            else if (tag.type == typeof(short)) 
                            { 
                                ushort[] uRes; 
                                uRes = m_master.ReadInputRegisters(m_slaveAdd, (ushort)tag.modAdd, (ushort)(tag.length / 2)); 
                                byte[] b = new byte[2]; 
                                Buffer.BlockCopy(uRes, 0, b, 0, 2); 
                                tag.value = BitConverter.ToInt16(b, 0); 
                            } 
                            else if (tag.type == typeof(double)) 
                            { 
                                ushort[] uRes; 
                                uRes =m_master.ReadInputRegisters(m_slaveAdd, (ushort)tag.modAdd, (ushort)(tag.length / 2)); 
                                byte[] b = new byte[8]; 
                                Buffer.BlockCopy(uRes, 0, b, 0, 8); 
                                tag.value = BitConverter.ToDouble(b, 0); 
                            } 
                            break; 
                        } 
                    #endregion 
                } 
                n++; 
            } 
            client.Close(); 
            return true; 
        } 
    } 
 
}