Ejemplo n.º 1
0
 protected abstract bool readTable(ReadOnlyTable value, int timeoutval);
Ejemplo n.º 2
0
 public MiniModbusDevice(byte id)
 {
     _gen_data             = new ReadOnlyTable(MiniModbusCommand.READ_DEVICE_INFO_COMMAND, 6);
     _id                   = id;
     _request_to_write_ROM = false;
 }
Ejemplo n.º 3
0
        void _processAllData()
        {
            while (_need_to_stop == false)
            {
                int interdelay = 1;
                Thread.Sleep(20);
                clearInput();
                AllCyclesNumber++;
                if (readTable(_gen_data, 100))
                {
                    if (_gen_data.DataWasUpdated())
                    {
                        UInt16[] t = _gen_data.ReadDeviceData();
                        _RO_data            = new ReadOnlyTable(MiniModbusCommand.READ_RO_COMMAND, t[3]);
                        _RW_data            = new ReadWriteTable(MiniModbusCommand.READ_RW_COMMAND, MiniModbusCommand.WRITE_RW_COMMAND, t[4]);
                        _RW_flash           = new ReadWriteTable(MiniModbusCommand.READ_FLASH_BUFFER_COMMAND, MiniModbusCommand.WRITE_FLASH_BUFFER_COMMAND, t[5]);
                        _RW_flash.WriteFlag = false;
                        if (deviceWasChanged != null)
                        {
                            deviceWasChanged(this, null);
                            ResetCounters();
                        }
                    }
                }
                else
                {
                    continue;
                }

                Thread.Sleep(interdelay);
                if (readTable(_RO_data, 100) == false)
                {
                    continue;
                }
                Thread.Sleep(interdelay);
                if (readTable(_RW_data, 100) == false)
                {
                    continue;
                }
                Thread.Sleep(interdelay);
                if (readTable(_RW_flash, 100) == false)
                {
                    continue;
                }
                Thread.Sleep(interdelay);
                if (readingIsComplete != null)
                {
                    readingIsComplete(this, null);
                }
                Thread.Sleep(interdelay);
                if (writeTable(_RW_data, 100) == false)
                {
                    continue;
                }
                Thread.Sleep(interdelay);
                if (writeTable(_RW_flash, 100) == false)
                {
                    continue;
                }
                if (_request_to_write_ROM)
                {
                    _request_to_write_ROM = false;
                    if (writeRom(100) == false)
                    {
                        continue;
                    }
                }

                GoodCyclesNumber++;
                if (cycleIsComplete != null)
                {
                    cycleIsComplete(this, null);
                }
            }
        }
Ejemplo n.º 4
0
        protected override bool readTable(ReadOnlyTable table, int timeoutval)
        {
            Debug.Assert(table.Size != 0);

            UInt16[] receive_data = new UInt16[table.Size];

            int wasread = 0;

            while (wasread != table.Size)
            {
                int size = table.Size - wasread;
                if (size > max_read_size)
                {
                    size = max_read_size;
                }

                Debug.Assert(size < 0x80);
                Debug.Assert(wasread < 0x200);

                byte[] tosend = new byte[4];
                tosend[0] = _id;
                tosend[1] = (byte)table.ReadCmd;
                tosend[2] = (byte)wasread;
                tosend[3] = (byte)size;
                if (wasread >= 0x100)
                {
                    tosend[3] |= 0x80;
                }

                if (!_serial.Send(tosend))
                {
                    return(false);
                }

                byte[] toreceive = null;

                do
                {
                    Thread.Sleep(1);
                    toreceive = _serial.Receive();
                }while ((toreceive == null) && (timeoutval-- != 0));

                if (toreceive == null)
                {
                    return(false);
                }

                if (toreceive.Length != (4 + size * 2))
                {
                    return(false);
                }

                for (int i = 0; i < 4; i++)
                {
                    if (toreceive[i] != tosend[i])
                    {
                        return(false);
                    }
                }

                for (int i = 0; i < size; i++)
                {
                    UInt16 t = Convert.ToUInt16(toreceive[4 + 2 * i] + (toreceive[4 + 2 * i + 1] << 8));
                    receive_data[wasread + i] = t;
                }

                wasread += size;
            }

            table._set(receive_data);
            return(true);
        }