private void SerialThread()
        {
            while (true)
            {
                ISerialCommand setCommand = DequeueSetCommand();
                if (setCommand != null)
                {
                    setCommand.Run(this);
                    continue;
                }

                ISerialCommand queryCommand = DequeueQueryCommand();
                if (queryCommand != null)
                {
                    queryCommand.Run(this);
                    continue;
                }

                if (firstLoop)
                {
                    firstLoop = false;
                }
                else
                {
                    NotifyCommunicationComplete();
                }

                //no set or query commands; wait for one
                waitingForCommand.Set();
                commandAvailable.WaitOne();
                waitingForCommand.Reset();

                NotifyCommunicationBegun();
            }
        }
Пример #2
0
        /// <summary>
        /// 查询某一行TableRow对象
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public TableRow Select(ISerialCommand command)
        {
            /*核心点是如何在多线程下只会创建一条记录*/
            if (this.sorted.ContainsKey(command.Body))
            {
                return(sorted[command.Body]);
            }

            /*创建一行记录太快,这里的锁的性能可以忽略*/
            System.Threading.Monitor.Enter(lockObject);
            try
            {
                if (this.sorted.ContainsKey(command.Body))
                {
                    return(sorted[command.Body]);
                }

                return(sorted[command.Body] = new TableRow());
            }
            catch
            {
            }
            finally
            {
                System.Threading.Monitor.Exit(lockObject);
            }

            return(sorted[command.Body] = new TableRow());
        }
Пример #3
0
        /// <summary>
        /// 查询某一Table对象
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public static Table Use(ISerialCommand command)
        {
            if (collections.Count == 0)
            {
                return(Create(command.GetType()));
            }

            return(collections[command.GetType()]);
        }
Пример #4
0
        public bool SendCommand(ISerialCommand cmd)
        {
            if (!serialPort.IsOpen)
                return false;

            try
            {
                serialPort.Write(cmd.Command);
                serialPort.Write("\r");

                return cmd.ParseResponse(serialPort);
            }
            catch (Exception) { return false; }
        }
 private ISerialCommand DequeueQueryCommand()
 {
     lock (semaphore)
     {
         if (queryCommandQueue.Count > 0)
         {
             ISerialCommand command = queryCommandQueue.Keys[0];
             queryCommandQueue.Remove(command);
             currentQueryCommands.Remove(command.ICommand);
             return(command);
         }
         return(null);
     }
 }
        public override ErrorCode SendPacket(ISerialCommand sourceComand, byte[] packet, out string commandId, out string response)
        {
            //return the default value as the response
            commandId = sourceComand.ICommand.Id;
            response  = sourceComand.ICommand.DefaultValueAsString;

            if (!IsConnected())
            {
                return(ErrorCode.NotConnected);
            }

            //Context.Logger.Info("Command bytes: " + BitConverter.ToString(packet));

            //simulate a serial port wait
            Thread.Sleep(20);
            return(ErrorCode.NoError);
        }
        public void SetValue <T>(Command <T> command, T value, ICommandListener <T> listener, int delayAfterCommandInMillis)
        {
            ISerialCommand serialCommand = new SerialSetCommand <T>(command, value, listener, commandId++, delayAfterCommandInMillis);

            lock (semaphore)
            {
                if (!queueDuplicateSetCommands)
                {
                    if (currentSetCommands.ContainsKey(command))
                    {
                        ISerialCommand key = currentSetCommands[command];
                        setCommandQueue.Remove(key);
                        currentSetCommands.Remove(command);
                        key.NotifyCancelled();
                    }
                    currentSetCommands.Add(command, serialCommand);
                }
                setCommandQueue.Add(serialCommand, serialCommand);
                serialCommand.NotifyQueued();
            }
            commandAvailable.Set();
        }
Пример #8
0
        public override ErrorCode SendPacket(ISerialCommand sourceComand, byte[] packet, out string commandId, out string response)
        {
            if (!IsConnected())
            {
                commandId = null;
                response  = null;
                return(ErrorCode.NotConnected);
            }

            port.DiscardOutBuffer();
            try
            {
                port.Write(packet, 0, packet.Length);
            }
            catch (Exception e)
            {
                Context.Logger.Error(e);
                commandId = null;
                response  = null;
                return(ErrorCode.UnknownError);
            }
            return(DuoProtocol.ReadResponse(port, out commandId, out response));
        }
Пример #9
0
 public int CompareTo(ISerialCommand other)
 {
     return(this.CommandId.CompareTo(other.CommandId));
 }
 public abstract ErrorCode SendPacket(ISerialCommand sourceComand, byte[] packet, out string commandId, out string response);