Example #1
0
        private MCFunction GetResponseFunction(MCQHeader qHeader, MCErrorCode errorCode, MCFunction request, MCFunction response)
        {
            if (errorCode == MCErrorCode.None)
            {
                return(response);
            }
            else
            {
                var f = new MCFunctionErrorResponse();
                f.IONumber      = qHeader.IONumber;
                f.NetworkNumber = qHeader.NetworkNumber;
                f.PLCNumber     = qHeader.PLCNumber;
                f.StationNumber = qHeader.PLCNumber;

                if (request != null)
                {
                    f.CommandCode    = MCFunctionRegistry.Find(request.GetType()).Id;
                    f.SubCommandCode = request.GetSubCommandCode();
                }
                else
                {
                    f.CommandCode    = 0;
                    f.SubCommandCode = 0;
                }

                return(f);
            }
        }
Example #2
0
        private MCQHeaderResponse CreateResponseQHeader(MCQHeader request, MCErrorCode errorCode)
        {
            var value = new MCQHeaderResponse();

            value.IONumber      = request.IONumber;
            value.NetworkNumber = request.NetworkNumber;
            value.PLCNumber     = request.PLCNumber;
            value.StationNumber = request.PLCNumber;
            value.ResultCode    = (ushort)errorCode;

            return(value);
        }
Example #3
0
 public MCMemoryException(string message, MCErrorCode errorCode)
     : base(message, errorCode)
 {
 }
Example #4
0
        private void Connection()
        {
            try
            {
                while (true)
                {
                    using (var sw = new StreamWrapper(this.Stream))
                    {
                        try
                        {
                            MCPacket    request          = null;
                            MCErrorCode errorCode        = MCErrorCode.None;
                            MCFunction  responseFunction = null;

                            try
                            {
                                request   = this.Parent.Slave.Protocol.ReadPacket(sw, null);
                                errorCode = this.ProcessPacket(request.Function, out responseFunction);
                            }
                            catch (MCProtocolException e)
                            {
                                errorCode        = e.ErrorCode;
                                responseFunction = null;
                            }
                            catch (IOException)
                            {
                                throw;
                            }
                            catch (ThreadInterruptedException)
                            {
                                throw;
                            }
                            catch (Exception)
                            {
                                errorCode        = MCErrorCode.UnkownRequest;
                                responseFunction = null;
                            }

                            var response = new MCPacket();
                            response.SubHeader = request.SubHeader;
                            response.QHeader   = this.CreateResponseQHeader(request.QHeader, errorCode);
                            response.Function  = this.GetResponseFunction(request.QHeader, errorCode, request.Function, responseFunction);
                            this.Parent.Slave.Protocol.WritePacket(sw, response);
                        }
                        finally
                        {
                            var mco   = this.Parent.Slave;
                            var datas = mco.MemoryMap.Get(DeviceCode.D, 500, 32);
                            Console.Clear();

                            Console.WriteLine("status : " + datas[3]);

                            var position = new BitNumber2Words(new BitShort(), new BitShort());
                            position.UpperWord.Raw32 = datas[4];
                            position.LowerWord.Raw32 = datas[5];
                            Console.WriteLine("Position : " + position.Value);

                            var force = new BitNumber2Words(new BitShort(), new BitShort());
                            force.UpperWord.Raw32 = datas[6];
                            force.LowerWord.Raw32 = datas[7];
                            Console.WriteLine("Force : " + force.Value);

                            for (int i = 0; i < datas.Length; i++)
                            {
                                //Console.WriteLine(i.ToString("D2") + " : " + datas[i].ToString("X4"));
                            }

                            //this.Logger.OnCommunicationLog(sw.Log);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                this.Logger.OnMessageLog(e.ToString());
            }

            this.Close();
        }
Example #5
0
 public MCProtocolException(string message, MCErrorCode errorCode)
     : base(message)
 {
     this.ErrorCode = errorCode;
 }