Пример #1
0
 public static DTPMaster CreateFromSerial(int TimeOut, Sender Sender, bool SyncTime)
 {
     if (SerialPacketReader.FirstAvailable(TimeOut, out var reader, out var writer, out var sender, SyncTime))
     {
         var master = new DTPMaster(Sender, new PacketListener(reader, writer));
         master.Device._deviceSender = sender;
         return(master);
     }
Пример #2
0
        internal static bool FirstAvailable(
            int TimeOutInterval,
            out SerialPacketReader reader,
            out SerialPacketWriter writer,
            out Sender sender,
            bool SyncTyme)
        {
            sender = null;
            reader = null; writer = null;
            var ports = SerialPort.GetPortNames();

            if (ports == null)
            {
                return(false);
            }
            foreach (var item in ports)
            {
                var port = new SerialPort(item, 115200);
                try { port.Open(); }
                catch { return(false); }
                if (port.IsOpen)
                {
                    reader = new SerialPacketReader(port, 500);
                    writer = new SerialPacketWriter(port);
                    var a   = new GeneralPacketHandler(Sender.Empty, new PacketListener(reader, writer));
                    var res = a.Device_Test();
                    if (res != null)
                    {
                        if (SyncTyme)
                        {
                            if (a.Device_SyncTime() == -1)
                            {
                                throw new FailOperationException("Неудалось синхронизировать время.");
                            }
                        }
                        reader.TimeOutInterval = TimeOutInterval;
                        sender = res;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            return(false);
        }