Ejemplo n.º 1
0
        private MCErrorCode ProcessPacket(MCFunction request, out MCFunction response)
        {
            if (request is MCFunctionBatchReadRequest)
            {
                var req = (MCFunctionBatchReadRequest)request;
                var res = new MCFunctionBatchReadResponse(req);

                res.Data = this.Parent.Slave.MemoryMap.Get(req.DeviceCode, req.Offset, req.Count);

                response = res;
                return(MCErrorCode.None);
            }
            else if (request is MCFunctionBatchWriteRequest)
            {
                var req = (MCFunctionBatchWriteRequest)request;
                var res = new MCFunctionBatchWriteResponse(req);

                this.Parent.Slave.MemoryMap.Set(req.DeviceCode, req.Offset, req.Data);

                response = res;
                return(MCErrorCode.None);
            }

            response = null;
            return(MCErrorCode.UnkownRequest);
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
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();
        }