示例#1
0
        public static async Task <bool> InitSerialServiceNew(SensorModel model)
        {
            sensorModel = model;
            try
            {
                while (null == serialPort)
                {
                    await InitSerialPort();

                    await Task.Delay(TimeSpan.FromSeconds(30));
                }

                // Configure serial settings
                serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                serialPort.ReadTimeout  = TimeSpan.FromMilliseconds(1000);
                serialPort.BaudRate     = 9600;
                serialPort.Parity       = SerialParity.None;
                serialPort.StopBits     = SerialStopBitCount.One;
                serialPort.DataBits     = 8;
                serialPort.Handshake    = SerialHandshake.None;

                //                // Display configured settings
                //                string status = "Serial port configured successfully: ";
                //                status += serialPort.BaudRate + "-";
                //                status += serialPort.DataBits + "-";
                //                status += serialPort.Parity.ToString() + "-";
                //                status += serialPort.StopBits;

                // Create cancellation token object to close I/O operations when closing the device
                readCancellationTokenSource = new CancellationTokenSource();

                NewListen();
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
示例#2
0
        public static async Task <bool> InitSerialService(SensorModel model)
        {
            sensorModel = model;
            try
            {
                //There should only be one serial device available. Select it.
                string aqs = SerialDevice.GetDeviceSelector();
                var    dis = await DeviceInformation.FindAllAsync(aqs);

                DeviceInformation entry = dis[0];
                serialPort = await SerialDevice.FromIdAsync(entry.Id);

                // Configure serial settings
                serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                serialPort.ReadTimeout  = TimeSpan.FromMilliseconds(1000);
                serialPort.BaudRate     = 9600;
                serialPort.Parity       = SerialParity.None;
                serialPort.StopBits     = SerialStopBitCount.One;
                serialPort.DataBits     = 8;
                serialPort.Handshake    = SerialHandshake.None;

//                // Display configured settings
//                string status = "Serial port configured successfully: ";
//                status += serialPort.BaudRate + "-";
//                status += serialPort.DataBits + "-";
//                status += serialPort.Parity.ToString() + "-";
//                status += serialPort.StopBits;

                // Create cancellation token object to close I/O operations when closing the device
                readCancellationTokenSource = new CancellationTokenSource();

                Listen();
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }