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 SerialCreater.Instance.Create(SerialEnum.LowerComputer);

                    if (serialPort != null)
                    {
                        serialPort.ReceiveHandler += SpHelper_ReceiveHandler;
                    }
                    else
                    {
                        Debug.WriteLine("LowerComputer is null");
                    }
                }

                if (serialPort != null)
                {
                    await serialPort.Open();

                    if (waitForFeedbackDirectives.ContainsKey(item.DirectiveId))
                    {
                        await serialPort.Send(directiveData, cancelTokenSource.Token);
                    }
                }
            }
            catch (CustomException)
            {
                Debug.WriteLine("send error");
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("send cancel");
            }
            catch (Exception e)
            {
                OnErrorEvent(new CustomException(e.Message + "Send", this.GetType().FullName,
                                                 ExceptionPriority.Unrecoverable), item);
            }
        }
Beispiel #2
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") continue;

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

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

                var helper = new SerialPortHelper(device);
//                var x = await helper.Open();

                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;
                    }
                }
            }
        }
Beispiel #3
0
        public async Task SendData(string msg)
        {
            var x = Encoding.UTF8.GetBytes(msg);
            var p = new CancellationTokenSource();

            if (serialPort == null)
            {
                serialPort = await SerialCreater.Instance.Create(SerialEnum.Sim);

                if (serialPort == null)
                {
                    Debug.WriteLine("sim is null");
                    return;
                }
                serialPort.ReceiveHandler += SerialPort_ReceiveHandler;
            }

            if (serialPort != null)
            {
                await serialPort.Open();

                await serialPort.Send(x.Concat(new byte[] { 0x0D, 0x0A }).ToArray(), p.Token);
            }
        }