private void Specification(ModbusPkt pkt)
        {
            bool success = false;

            switch (pkt.PrinterSpec)
            {
            case ccPS.Character_Height:
            case ccPS.Character_Width:
            case ccPS.Repeat_Interval:
                int st = MB.GetDecAttribute(ccUS.Operation_Status);
                if (st >= 0x32)  // 0x30 and 0x31 indicate already in standby
                {
                    success = MB.SetAttribute(ccIJP.Remote_operation, (int)RemoteOps.StandBy);
                    success = MB.SetAttribute(pkt.PrinterSpec, pkt.Data);
                    success = MB.SetAttribute(ccIJP.Remote_operation, (int)RemoteOps.Ready);
                }
                else
                {
                    success = MB.SetAttribute(pkt.PrinterSpec, pkt.Data);
                }
                break;

            default:
                break;
            }
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                Success = success
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void GetErrors(ModbusPkt pkt)
        {
            int errCount = MB.GetDecAttribute(ccAH.Message_Count);

            string[] errs = new string[errCount];
            AttrData attr = MB.GetAttrData(ccAH.Year);
            int      len  = attr.Stride;

            for (int i = 0; i < errCount; i++)
            {
                MB.GetAttribute(Modbus.FunctionCode.ReadInput, 1, attr.Val + i * len, len * 2, out byte[] data);
                int year   = (data[0] << 8) + data[1];
                int month  = data[3];
                int day    = data[5];
                int hour   = data[7];
                int minute = data[9];
                int second = data[11];
                int fault  = (data[12] << 8) + data[13];
                errs[i] = $"{fault:###} {year}/{month:##}/{day:##} {hour:##}:{minute:##}:{second:##}";
            }
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                MultiLine = errs, Value = errCount
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void GetErrors(ModbusPkt pkt)
        {
            int errCount = MB.GetDecAttribute(ccAH.Message_Count);

            string[]       errs = new string[errCount];
            AttrData       attr = MB.GetAttrData(ccAH.Year);
            Section <ccAH> hist = new Section <ccAH>(MB, attr, 0, errCount * attr.Stride, true);

            byte[] data = new byte[errCount * attr.Stride * 2];
            Buffer.BlockCopy(hist.b, 0, data, 0, data.Length);
            int n = 0;

            for (int i = 0; i < errCount * attr.Stride * 2; i += attr.Stride * 2)
            {
                int year   = (data[i + 0] << 8) + data[i + 1];
                int month  = data[i + 3];
                int day    = data[i + 5];
                int hour   = data[i + 7];
                int minute = data[i + 9];
                int second = data[i + 11];
                int fault  = (data[i + 12] << 8) + data[i + 13];
                errs[n++] = $"{fault:D3} {year}/{month:D2}/{day:D2} {hour:D2}:{minute:D2}:{second:D2}";
            }
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                MultiLine = errs, Value = errCount
            };

            parent.BeginInvoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void GetStatus(ModbusPkt pkt)
        {
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                Status = GetStatus(),
            };

            parent.BeginInvoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void Idle(ModbusPkt pkt)
        {
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                Success = true, Resp1 = pkt.Marker
            };

            parent.BeginInvoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void AckOnly(ModbusPkt pkt)
        {
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                Success = true
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void Connect(ModbusPkt pkt)
        {
            bool          success = MB.Connect(pkt.IpAddress, pkt.IpPort);
            AsyncComplete ac      = new AsyncComplete(MB, pkt)
            {
                Success = success
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void ReadData(ModbusPkt pkt)
        {
            MB.GetAttribute(pkt.fc, pkt.DevAddr, pkt.Addr, pkt.Len, out byte[] data);
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                DataA = data
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void DeleteMessage(ModbusPkt pkt)
        {
            bool          success = MB.DeleteMessage(pkt.Value);
            AsyncComplete ac      = new AsyncComplete(MB, pkt)
            {
                Success = success
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void RecallMessage(ModbusPkt pkt)
        {
            bool          success = MB.SetAttribute(ccPDR.Recall_Message, pkt.Value);
            AsyncComplete ac      = new AsyncComplete(MB, pkt)
            {
                Success = success
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void WritePattern(ModbusPkt pkt)
        {
            bool          success = MB.SendFixedLogo(pkt.DotMatrix, pkt.Addr, pkt.DataA);
            AsyncComplete ac      = new AsyncComplete(MB, pkt)
            {
                Success = success
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void Disconnect(ModbusPkt pkt)
        {
            MB.Disconnect();
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                Success = true
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void IssueccIJP(ModbusPkt pkt)
        {
            MB.SetAttribute(pkt.Attribute, pkt.Value);
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                Attribute = pkt.Attribute,
                Value     = pkt.Value,
                Status    = GetStatus(),
            };

            parent.BeginInvoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void GetStatus(ModbusPkt pkt)
        {
            char          c  = (char)MB.GetDecAttribute(ccUS.Communication_Status);
            char          r  = (char)MB.GetDecAttribute(ccUS.Receive_Status);
            char          o  = (char)MB.GetDecAttribute(ccUS.Operation_Status);
            char          w  = (char)MB.GetDecAttribute(ccUS.Warning_Status);
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                Status = new string(new char[] { (char)2, (char)0x31, c, r, o, w, (char)3 })
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void WriteData(ModbusPkt pkt)
        {
            MB.SetAttribute(ccIDX.Start_Stop_Management_Flag, 1);
            bool success = MB.SetAttribute(pkt.DevAddr, pkt.Addr, pkt.DataA);

            MB.SetAttribute(ccIDX.Start_Stop_Management_Flag, 2);
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                Success = success
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void SendLabel(ModbusPkt pkt)
        {
            SendRetrieveXML send = new SendRetrieveXML(MB);

            send.Log += Modbus_Log;
            try {
                send.SendXML(pkt.Label);
            } finally {
                AsyncComplete ac = new AsyncComplete(MB, pkt);
                parent.BeginInvoke(new EventHandler(delegate { Complete(this, ac); }));
                send.Log -= Modbus_Log;
                send      = null;
            }
        }
        private void AddMessage(ModbusPkt pkt)
        {
            bool success = MB.DeleteMessage(pkt.Value);

            MB.SetAttribute(ccIDX.Start_Stop_Management_Flag, 1);
            success &= MB.SetAttribute(ccPDR.MessageName, pkt.Data);
            success &= MB.SetAttribute(ccPDR.Message_Number, pkt.Value);
            MB.SetAttribute(ccIDX.Start_Stop_Management_Flag, 2);
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                Success = success
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void Send(ModbusPkt pkt)
        {
            SendRetrieveXML send = new SendRetrieveXML(MB);

            send.Log += Modbus_Log;
            try {
                send.SendXML(pkt.Data);
            } finally {
                string        logXML = send.LogXML;
                AsyncComplete ac     = new AsyncComplete(MB, pkt)
                {
                    Resp2 = logXML
                };
                parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
                send.Log -= Modbus_Log;
                send      = null;
            }
        }
        private void WriteSelectedItems(ModbusPkt pkt)
        {
            bool            success = false;
            SendRetrieveXML send    = new SendRetrieveXML(MB);

            send.Log += Modbus_Log;
            try {
                success = send.WriteSelectedItems(pkt.Item, pkt.Data);
            } finally {
                AsyncComplete ac = new AsyncComplete(MB, pkt)
                {
                    Success = success
                };
                parent.BeginInvoke(new EventHandler(delegate { Complete(this, ac); }));
                send.Log -= Modbus_Log;
                send      = null;
            }
        }
        private void RetrieveMsg(ModbusPkt pkt)
        {
            string          msgXML      = string.Empty;
            SendRetrieveXML retrieveMsg = new SendRetrieveXML(MB);

            retrieveMsg.Log += Modbus_Log;
            try {
                msgXML = retrieveMsg.Retrieve();
            } finally {
                AsyncComplete ac = new AsyncComplete(MB, pkt)
                {
                    Resp1 = msgXML
                };
                parent.BeginInvoke(new EventHandler(delegate { Complete(this, ac); }));
                retrieveMsg.Log -= Modbus_Log;
                retrieveMsg      = null;
            }
        }
        private void DoSubstitutions(ModbusPkt pkt)
        {
            bool            success = false;
            Substitution    sub     = null;
            SendRetrieveXML sr      = new SendRetrieveXML(MB);

            if (pkt.substitution == null)
            {
                sub = sr.RetrieveAllSubstitutions(1);
            }
            else
            {
                sr.SendSubstitution(pkt.substitution);
            }
            sr = null;
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                Success = success, substitution = sub
            };

            parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
        }
        private void GetMessages(ModbusPkt pkt)
        {
            StringBuilder sb   = new StringBuilder();
            List <string> msgs = new List <string>();

            string[] s = new string[3];
            // For now, look at the first 48 only.  Need to implement block read
            AttrData attrCount = MB.GetAttrData(ccMM.Registration);

            for (int i = 0; i < Math.Min(3, attrCount.Count); i++)
            {
                int reg = MB.GetDecAttribute(ccMM.Registration, i);
                if (reg == 0)
                {
                    continue;
                }
                for (int j = 15; j >= 0; j--)
                {
                    if ((reg & (1 << j)) > 0)
                    {
                        int n = i * 16 - j + 15;                      // 1-origin
                        MB.SetAttribute(ccIDX.Start_Stop_Management_Flag, 1);
                        MB.SetAttribute(ccIDX.Message_Number, n + 1); // Load the message into input registers
                        MB.SetAttribute(ccIDX.Start_Stop_Management_Flag, 2);
                        s[0] = MB.GetHRAttribute(ccMM.Group_Number);
                        s[1] = MB.GetHRAttribute(ccMM.Message_Number);
                        s[2] = MB.GetHRAttribute(ccMM.Message_Name);
                        msgs.Add(string.Join(",", s));
                    }
                }
            }
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                MultiLine = msgs.ToArray()
            };

            parent.BeginInvoke(new EventHandler(delegate { Complete(this, ac); }));
        }
 public AsyncComplete(Modbus p, ModbusPkt pkt)
 {
     this.Printer = p;
     this.Type    = pkt.Type;
     this.Packet  = pkt.Packet;
 }
        private void processTasks()
        {
            bool done = false;

            // Just one big loop
            while (!done)
            {
                try {
                    // Wait for the next request
                    pkt = Tasks.Take();
                    switch (pkt.Type)
                    {
                    case TaskType.Connect:
                        Connect(pkt);
                        break;

                    case TaskType.Disconnect:
                        Disconnect(pkt);
                        break;

                    case TaskType.Send:
                        Send(pkt);
                        break;

                    case TaskType.SendLabel:
                        SendLabel(pkt);
                        break;

                    case TaskType.Retrieve:
                        Retrieve(pkt);
                        break;

                    case TaskType.WriteData:
                        WriteData(pkt);
                        break;

                    case TaskType.ReadData:
                        ReadData(pkt);
                        break;

                    case TaskType.RecallMessage:
                        RecallMessage(pkt);
                        break;

                    case TaskType.Substitutions:
                        DoSubstitutions(pkt);
                        break;

                    case TaskType.AddMessage:
                        AddMessage(pkt);
                        break;

                    case TaskType.DeleteMessage:
                        DeleteMessage(pkt);
                        break;

                    case TaskType.IssueccIJP:
                        IssueccIJP(pkt);
                        break;

                    case TaskType.GetStatus:
                        GetStatus(pkt);
                        break;

                    case TaskType.GetMessages:
                        GetMessages(pkt);
                        break;

                    case TaskType.GetErrors:
                        GetErrors(pkt);
                        break;

                    case TaskType.AckOnly:
                        AckOnly(pkt);
                        break;

                    case TaskType.Specification:
                        Specification(pkt);
                        break;

                    case TaskType.WritePattern:
                        WritePattern(pkt);
                        break;

                    case TaskType.TimedDelay:
                        Thread.Sleep(pkt.TimeDelay);
                        AckOnly(pkt);
                        break;

                    case TaskType.Exit:
                        done = true;
                        break;

                    default:
                        return;
                    }
                } catch {
                    AsyncComplete ac = new AsyncComplete(MB, pkt)
                    {
                        Success = false
                    };
                    parent.Invoke(new EventHandler(delegate { Complete(this, ac); }));
                }
            }
        }
Example #25
0
        internal void Retrieve(ModbusPkt pkt)
        {
            string response = "\x02\x03";

            MB.Errors = 0;
            try {
                switch ((RetrieveOps)pkt.SubOp)
                {
                case RetrieveOps.LineSetting:
                    response = LineSetting();
                    break;

                case RetrieveOps.PrintContentsAttributes:
                    response = PrintContentsAttributes();
                    break;

                case RetrieveOps.PrintContentsNoAttributes:
                    response = PrintContentsNoAttributes();
                    break;

                case RetrieveOps.CalendarCondition:
                    response = CalendarCondition();
                    break;

                case RetrieveOps.SubstitutionRule:
                    response = SubstitutionRule();
                    break;

                case RetrieveOps.SubstitutionRuleData:
                    response = SubstitutionRuleData();
                    break;

                case RetrieveOps.ShiftCodeSetup:
                    response = ShiftCodeSetup();
                    break;

                case RetrieveOps.TimeCountCondition:
                    response = TimeCountCondition();
                    break;

                case RetrieveOps.CountCondition:
                    response = CountCondition();
                    break;

                case RetrieveOps.PrintFormat:
                    response = PrintFormat();
                    break;

                case RetrieveOps.AdjustICS:
                    response = AdjustICS();
                    break;

                case RetrieveOps.PrintSpecifications:
                    response = PrintSpecifications();
                    break;

                case RetrieveOps.VariousPrintSetup:
                    response = VariousPrintSetup();
                    break;

                case RetrieveOps.MessageGroupNames:
                    response = MessageGroupNames();
                    break;

                case RetrieveOps.PrintData:
                    response = PrintData();
                    break;

                case RetrieveOps.UserEnvironmentSetup:
                    response = UserEnvironmentSetup();
                    break;

                case RetrieveOps.DateTimeSetup:
                    response = DateTimeSetup();
                    break;

                case RetrieveOps.CommunicationsSetup:
                    response = CommunicationsSetup();
                    break;

                case RetrieveOps.TouchScreenSetup:
                    response = TouchScreenSetup();
                    break;

                case RetrieveOps.UnitInformation:
                    response = UnitInformation();
                    break;

                case RetrieveOps.OperationManagement:
                    response = OperationManagement();
                    break;

                case RetrieveOps.AlarmHistory:
                    response = AlarmHistory();
                    break;

                case RetrieveOps.PartsUsageTime:
                    response = PartsUsageTime();
                    break;

                case RetrieveOps.CirculationSystemSetup:
                    response = CirculationSystemSetup();
                    break;

                case RetrieveOps.SoftwareVersion:
                    response = SoftwareVersion();
                    break;

                case RetrieveOps.AdjustmentOperationalCheckout:
                    response = AdjustmentOperationalCheckout();
                    break;

                case RetrieveOps.SolenoidValvePumpTest:
                    response = SolenoidValvePumpTest();
                    break;

                case RetrieveOps.FreeLayoutCoordinates:
                    response = FreeLayoutCoordinates();
                    break;

                case RetrieveOps.StirrerTest:
                    response = StirrerTest();
                    break;

                case RetrieveOps.MonthSubstituteRule:
                    response = MonthSubstituteRule();
                    break;

                case RetrieveOps.ViscometerCalibration:
                    response = ViscometerCalibration();
                    break;

                case RetrieveOps.SystemEnvironmentSetup:
                    response = SystemEnvironmentSetup();
                    break;

                case RetrieveOps.UserPatternFixed:
                    response = UserPatternFixed(pkt);
                    break;

                case RetrieveOps.StandardCharacterPattern:
                    response = StandardCharacterPattern(pkt);
                    break;

                case RetrieveOps.UserPatternFree:
                    response = UserPatternFree();
                    break;

                default:
                    break;
                }
            } catch (Exception e) {
                Log(this, $"Retrieve \"{(RetrieveOps)pkt.SubOp}\" failed! -- {e.Message}");
                MB.Errors = 1;
            }
            AsyncComplete ac = new AsyncComplete(MB, pkt)
            {
                Resp1 = response, Success = MB.Errors == 0
            };

            parent.BeginInvoke(new EventHandler(delegate { Complete(this, ac); }));
        }