Пример #1
0
        public void Run(MsDevice dev)
        {
            var c = this.Build();

            if (c == null)
            {
                return;
            }
            if (c.ioList != null)
            {
                foreach (var v in c.ioList) // register IO variables
                {
                    var t = _owner.parent.Get(v, true, _owner);
                    t.SetField("MQTT-SN.tag", v);
                }
            }
            var ch_t = JSC.JSObject.CreateObject();

            if (c.varList != null)
            {
                string       n;
                JSC.JSObject o, mo, mqo;
                foreach (var kv in c.varList)
                {
                    n             = kv.Key.Replace('.', '_');
                    o             = JSC.JSObject.CreateObject();
                    mo            = JSC.JSObject.CreateObject();
                    mqo           = JSC.JSObject.CreateObject();
                    mqo["tag"]    = kv.Value;
                    mo["MQTT-SN"] = mqo;
                    o["manifest"] = mo;
                    var            nt = MsDevice.NTTable.FirstOrDefault(z => kv.Value.StartsWith(z.Item1));
                    MsDevice.DType vt = nt == null?MsDevice.DType.Integer:nt.Item2;
                    switch (vt)
                    {
                    case MsDevice.DType.Boolean:
                        o["default"] = false;
                        o["type"]    = "Boolean";
                        break;

                    case MsDevice.DType.ByteArray:
                        o["default"] = null;
                        o["type"]    = "ByteArray";
                        break;

                    case MsDevice.DType.String:
                        o["default"] = string.Empty;
                        o["type"]    = "String";
                        break;

                    default:
                        o["default"] = 0;
                        o["type"]    = "Integer";
                        break;
                    }
                    o["menu"] = "plc";
                    ch_t[n]   = o;
                }
            }
            _owner.parent.SetField("Children", ch_t, _owner);
            string sTag;
            var    varLst = _owner.parent.children.Where(z => (sTag = z.GetField("MQTT-SN.tag").Value as string) != null && sTag.StartsWith("M")).ToArray();
            var    rereg  = new List <Tuple <Topic, string> >();

            foreach (var t in varLst)
            {
                sTag = t.GetField("MQTT-SN.tag").Value as string;
                var vt = c.varList.FirstOrDefault(z => t.name == z.Key.Replace('.', '_'));
                if (vt.Key == null)
                {
                    rereg.Add(new Tuple <Topic, string>(t, string.Empty));
                }
                else if (sTag != vt.Value)
                {
                    rereg.Add(new Tuple <Topic, string>(t, vt.Value));
                }
            }
            if (rereg.Any())
            {
                dev.ReReg(rereg);
            }
            _stackBottom = (c.StackBottom + 3) / 4;
            _prg         = new SortedSet <Chunk>();
            foreach (var kv in c.Hex)
            {
                var ch = new Chunk((int)kv.Key);
                ch.Data   = kv.Value;
                ch.crcCur = Crc16.UpdateCrc(0xFFFF, ch.Data);
                _prg.Add(ch);
                if (System.Threading.Interlocked.CompareExchange(ref _st, 1, 0) == 0)
                {
                    _curChunk = ch;
                }
            }
        }
Пример #2
0
        public void Tick()
        {
            byte[] buf;

            if (_st == 0 || _st == 9)
            {
                return;
            }
            else
            {
                if (_prg == null)
                {
                    _st = 0;
                    return;
                }
                if (_curChunk == null && _st < 7)
                {
                    _st = 1;
                }
                if (_st == 1)
                {
                    if (_curChunk == null)
                    {
                        _curChunk = _prg.FirstOrDefault(z => z.crcCur != z.crcDev);
                        if (_curChunk == null)
                        {
                            if (_plcStoped)
                            {
                                _st = 7;
                                _pub(new byte[] { (byte)Cmd.WriteStackBottomReq, (byte)_stackBottom, (byte)(_stackBottom >> 8), (byte)(_stackBottom >> 16), (byte)(_stackBottom >> 24) });
                            }
                            else
                            {
                                _st = 0;
                            }
                            return;
                        }
                    }
                    _pub(new byte[] { (byte)Cmd.GetCRCReq, (byte)_curChunk.offset, (byte)(_curChunk.offset >> 8), (byte)_curChunk.Data.Length, (byte)(_curChunk.Data.Length >> 8) });
                    _st = 2;
                }
                else if (_st == 3)
                {
                    _pub(new byte[] { (byte)Cmd.PlcStopReq });
                    _st = 4;
                }
                else if (_st == 5)
                {
                    int len = _curChunk.Data.Length - _offset; //-V3125
                    if (len > 32)
                    {
                        len = 32;
                    }
                    buf = new byte[len + 5];
                    int addr = _curChunk.offset + _offset;
                    buf[0] = (byte)(Cmd.WriteBlockReq);
                    buf[1] = (byte)addr;
                    buf[2] = (byte)(addr >> 8);
                    Buffer.BlockCopy(_curChunk.Data, _offset, buf, 3, len);
                    ushort crc = Crc16.UpdateCrc(0xFFFF, buf.Skip(3).Take(len).ToArray());
                    buf[len + 3] = (byte)crc;
                    buf[len + 4] = (byte)(crc >> 8);
                    _st          = 6;
                    _pub(buf);
                    if (verbose)
                    {
                        Log.Info("{0}.write 0x{1:X4} {2}", _owner.path, addr, BitConverter.ToString(buf, 3, len));
                    }
                }
                else if (_st == 8)
                {
                    _st = 9;
                    _pub(new byte[] { (byte)Cmd.PlcStartReq });
                }
            }
        }