Exemple #1
0
        public async Task SendData(string msg)
        {
            var x = Encoding.UTF8.GetBytes(msg);
            var p = new CancellationTokenSource();

            // 第一次加载
            if (serialPort == null)
            {
                serialPort = await getSerialPort();
            }

            if (serialPort != null)
            {
                // 运行过程中串口断开了
                if (serialPort.serialPort == null)
                {
                    serialPort.ReceiveHandler -= SerialPort_ReceiveHandler;
                    serialPort = await getSerialPort();
                }

                if (serialPort != null)
                {
                    await serialPort.Send(x.Concat(new byte[] { 0x0D, 0x0A }).ToArray(), p.Token);
                }
            }
        }
Exemple #2
0
        private async Task <SerialPortHelper> getSerialPort()
        {
            serialPort = await SerialCreater.Instance.Create(SerialEnum.Sim);

            if (serialPort != null)
            {
                serialPort.ReceiveHandler += SerialPort_ReceiveHandler;
            }
            else
            {
                Debug.WriteLine("sim is null");
            }
            return(serialPort);
        }
        public async Task Send(BaseDirective item, int reSendTimes = 0)
        {
            try
            {
                waitForFeedbackDirectives.TryAdd(item.DirectiveId, new WaitForFeedBack(DateTime.Now, item, reSendTimes));

                var directiveData = protocolProvider.GenerateDirectiveBuffer(item);
                //第一次加载
                if (serialPort == null)
                {
                    serialPort = await getSerialPort();
                }

                if (serialPort != null)
                {
                    // 运行过程中串口断开了
                    if (serialPort.serialPort == null)
                    {
                        serialPort.ReceiveHandler -= SpHelper_ReceiveHandler;
                        serialPort = await getSerialPort();
                    }

                    if (waitForFeedbackDirectives.ContainsKey(item.DirectiveId) && serialPort != null)
                    {
                        await serialPort.Send(directiveData, cancelTokenSource.Token);
                    }
                }
            }
            catch (CustomException e)
            {
                Debug.WriteLine("send error->" + e.Message);
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("send cancel");
            }
            catch (Exception e)
            {
                OnErrorEvent(new CustomException(e.Message + "Send", this.GetType().FullName,
                                                 ExceptionPriority.Unrecoverable), item);
            }
        }
Exemple #4
0
        public async Task Build()
        {
            string aqs = SerialDevice.GetDeviceSelector();
            var    dis = await DeviceInformation.FindAllAsync(aqs);

            foreach (var each in dis)
            {
                Debug.WriteLine("portname:" + each.Name);
                if (each.Name.IndexOf("USB", StringComparison.Ordinal) == -1 &&
                    each.Name != "MINWINPC" &&
                    each.Name.IndexOf("Virtual Serial Port", StringComparison.Ordinal) == -1)
                {
                    continue;
                }

                var device = await SerialDevice.FromIdAsync(each.Id);

                if (device == null)
                {
                    Debug.WriteLine(each.Name + "is null");
                    continue;
                }

                var helper = new SerialPortHelper(device);

                var cancellationToken            = new CancellationTokenSource(3 * 1000).Token;
                var cancellationCompletionSource = new TaskCompletionSource <SerialEnum>();

                using (cancellationToken.Register(() => cancellationCompletionSource.TrySetResult(SerialEnum.Unknown)))
                {
                    var task = await Task.WhenAny(helper.Open(), cancellationCompletionSource.Task);

                    Debug.WriteLine("open:" + task.Result);
                    if (task.Result != SerialEnum.Unknown)
                    {
                        serialHelpers[task.Result] = helper;
                    }
                }
            }
        }