Пример #1
0
        public async Task <SerialPortHelper> Create(SerialEnum serial)
        {
            if (serialHelpers.ContainsKey(serial))
            {
                var p = serialHelpers[serial];

                if (p.serialPort != null)
                {
                    return(serialHelpers[serial]);
                }

                serialHelpers.Remove(serial);
            }

            Debug.WriteLine("start build");
            await Build();

            Debug.WriteLine("finish build");

            if (serialHelpers.ContainsKey(serial))
            {
                return(serialHelpers[serial]);
            }

            return(null);
        }
Пример #2
0
        private byte[] generatePingBytesByType(SerialEnum serialType)
        {
            byte[] ret = {};
            switch (serialType)
            {
            case SerialEnum.LowerComputer:
                ret = generatePingBytes(Config.DetectorId);
                break;

            case SerialEnum.Sim:
                ret = Encoding.UTF8.GetBytes("AT+CCID").Concat(new byte[] { 0x0D, 0x0A }).ToArray();
                break;

            default: break;
            }

            return(ret);
        }
Пример #3
0
        public async Task <SerialPortHelper> Create(SerialEnum serial)
        {
            if (serialHelpers.ContainsKey(serial))
            {
                return(serialHelpers[serial]);
            }

            await Build();

            Debug.WriteLine("build finish");

            if (serialHelpers.ContainsKey(serial))
            {
                return(serialHelpers[serial]);
            }

            return(null);
        }
Пример #4
0
        public async Task Open(SerialEnum serialType)
        {
            await Task.Yield();

            if (Status == SerialPortStatus.Initialled)
            {
                try
                {
                    Status = SerialPortStatus.Opening;
                    client.Connect(ADDR, PORT);

                    Status = SerialPortStatus.Opened;

                    _readCancellationTokenSource = new CancellationTokenSource();
                    Listen().IgnorCompletion();
                }
                catch (Exception e)
                {
                    LogFactory.Create().Info("net serial open error" + e.Message);
                    Status = SerialPortStatus.Initialled;
                }
            }
        }
Пример #5
0
        public async Task Open(SerialEnum serialType)
        {
            if (Status == SerialPortStatus.Initialled)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Status = SerialPortStatus.Opening;
                var dis = SerialPort.GetPortNames();
                if (!dis.Any())
                {
                    return;
                }

                foreach (COMPortInfo comPort in COMPortInfo.GetCOMPortsInfo())
                {
                    Console.WriteLine($"{comPort.Name} – {comPort.Description}");
                    if (comPort.Description.IndexOf("USB", StringComparison.Ordinal) == -1 &&
                        comPort.Description.IndexOf("Virtual Serial Port", StringComparison.Ordinal) == -1)
                    {
                        continue;
                    }

                    try
                    {
                        var portName = comPort.Name.Split("->".ToCharArray())[0];
                        LogFactory.Create().Info("portname:" + portName);

                        SerialPort = new SerialPort(portName);
                        if (SerialPort == null)
                        {
                            continue;
                        }

                        SerialPort.WriteTimeout    = 50;
                        SerialPort.ReadTimeout     = 50;
                        SerialPort.BaudRate        = 9600;
                        SerialPort.Parity          = Parity.None;
                        SerialPort.StopBits        = StopBits.One;
                        SerialPort.DataBits        = 8;
                        SerialPort.Handshake       = Handshake.None;
                        SerialPort.WriteBufferSize = 1024;
                        SerialPort.ReadBufferSize  = 1024;

                        SerialPort.DataReceived  += SerialPort_DataReceived;
                        SerialPort.ErrorReceived += SerialPort_ErrorReceived;

                        _readCancellationTokenSource = new CancellationTokenSource();
                        SerialPort.Open();
                        Send(generatePingBytesByType(serialType), CancellationToken.None);

                        var cancellationToken = new CancellationTokenSource(1000).Token;
                        completionSource = new TaskCompletionSource <byte[]>();
                        using (cancellationToken.Register(() => completionSource.TrySetResult(new byte[] { })))
                        {
                            var x   = await completionSource.Task;
                            var ret = Shunxi.Common.Utility.Common.BytesToString(x);
                            if (!string.IsNullOrEmpty(ret) && ret.Length > 10)
                            {
                                Status = SerialPortStatus.Opened;
                                LogFactory.Create().Info($"Serial Port {portName} Opened");
                                break;
                            }

                            Status = SerialPortStatus.Initialled;
                            SerialPort.DataReceived  -= SerialPort_DataReceived;
                            SerialPort.ErrorReceived -= SerialPort_ErrorReceived;
                            SerialPort.Close();
                            SerialPort.Dispose();
                            SerialPort = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (SerialPort != null)
                        {
                            SerialPort.DataReceived  -= SerialPort_DataReceived;
                            SerialPort.ErrorReceived -= SerialPort_ErrorReceived;
                        }

                        Status = SerialPortStatus.Initialled;
                        SerialPort?.Close();
                        SerialPort?.Dispose();
                        LogFactory.Create().Info("port open error" + ex.Message);
                    }
                }

                sw.Stop();
                LogFactory.Create().Info("open port consume time " + sw.ElapsedMilliseconds);
            }
        }