Esempio n. 1
0
        public static DataReport GetReport([Implicit] ICallContext context, string ID)
        {
            var internalContext = context as ScriptCallContext;
            var report          = internalContext.ListReports().FirstOrDefault(r => String.Equals(ID, r.ID, StringComparison.InvariantCulture));

            if (report == null)
            {
                context.ReportError("Failed to find report named \"" + ID + "\".");
            }
            return(report);
        }
Esempio n. 2
0
 public ReceivedStatus CreateStatusReceiver([Implicit] ICallContext context, string name, Predicate <IMessage> filter)
 {
     if (m_receivers.FirstOrDefault(r => !String.IsNullOrEmpty(r.Name) && String.Equals(r.Name, name, StringComparison.InvariantCulture)) != null)
     {
         if (context != null)
         {
             context.ReportError(description: "There are already a receiver named \"" + name + "\".");
         }
         return(null);
     }
     else
     {
         if (context != null && context.LoggingEnabled)
         {
             context.Logger.Log("PCANChannel.CreateStatusReceiver", "Registered status receiver named \"" + name + "\".");
         }
         var s = new ReceivedStatus(name, filter);
         m_receivers.Insert(m_receivers.Count - 1, s);
         return(s);
     }
 }
Esempio n. 3
0
        public override string ReadLine([Implicit] ICallContext context, TimeSpan timeout)
        {
            if (m_port.IsOpen)
            {
                var entry    = DateTime.Now;
                var timeleft = timeout;

                if (m_textualFifo == null)
                {
                    while (m_port.BytesToRead == 0 && timeleft >= TimeSpan.Zero)
                    {
                        System.Threading.Thread.Sleep(20);
                        if (timeout != TimeSpan.Zero)
                        {
                            timeleft = DateTime.Now.TimeTill(entry + timeout);
                        }
                    }
                    if (m_port.BytesToRead > 0)
                    {
                        var line = m_port.ReadLine();
                        if (context != null && context.LoggingEnabled)
                        {
                            context.Logger.Log("SerialPort", "ReadLine : " + StringUtils.ObjectToString(line));
                        }
                        return(line);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    var newLineLen       = m_port.NewLine.Length;
                    int newLineCharsSeen = 0;
                    var knownCount       = 0;
                    while (timeleft >= TimeSpan.Zero)
                    {
                        if (m_textualFifo.AwaitNewData(knownCount, timeleft))
                        {
                            while (knownCount < m_textualFifo.Count)
                            {
                                if (m_textualFifo[knownCount] == m_port.NewLine[newLineCharsSeen])
                                {
                                    newLineCharsSeen++;
                                    if (newLineCharsSeen == newLineLen)
                                    {
                                        var line = new string(m_textualFifo.Get(0, knownCount + (1 - newLineLen), knownCount + 1));
                                        context.Logger.Log("SerialPort", "ReadLine : " + StringUtils.ObjectToString(line));
                                        return(line);
                                    }
                                }
                                else
                                {
                                    newLineCharsSeen = 0;
                                }
                                knownCount++;
                            }
                            if (timeout != TimeSpan.Zero)
                            {
                                timeleft = DateTime.Now.TimeTill(entry - timeout);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (context != null)
                    {
                        context.ReportError("No lines read.");
                    }
                    return(null);
                }
            }
            else
            {
                if (context != null)
                {
                    context.ReportError("ReadLine, but port is not open.");
                }
            }
            return(null);
        }