using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.IO.Ports;
namespace WpfApplication19
{
    public class OBInterface
    {
        SerialPort _sp;
        public OBInterface(SerialPort sp)
        {
            _sp = sp;
            DataSender sender = new DataSender(_sp);
            DataReceiver receiver = new DataReceiver(_sp);

            Thread pro = new Thread(new ThreadStart(sender.ThreadRun));
            Thread con = new Thread(new ThreadStart(receiver.ThreadRun));

            try
            {
                pro.Start();
                con.Start();
                pro.Join();
                con.Join();
            }
            catch (ThreadStateException e)
            {
                Console.WriteLine(e);
            }
            catch (ThreadInterruptedException e)
            {
                Console.WriteLine(e);
            }
        }
    }
    public class DataSender
    {
        SerialPort _serialPort;
        public DataSender(SerialPort sp)
        {
            _serialPort = sp;
        }
        public void ThreadRun()
        {
            while(_serialPort.IsOpen){
                Console.WriteLine("Sending");
                Thread.Sleep(500);
            }
        }
    }
    public class DataReceiver
    {
        SerialPort _serialPort;
        public DataReceiver(SerialPort sp)
        {
            _serialPort = sp;
        }
        public void ThreadRun()
        {
            while (_serialPort.IsOpen)
            {
                Console.WriteLine("Reading");
                Thread.Sleep(500);
            }
        }
    }
    class Connector
    {
        bool _readerFlag = false;
        public void ReadFromComPort()
        {
            lock (this)
            {
                if (!_readerFlag)
                {
                    try
                    {
                        Monitor.Wait(this);
                    }
                    catch (SynchronizationLockException e)
                    {
                        Console.WriteLine(e);
                    }
                    catch (ThreadInterruptedException e)
                    {
                        Console.WriteLine(e);
                    }
                }
                Console.WriteLine("Reader");
            }
        }
        public void WriteToComPort()
        {

        }
    }
}
