Example #1
0
 public static void SaveComPortConnectorSetting(ComPortConnectorSetting setting)
 {
     using (var writer = new StreamWriter(comportSettingJson))
     {
         var json = JsonConvert.SerializeObject(setting);
         writer.Write(json);
     }
 }
        public void InitializeSerialPort(ComPortConnectorSetting setting)
        {
            try
            {
                if (_serialPort.IsOpen)
                {
                    _serialPort.Close();
                }

                string[] ports = SerialPort.GetPortNames();
                var      port  = ports.FirstOrDefault(x => x == setting.ComPortName);
                if (setting != null && port != null)
                {
                    _serialPort.PortName = port;
                    _serialPort.BaudRate = setting.BaudRate;
                    _serialPort.DataBits = setting.DataBits;
                    _serialPort.StopBits = setting.StopBits;
                    _serialPort.Parity   = setting.Parity;
                    _serialPort.Open();
                    _serialPort.DataReceived  -= SerialPort_DataReceived;
                    _serialPort.ErrorReceived -= SerialPort_ErrorReceived;
                    _serialPort.DataReceived  += SerialPort_DataReceived;
                    _serialPort.ErrorReceived += SerialPort_ErrorReceived;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            //Task.Run(async () =>
            //{
            //    await Imitation(20);
            //    await Task.Delay(5000);
            //    await Imitation(20);
            //    await Task.Delay(5000);
            //    await Imitation(20);
            //    await Imitation(20);
            //});
        }