Ejemplo n.º 1
0
        private void StartSerialPort(object sender, SerialPortPropertiesEventArgs e)
        {
            // Common serial port exceptions: TimeoutException, IOException, InvalidOperationException
            Log.Information("Opening Serial Port...");
            try
            {
                serialPort = new SerialPort()
                {
                    PortName     = e.Port,
                    BaudRate     = e.BaudRate,
                    Parity       = e.Parity,
                    DataBits     = e.Databits,
                    StopBits     = e.Stopbits,
                    Handshake    = e.Handshake,
                    ReadTimeout  = e.ReadTimeout,
                    WriteTimeout = e.WriteTimeout,
                };

                controller.SerialPort = serialPort;
                controller.Startup();
                controller.PingDevice();
                Log.Information("Serial Port Successfully opened.");
            }
            catch (Exception ex)
            {
                Log.Error($"Error has occured. Serial Port Opening Failed.\n{ex.Message}");
            }
        }
        public void TestMethod1()
        {
            sp1 = new SerialPort()
            {
                PortName     = "COM1",
                BaudRate     = 9600,
                Parity       = Parity.None,
                DataBits     = 8,
                StopBits     = StopBits.One,
                Handshake    = Handshake.None,
                ReadTimeout  = 5000,
                WriteTimeout = 5000
            };
            sp2 = new SerialPort()
            {
                PortName     = "COM2",
                BaudRate     = 9600,
                Parity       = Parity.None,
                DataBits     = 8,
                StopBits     = StopBits.One,
                Handshake    = Handshake.None,
                ReadTimeout  = 500,
                WriteTimeout = 500
            };
            sp2.Open();
            sp2.DataReceived += OnDataReceived2;

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .WriteTo.Console(outputTemplate:
                                          "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Level:u4}] {Message:lj}{NewLine}{Exception}")
                         .WriteTo.File("logs\\myapp.txt", rollingInterval: RollingInterval.Day, shared: true)
                         .CreateLogger();
            IHeightSensorController ODValue = new SickODController(sp1);

            ODValue.Startup();
            ODValue.Shutdown();
        }