Ejemplo n.º 1
1
        public void RTU_ADU_DocSampleReq()
        {
            var DocRequest = new byte[]
            {
                0x01,
                0x10, 0x00, 0x01, 0x00, 0x01, 0x02, 0xFF, 0xFF,
                0xA6, 0x31,
            };

            var DocResponse = new byte[]
            {
                0x01,
                0x10, 0x00, 0x01, 0x00, 0x01,
                0x50, 0x09,
            };

            var ClientPDU = new WriteMultipleRegistersPDU(1, new int[] { 0xFFFF });
            var Device = new RTU_Device(1);

            var ClientADU = new RTU_ADU(Device, ClientPDU);

            CollectionAssert.AreEqual(DocRequest, ClientADU.BuildRequest());

            Assert.AreEqual(Status.OK, ClientADU.CheckResponse(DocResponse));

            

        }
Ejemplo n.º 2
0
        public int[] ReadInputRegisters(IModbusDevice Device, int StartAddr, int Quantity)
        {
            var PDU = new ReadInputRegistersPDU(StartAddr, Quantity);
            var ADU = new RTU_ADU((Device as RTU_Device), PDU);

            Query(ADU);
            return(PDU.Value);
        }
Ejemplo n.º 3
0
        public void RTU_ADU_LifeCycle()
        {
            var ClientPDU = new ReadHoldingRegistersPDU(5, 10);
            var Device = new RTU_Device(1);
            var ClientADU = new RTU_ADU(Device, ClientPDU);

            var ServerADU = new RTU_ADU(Device);

            ServerADU.CheckRequest(ClientADU.BuildRequest());
        }
Ejemplo n.º 4
0
        public bool[] ReadCoils(IModbusDevice Device, int StartAddr, int Quantity)
        {
            if (!(Device is RTU_Device))
                throw new ArgumentException("Wrong Type", "Device");

            var PDU = new ReadCoilsPDU(StartAddr, Quantity);
            var ADU = new RTU_ADU((Device as RTU_Device), PDU);

            Query(ADU);

            return PDU.Value;
        }
Ejemplo n.º 5
0
        public void WriteMultipleRegisters(IModbusDevice Device, int StartAddr, int[] Value)
        {
            if (!(Device is RTU_Device))
            {
                throw new ArgumentException("Wrong Type", "Device");
            }

            var PDU = new WriteMultipleRegistersPDU(StartAddr, Value);
            var ADU = new RTU_ADU((Device as RTU_Device), PDU);

            Query(ADU);
        }
Ejemplo n.º 6
0
        public void WriteSingleCoil(IModbusDevice Device, int StartAddr, bool Value)
        {
            if (!(Device is RTU_Device))
            {
                throw new ArgumentException("Wrong Type", "Device");
            }

            var PDU = new WriteSingleCoilPDU(StartAddr, Value);
            var ADU = new RTU_ADU((Device as RTU_Device), PDU);

            Query(ADU);
        }
Ejemplo n.º 7
0
        void Listen()
        {
            var Device = new RTU_Device(Address);

            
            Buffer = new List<byte>();

            try
            {
                while (true)
                {
                    var ADU = new RTU_ADU(Device);

                    Port.DiscardInBuffer();
                    Buffer.Clear();

                    #region Recieve first byte
                    Port.ReadTimeout = SerialPort.InfiniteTimeout;

                    Buffer.Add((byte)Port.ReadByte());
                    //ByteRecievedEvent(Buffer.Last());

                    #endregion

                    #region Recieve All bytes
                    Port.ReadTimeout = ByteTimeout;


                    try
                    {
                        while(true)
                            Buffer.Add((byte)Port.ReadByte());
                            //ByteRecievedEvent(Buffer.Last());
                        
                    }
                    catch (TimeoutException) { }
                    #endregion

                    #region Handle Req
                    if ((ADU.CheckRequest(Buffer.ToArray())) == Status.OK)
                    {
                        ADU.PDU.Handle(this);
                        var Response = ADU.BuildResponse();
                        Port.Write(Response, 0, Response.Length);
                    }
                    #endregion

                }
            }
            catch (IOException)
            { }

          }
Ejemplo n.º 8
0
        void Listen()
        {
            var Device = new RTU_Device(Address);


            Buffer = new List <byte>();

            try
            {
                while (true)
                {
                    var ADU = new RTU_ADU(Device);

                    Port.DiscardInBuffer();
                    Buffer.Clear();

                    #region Recieve first byte
                    Port.ReadTimeout = SerialPort.InfiniteTimeout;

                    Buffer.Add((byte)Port.ReadByte());
                    //ByteRecievedEvent(Buffer.Last());

                    #endregion

                    #region Recieve All bytes
                    Port.ReadTimeout = ByteTimeout;


                    try
                    {
                        while (true)
                        {
                            Buffer.Add((byte)Port.ReadByte());
                        }
                        //ByteRecievedEvent(Buffer.Last());
                    }
                    catch (TimeoutException) { }
                    #endregion

                    #region Handle Req
                    if ((ADU.CheckRequest(Buffer.ToArray())) == Status.OK)
                    {
                        ADU.PDU.Handle(this);
                        var Response = ADU.BuildResponse();
                        Port.Write(Response, 0, Response.Length);
                    }
                    #endregion
                }
            }
            catch (IOException)
            { }
        }
Ejemplo n.º 9
0
        public bool[] ReadDiscreteInputs(IModbusDevice Device, int StartAddr, int Quantity)
        {
            if (!(Device is RTU_Device))
            {
                throw new ArgumentException("Wrong Type", "Device");
            }

            var PDU = new ReadDiscreteInputsPDU(StartAddr, Quantity);
            var ADU = new RTU_ADU((Device as RTU_Device), PDU);

            Query(ADU);
            return(PDU.Value);
        }
Ejemplo n.º 10
0
        private Status Query(RTU_ADU ADU)
        {
            lock (Port)
            {
                var Request = ADU.BuildRequest();
                var Count = 0;

                try
                {

                    do
                    {

                        Port.DiscardInBuffer();
                        Buffer.Clear();

                        Port.Write(Request, 0, ADU.ReqSize);
                        ReqSendEvent(ADU.BuildRequest());

                        if (!ADU.Device.Broadcast)
                        {
                            Port.ReadTimeout = Timeout;

                            #region Wait for first byte
                            try
                            {
                                Buffer.Add((byte)Port.ReadByte());
                                ByteRecievedEvent(Buffer.Last());
                            }
                            catch (TimeoutException)
                            {
                                Count++;
                                continue;
                            }
                            #endregion

                            Port.ReadTimeout = ByteTimeout;

                            #region Recieve All bytes
                            try
                            {
                                for (int i = 0; i < ADU.RespSize - 1; i++)
                                {
                                    Buffer.Add((byte)Port.ReadByte());
                                    ByteRecievedEvent(Buffer.Last());
                                }
                            }
                            catch (TimeoutException) { }
                            #endregion

                            switch (ADU.CheckResponse(Buffer.ToArray()))
                            {
                                case Status.OK: Thread.Sleep(4); return Status.OK;
                                case Status.Exception: throw new ModbusException(ADU.Device.Address, FunctionCode.WriteMultipleRegisters, (ExceptionType)ADU.PDU.ExceptionCode);
                                case Status.Error: Thread.Sleep(4); Count++; break;
                            }
                        }
                        else
                            return Status.OK;

                    } while (Count < MaxCount);
                }
                catch (IOException ex) { throw new RTU_Exception("Ошибка порта", ex); }
                throw new ModbusException(ADU.Device.Address, FunctionCode.WriteMultipleRegisters, (ExceptionType)ADU.PDU.ExceptionCode);
            }

        }
Ejemplo n.º 11
0
        public void WriteMultipleRegisters(IModbusDevice Device, int StartAddr, int[] Value)
        {
            if (!(Device is RTU_Device))
                throw new ArgumentException("Wrong Type", "Device");

            var PDU = new WriteMultipleRegistersPDU(StartAddr, Value);
            var ADU = new RTU_ADU((Device as RTU_Device), PDU);

            Query(ADU) ;

       
        }
Ejemplo n.º 12
0
        public void WriteSingleCoil(IModbusDevice Device, int StartAddr, bool Value)
        {
            if (!(Device is RTU_Device))
                throw new ArgumentException("Wrong Type", "Device");

            var PDU = new WriteSingleCoilPDU(StartAddr, Value);
            var ADU = new RTU_ADU((Device as RTU_Device), PDU);
            Query(ADU);
        }
Ejemplo n.º 13
0
 public int[] ReadInputRegisters(IModbusDevice Device, int StartAddr, int Quantity)
 {
     var PDU = new ReadInputRegistersPDU(StartAddr, Quantity);
     var ADU = new RTU_ADU((Device as RTU_Device), PDU);
     Query(ADU);
     return PDU.Value;
 }
Ejemplo n.º 14
0
        private Status Query(RTU_ADU ADU)
        {
            lock (Port)
            {
                var Request = ADU.BuildRequest();
                var Count   = 0;

                try
                {
                    do
                    {
                        Port.DiscardInBuffer();
                        Buffer.Clear();

                        Port.Write(Request, 0, ADU.ReqSize);
                        ReqSendEvent(ADU.BuildRequest());

                        if (!ADU.Device.Broadcast)
                        {
                            Port.ReadTimeout = Timeout;

                            #region Wait for first byte
                            try
                            {
                                Buffer.Add((byte)Port.ReadByte());
                                ByteRecievedEvent(Buffer.Last());
                            }
                            catch (TimeoutException)
                            {
                                Count++;
                                continue;
                            }
                            #endregion

                            Port.ReadTimeout = ByteTimeout;

                            #region Recieve All bytes
                            try
                            {
                                for (int i = 0; i < ADU.RespSize - 1; i++)
                                {
                                    Buffer.Add((byte)Port.ReadByte());
                                    ByteRecievedEvent(Buffer.Last());
                                }
                            }
                            catch (TimeoutException) { }
                            #endregion

                            switch (ADU.CheckResponse(Buffer.ToArray()))
                            {
                            case Status.OK: Thread.Sleep(4); return(Status.OK);

                            case Status.Exception: throw new ModbusException(ADU.Device.Address, FunctionCode.WriteMultipleRegisters, (ExceptionType)ADU.PDU.ExceptionCode);

                            case Status.Error: Thread.Sleep(4); Count++; break;
                            }
                        }
                        else
                        {
                            return(Status.OK);
                        }
                    } while (Count < MaxCount);
                }
                catch (IOException ex) { throw new RTU_Exception("Ошибка порта", ex); }
                throw new ModbusException(ADU.Device.Address, FunctionCode.WriteMultipleRegisters, (ExceptionType)ADU.PDU.ExceptionCode);
            }
        }